PDA

Click to See Complete Forum and Search --> : RegisterWindowMessage API and passing strings


reeset
Jul 31st, 2001, 03:01 PM
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



SendMessage HWND_BROADCAST, WM_MYMESSAGE, LENB(cmdline), StrPtr(cmdline)



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



Case WM_MYMESSAGE
Dim a as string
a=Space(wParam)
CopyMemory Byval StrPtr(a), VarPtr(lParam), wParam
msgbox a

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

jim mcnamara
Jul 31st, 2001, 04:54 PM
Here's how to handle reading string pointers



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.

reeset
Aug 1st, 2001, 12:54 AM
jim mcnamara:

Thank you. This was just driving me crazy.