Determining IP of client in Windows Terminal Server, .NET

  • Thread starter Thread starter green
  • Start date Start date
G

green

Hey there - I'm looking for a working solution to get the IP address of
a client that has connected into a terminal services server.

IE, the user would be logged into the server, and they would run an app
that would like to know the ip address that they're connecting from.

I really need the solution to work in a .NET app (VB).

I've seen the tons of sample code out there for
WTSQuerySessionInformation, but no matter what I do I can't seem to get
it to run in VB.NET....

In particular, when I run this code,

Dim joe As New System.IntPtr
Dim bytes As New UInteger

Dim pp As New WtsApi32.WtsClientAddress

Dim t As Boolean =
WtsApi32.NativeMethods.WtsQuerySessionInformation(0, -1,
WtsInfoClass.WtsClientAddress, joe, bytes)
pp = CType(Marshal.PtrToStructure(joe,
GetType(WtsApi32.WtsClientAddress)), WtsApi32.WtsClientAddress)
bla = pp.Address(2) & "." & pp.Address(3) & "." & pp.Address(4)
& "." & pp.Address

I get an error assigning PP, "AccessViolationException"

I'm using .NET 2.0 - is any of this found in native .NET code? If not,
can anyone help me out?
 
Hey there - I'm looking for a working solution to get the IP address of
a client that has connected into a terminal services server.

IE, the user would be logged into the server, and they would run an app
that would like to know the ip address that they're connecting from.

I really need the solution to work in a .NET app (VB).

I've seen the tons of sample code out there for
WTSQuerySessionInformation, but no matter what I do I can't seem to get
it to run in VB.NET....

In particular, when I run this code,

Dim joe As New System.IntPtr
Dim bytes As New UInteger

Dim pp As New WtsApi32.WtsClientAddress

Dim t As Boolean =
WtsApi32.NativeMethods.WtsQuerySessionInformation(0, -1,
WtsInfoClass.WtsClientAddress, joe, bytes)
pp = CType(Marshal.PtrToStructure(joe,
GetType(WtsApi32.WtsClientAddress)), WtsApi32.WtsClientAddress)
bla = pp.Address(2) & "." & pp.Address(3) & "." & pp.Address(4)
& "." & pp.Address

I get an error assigning PP, "AccessViolationException"

I'm using .NET 2.0 - is any of this found in native .NET code? If not,
can anyone help me out?

Please post the declare for WtsQuerySessionInformation..
 
Here's what I've got...

Private Declare Function WTSQuerySessionInformation Lib
"wtsapi32.dll" _
Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, _
ByVal SessionID As Integer, ByVal WTS_INFO_CLASS As
WTSInfoClass, _
ByRef QSbuffer As Integer, ByRef pCount As Integer) As Long


You may have just konked me in the head to show me where my problem was
- but just in case, I'll ask directly - do you see a problem in the
declaration/usage, perhaps? :-)


(Thanks a ton!)
 
Here's what I've got...

Private Declare Function WTSQuerySessionInformation Lib
"wtsapi32.dll" _
Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, _
ByVal SessionID As Integer, ByVal WTS_INFO_CLASS As
WTSInfoClass, _
ByRef QSbuffer As Integer, ByRef pCount As Integer) As Long


You may have just konked me in the head to show me where my problem was
- but just in case, I'll ask directly - do you see a problem in the
declaration/usage, perhaps? :-)

Yes, actually I do:


Private Declare Auto Function WTSQuerySessionInformation Lib
"wtsapi32" ( _
ByVal hServer As IntPtr, _
ByVal SessionId As Integer, _
ByVal WTSInfoClass As WTS_INFO_CLASS, _
ByRef ppBuffer As IntPtr, _
ByRef pBytesReturned As Integer) As Boolean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top