Results 1 to 3 of 3

Thread: [Resolved] Unicode Pointer Problem

  1. #1

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Resolved [Resolved] Unicode Pointer Problem

    Consider this code:
    VB Code:
    1. Friend Sub Load()
    2.  
    3.     On Error GoTo ERR_Load
    4.    
    5.     Dim n As Long
    6.     Dim i As Long
    7.     Dim Ret As Long
    8.     Dim pBuffer As Long
    9.     Dim SessionInfo() As WTS_SESSION_INFO
    10.     Dim Session As CSession
    11.    
    12.     '*************************************************
    13.     '* Enumerate sessions on the terminal server . . .
    14.     '*************************************************
    15.     Ret = WTSEnumerateSessionsW(mhServer, 0&, 1, pBuffer, n)
    16.    
    17.     '**********************************************************
    18.     '* If success then load up the UDT, else raise error . . .
    19.     '**********************************************************
    20.     If Not (Ret = 0) Then
    21.         ReDim SessionInfo(n - 1)
    22.         CopyMemory SessionInfo(0), ByVal pBuffer, n * LenB(SessionInfo(0))
    23.         WTSFreeMemory pBuffer
    24.     Else
    25.         Err.Raise Err.LastDllError, , ErrorMessage(Err.LastDllError)
    26.     End If
    27.    
    28.     '***********************************************
    29.     '* Load up the minimal session information . . .
    30.     '***********************************************
    31.     For i = 0 To n - 1
    32.         Set Session = New CSession
    33.         With Session
    34.             .SessionId = SessionInfo(i).SessionId
    35.             .WinStationName = StrConv(SysAllocString(SessionInfo(i).pWinStationName), vbFromUnicode)
    36.             .ConnectState = SessionInfo(i).State
    37.         End With
    38.         Me.Add Session
    39.     Next
    40.    
    41.     Exit Sub
    42.    
    43. ERR_Load:
    44.     Err.Raise Err.Number, "WTSModel.CSessions.Load", Err.Description, Err.HelpFile, Err.HelpContext
    45. End Sub

    It's this line
    VB Code:
    1. .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:
    1. 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

  2. #2

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    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

  3. #3

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: [Resolved] Unicode Pointer Problem

    For anyone's information the IDL looks like this:

    VB Code:
    1. [
    2.     uuid(842b47f0-0b27-11da-8cd6-0800200c9a66),
    3.     version(0.1),
    4.     helpstring("Win32 API OLEAUT32.DLL")
    5. ]
    6. library Win32Stuff
    7. {
    8.     importlib("stdole2.tlb");
    9.  
    10.     [dllname("oleaut32.dll")]
    11.  
    12.     module oleaut32
    13.     {
    14.         [
    15.             entry("SysAllocString")
    16.         ]
    17.         BSTR SysAllocString ([in] BSTR*lpBSTR);
    18.     }
    19. }

    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
  •  



Click Here to Expand Forum to Full Width