|
|
![]() |
|
||
|
|
|||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Visual Basic System Services WTSQuerySessionInformation: Obtaining the Name of a Computer running a Remote Desktop Session |
||
| Posted: | Wednesday September 10, 2008 | |
| Updated: | Monday December 26, 2011 | |
| Applies to: | VB4-32, VB5, VB6 | |
| Developed with: | VB6, Windows Vista | |
| OS restrictions: | OS Supporting Terminal Service "Remote Desktop" Connection | |
| Author: | Ian Williams, VBnet - Randy Birch | |
| Prerequisites |
| None. |
|
|
This
code allows a remote desktop session to identify the name of the machine
on which the remote desktop session is being run.For example, my home machine is named 'blackbird'; I start a remote desktop session connecting to a Windows 2003 server box and run the code below in that session. The value returned is my machine name. This code is based on a posting to the msnews winapi newsgroup by Ian Williams. |
| BAS Module Code |
| None: |
|
|
| Form Code |
|
|
| To create this project, add a command button (Command1) and a label (Label1) together with the following code: |
|
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const WTSClientName As Long = 10
Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" _
Alias "WTSQuerySessionInformationA" _
(ByVal hServer As Long, _
ByVal SessionID As Long, _
ByVal WTSInfoClass As Long, _
ppBuffer As Long, _
pBytesReturned As Long) As Long
Private Declare Function lstrcpyA Lib "kernel32" _
(ByVal RetVal As String, ByVal ptr As Long) As Long
Private Declare Function lstrlenA Lib "kernel32" _
(ByVal ptr As Any) As Long
Private Sub Command1_Click()
Label1.Caption = "This Terminal Session is running on computer " & GetTSClientHostName()
End Sub
Private Function GetTSClientHostName() As String
Dim bufptr As Long
Dim dwBufLen As Long
If WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
WTS_CURRENT_SESSION, _
WTSClientName, _
bufptr, _
dwBufLen) > 0 Then
GetTSClientHostName = GetStrFromPtrA(bufptr)
End If
End Function
Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function |
| Comments |
| Note that to test this code it must be run (IDE or compiled) on a terminal session. Running in on a non-TS session will return an empty string. |
|
|
|
|
|
|||||
|
|||||
|
|
|||||
|
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |