本帖最后由 李岸 于 2014-11-1 17:03 编辑
发送消息:#include <SendMessage.au3>
Global Const $WM_COPYDATA = 0x4A
$Form1 = GUICreate("A窗口")
$button = GUICtrlCreateButton("发送",100,100,100,30)
GUISetState()
While 1
Local $msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $button
Local $hwnd = WinGetHandle("B窗口")
_SendCopyData($hwnd,"发")
EndSwitch
WEnd
Func _SendCopyData($hwnd, $data) ;发送消息
Local $len = BinaryLen(StringToBinary($data))*2
Local $tagCOPYDATASTRUCT = DllStructCreate( _
"DWORD dwData;" & _
"DWORD cbData;" & _
"ptr lpData;")
Local $lpdata = DllStructCreate("wchar lpData["&$len&"]")
DllStructSetData($lpdata,1,$data)
DllStructSetData($tagCOPYDATASTRUCT,1,0)
DllStructSetData($tagCOPYDATASTRUCT,2,$len)
DllStructSetData($tagCOPYDATASTRUCT,3,DllStructGetPtr($lpdata))
_SendMessage($hwnd, $WM_COPYDATA, 0,DllStructGetPtr($tagCOPYDATASTRUCT))
EndFunc ;==>_SendCopyData
接收消息:#include <GUIConstants.au3>
;~ Global Const $WM_COPYDATA = 0x4A
GUICreate('B窗口')
GUISetState()
GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA")
Do
Until GUIGetMsg()=-3
Func WM_COPYDATA($hWnd, $wMsg, $wParam, $wlParam)
Local $SSS = "DWORD dwData;DWORD cbData;ptr lpData;"
Local $struct = DllStructCreate($SSS, $wlParam)
Local $CopyData = DllStructGetData($struct, 3) ;第三个元素是数据的指针
Local $len = DllStructGetData($struct,2)
Local $copy = "wchar str["&$len&"]" ;发送内容的数据结构
Local $data = DllStructCreate($copy, $CopyData)
Local $str = DllStructGetData($data,"str") ;返回的内容
MsgBox(64,"接收到的消息",$str)
EndFunc ;==>WM_COPYDATA
我的疑问是为什么发送消息的脚本中代码: Local $len = BinaryLen(StringToBinary($data))*2
Local $lpdata = DllStructCreate("wchar lpData["&$len&"]")
这句很好理解一个汉字等于两个字节,可是后面的乘以二是什么呢? Local $len = BinaryLen(StringToBinary($data))*2
继续第二句,如果乘以二以后$len等于4,wchar lpdata数据结构的大小就是8字节了,足足可以存放4个汉字了;Local $lpdata = DllStructCreate("wchar lpData["&$len&"]")
正确的写法不应该是这样子吗?可是又无法正常发送..Local $len = BinaryLen(StringToBinary($data))/2
Local $lpdata = DllStructCreate("wchar lpData[" & $len& "]")
求高手赐教,O(∩_∩)O谢谢 |