obtain client machine name using Terminal Services API

mmccormick

New member
Joined
Nov 14, 2005
Messages
2
Programming Experience
1-3
I am trying to get the client's machine name when a user logs onto a Terminal Server. I would like to use the Terminal Server api - wtsapi32.dll. I have found several posts on the Internet that use the wtsapi32.dll but they all refer to C# or VB6. When I try to translate them to VB.Net, the application fails. I am certain that I have to declare "wtsapi32.dll" and use the function WTSQuerySessionInformation but I just can't seem to get the correct results. Does anyone have any sample code on how to do this?

thanks in advance,
Mike
 

mmccormick

New member
Joined
Nov 14, 2005
Messages
2
Programming Experience
1-3
After some more digging, I was able to generate the code. Here is the complete code if anyone is interested.

Imports System.Runtime.InteropServices
Module Module1
Declare Auto Function WTSQuerySessionInformation Lib "wtsapi32.dll" ( _
ByVal hServer As Integer, _
ByVal SessionId As Integer, _
ByVal InfoClass As WTS_INFO_CLASS, _
ByRef ppBuffer As IntPtr, _
ByRef pCount As Integer) As Boolean
Declare Sub WTSFreeMemory Lib "wtsapi32.dll" ( _
ByVal pMemory As IntPtr)
Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
WTSClientProtocolType
End Enum
Sub Main()
Getclientname()
End Sub
Sub Getclientname()
Dim myarlist As ArrayList
Dim lpBuffer As IntPtr
Dim count As Integer
Dim machinename As String
Dim clientbuildname, hardwarename, sessionid As String
If WTSQuerySessionInformation(0&, -1, WTS_INFO_CLASS.WTSClientName, lpBuffer, count) Then
machinename = Marshal.PtrToStringAuto(lpBuffer)
WTSFreeMemory(lpBuffer)
End If
Console.WriteLine(machinename)
End Sub

End
Module
 
Top Bottom