Results 1 to 3 of 3

Thread: RegisterWindowMessage API and passing strings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316

    RegisterWindowMessage API and passing strings

    I have a quick question. In the General forum, Megatron had made mention of the RegisterWindowMessage API as an option for passing data between two programs. Since I had never used this little nugget, I tried setting up my own message, and then using SendMessage to pass a string variable through the lParam and the string's length through the wParam. In the project, I was able to successfuly create the message, detect the message, and use the SendMessage to pass a pointer to a string, but I am having difficulty reading the value that the pointer addresses. Anyway, here is the message from the post. I realize that it is addressed to Megatron, but I'm looking for anyone that can offer a little light on this subject.

    --Thanks

    Megatron:

    I have a quick question since you suggested it. I've never created my own windows messages before, so I setup up a quick example, and found the message setup very easy. However, I run into problems when I try to pass information, well namely strings, back to the callback. Using the SendMessage API, I call the message, and add length of a string in wparam, and the strptr in lparam

    VB Code:
    1. SendMessage HWND_BROADCAST, WM_MYMESSAGE, LENB(cmdline), StrPtr(cmdline)

    Now in the callback, I try to do the following:

    VB Code:
    1. Case WM_MYMESSAGE
    2.       Dim a as string
    3.       a=Space(wParam)
    4.       CopyMemory Byval StrPtr(a), VarPtr(lParam), wParam
    5.       msgbox a
    6.  
    7. End Select

    The event fires, but the string I'm getting is just random characters, so I'm guessing that the wrong address is being read. Do you have any suggestions. I though about using WM_COPYDATA, but again, I couldn't get the data to format correctly.

    Thanks

  2. #2
    jim mcnamara
    Guest
    Here's how to handle reading string pointers

    Code:
    Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    Private Declare Function lstrlenW Lib "kernel32" (lpString As Any) As Long
    Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal codepage As Long, ByVal dwFlags As Long, lpWideCharStr As Any, ByVal cchWideChar As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
    ' Returns an ANSI string from a pointer to a Unicode string.
    Public Function GetStrFromPtrW(lpszW As Long) As String
        Dim sRtn As String
        sRtn = String$(lstrlenW(ByVal lpszW) * 2, 0)   ' 2 bytes/char
        ' WideCharToMultiByte also returns Unicode string length
        Call WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, ByVal sRtn, Len(sRtn), 0, 0)
        GetStrFromPtrW = GetStrFromBufferA(sRtn)
    End Function
    ' Returns the string before first null char encountered (if any) from an ANSII string.
    Public Function GetStrFromBufferA(sz As String) As String
        If InStr(sz, vbNullChar) Then
            GetStrFromBufferA = Left$(sz, InStr(sz, vbNullChar) - 1)
        Else
            ' If sz had no null char, the Left$ function
            ' above would return a zero length string ("").
            GetStrFromBufferA = sz
        End If
    End Function
    You need to watch out for the type of string (Unicode vs Ansi) that each call is expecting/returning to you.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    jim mcnamara:

    Thank you. This was just driving me crazy.

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