这个窗口是其他程序的窗口,同样的VB程序。
能正确接收到消息,但却不能正确获得字符串和数字。
但以相同程序发送到自己程序内的窗口却能正确获得字符串和数字。请问通常的处理是怎么解决?
发送端代码:
private const MSG_Error=&H1900
dim ErrStr as string
dim I as long,J as long
ErrStr = "Thello word"
i = VarPtr(ErrStr)
j=41000
SendMessage Me.hwnd, MSG_Error, i, j (如果me.hwnd换成其他程序的窗口HWND则无法正确获得)
接收端代码:
Dim ErrStr2 As String,tempStr As String
Dim tempAddress As Long,tempAddress2 As Long
Dim ErrNumber As Long
If iMsg = MSG_Error Then
tempStr = "大家好!"
tempAddress2 = VarPtr(tempAddress)
Call CopyMemory(tempAddress2, VarPtr(tempStr), 4)
Call CopyMemory(VarPtr(tempStr), wParam, 4)
ErrStr2 = Trim(tempStr)
Call CopyMemory(VarPtr(tempStr), tempAddress2, 4)
Call CopyMemory(ByVal VarPtr(ErrNumber), ByVal lParam, 4) MsgBox "错误号:" & ErrNumber & Chr(13) _
& "错误描述:" & ErrStr2 & Chr(13) _
& "原字符串:" & tempStr
End If
能正确接收到消息,但却不能正确获得字符串和数字。
但以相同程序发送到自己程序内的窗口却能正确获得字符串和数字。请问通常的处理是怎么解决?
发送端代码:
private const MSG_Error=&H1900
dim ErrStr as string
dim I as long,J as long
ErrStr = "Thello word"
i = VarPtr(ErrStr)
j=41000
SendMessage Me.hwnd, MSG_Error, i, j (如果me.hwnd换成其他程序的窗口HWND则无法正确获得)
接收端代码:
Dim ErrStr2 As String,tempStr As String
Dim tempAddress As Long,tempAddress2 As Long
Dim ErrNumber As Long
If iMsg = MSG_Error Then
tempStr = "大家好!"
tempAddress2 = VarPtr(tempAddress)
Call CopyMemory(tempAddress2, VarPtr(tempStr), 4)
Call CopyMemory(VarPtr(tempStr), wParam, 4)
ErrStr2 = Trim(tempStr)
Call CopyMemory(VarPtr(tempStr), tempAddress2, 4)
Call CopyMemory(ByVal VarPtr(ErrNumber), ByVal lParam, 4) MsgBox "错误号:" & ErrNumber & Chr(13) _
& "错误描述:" & ErrStr2 & Chr(13) _
& "原字符串:" & tempStr
End If
相关API:
Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As Any, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
如果不是,可以使用 文件共享内存映射 的方法以上方法都可以在这里轻易搜索到,我就不贴了