|
-
Aug 10th, 2005, 03:54 AM
#1
Thread Starter
Frenzied Member
[Resolved] Unicode Pointer Problem
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 line
VB Code:
.WinStationName = StrConv(SysAllocString(SessionInfo(i).pWinStationName), vbFromUnicode)
that I'm having problems with.
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 is
VB Code:
Public Declare Function SysAllocString Lib "oleaut32" (ByVal pUnicodeString As Long) As String
I 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.
Any ideas?
Last edited by yrwyddfa; Aug 10th, 2005 at 04:45 AM.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 10th, 2005, 04:44 AM
#2
Thread Starter
Frenzied Member
Re: Unicode Pointer Problem
Solved it.
If you declare the SysAllocString in a TLB using IDL VB doesn't reinterpret the string on the function stack for you. You don't then need to use StrConv.
Yuk.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 12th, 2005, 08:49 AM
#3
Thread Starter
Frenzied Member
Re: [Resolved] Unicode Pointer Problem
For anyone's information the IDL looks like this:
VB Code:
[
uuid(842b47f0-0b27-11da-8cd6-0800200c9a66),
version(0.1),
helpstring("Win32 API OLEAUT32.DLL")
]
library Win32Stuff
{
importlib("stdole2.tlb");
[dllname("oleaut32.dll")]
module oleaut32
{
[
entry("SysAllocString")
]
BSTR SysAllocString ([in] BSTR*lpBSTR);
}
}
and was compiled using MkTypLib (provided with the platform SDK)
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|