Consider this code:
VB Code:
Friend Sub Load() On Error GoTo ERR_Load Dim n As Long Dim i As Long Dim Ret As Long Dim pBuffer As Long Dim SessionInfo() As WTS_SESSION_INFO Dim Session As CSession '************************************************* '* Enumerate sessions on the terminal server . . . '************************************************* Ret = WTSEnumerateSessionsW(mhServer, 0&, 1, pBuffer, n) '********************************************************** '* If success then load up the UDT, else raise error . . . '********************************************************** If Not (Ret = 0) Then ReDim SessionInfo(n - 1) CopyMemory SessionInfo(0), ByVal pBuffer, n * LenB(SessionInfo(0)) WTSFreeMemory pBuffer Else Err.Raise Err.LastDllError, , ErrorMessage(Err.LastDllError) End If '*********************************************** '* Load up the minimal session information . . . '*********************************************** For i = 0 To n - 1 Set Session = New CSession With Session .SessionId = SessionInfo(i).SessionId .WinStationName = StrConv(SysAllocString(SessionInfo(i).pWinStationName), vbFromUnicode) .ConnectState = SessionInfo(i).State End With Me.Add Session Next Exit Sub ERR_Load: Err.Raise Err.Number, "WTSModel.CSessions.Load", Err.Description, Err.HelpFile, Err.HelpContext End Sub
It's this linethat I'm having problems with.VB Code:
.WinStationName = StrConv(SysAllocString(SessionInfo(i).pWinStationName), vbFromUnicode)
The array of UDT's is successfuly copied: ie all the other bits are OK. The pWinStation variable is a LPWSTR. If I use SysAllocString (the 'right' way to do this?) then it almost correctly resolves the pointer creating, and copying the string.
However it appears as if something is expecting the string to be ANSI because when the string is resolved it looks something like this 'C O N S O L E' rather than 'CONSOLE' This is why I'm having to use Strconv to convert back from the double UNICODE conversion.
The declaration for SysAllocString isI suspect that VB misinterprets the return BSTR from this declare. The only problem is I don't understand why, so I can't figure out how to get around this.VB Code:
Public Declare Function SysAllocString Lib "oleaut32" (ByVal pUnicodeString As Long) As String
Any ideas?




Reply With Quote