Re: Computer Name of Remote Desktop



Ian Williams wrote:
Of course you can

***********************
Option Explicit

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" _
Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, _
ByVal SessionID As Long, ByVal WTSInfoClass As Long, _
ByRef ppBuffer As Long, ByRef lLen As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1

Public Function GetTSClientHostName() As String
Dim sVal As String
Dim lRet As Long
Dim lLen As Long
Dim lErr As Long
Dim I As Long

Dim lBufferAddress As Long

lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
WTS_CURRENT_SESSION, _
10, _
lBufferAddress, _
lLen)

' copying the buffer to a VB string

If lLen > 0 Then
lVal = 0
CopyMemory ByVal sVal, ByVal lBufferAddress, lLen
GetTSClientHostName = sVal
End If


If lRet = 0 Then
lErr = Err.LastDllError
GetTSClientHostName = ""
End If

End Function


If I take a closer look to your implementation, I think you introduce a possible GPF in the CopyMemory line. Reason: you didn't allocate memory for the sVal string. So I think the following improvement will do:
<code>
If lLen > 0 Then
sVal = String(lLen, vbNullChar)
CopyMemory ByVal sVal, ByVal lBufferAddress, lLen
GetTSClientHostName = sVal
End If
</code>

More: I'm also not sure about the CopyMemory call itself, as there are many declarations for the same call (As Any, As Long, ...) all resulting in a different behavior.


Sinna

.


Loading