本帖最后由 republican 于 2010-9-18 22:33 编辑
整个例子是用WM_COPYDATA传输数据,不过只能传递char,单独修改为wchar后会有小问题。
这么做的目的是为了传递中文字符,而我对DllStruct之类的没什么研究,希望得到大家的帮忙,谢谢!
Parent.au3#include <WindowsConstants.au3>
Global $hParent, $hChild, $received
; parent gui
$hParent = GUICreate("Parent", 300, 120, 100)
$input_send = GUICtrlCreateInput("", 10, 10, 280, 20)
$btn = GUICtrlCreateButton("SEND TO CHILD", 80, 35, 140, 20, 0x0001)
GUICtrlCreateLabel("RECEIVED FROM CHILD", 10, 75, 280, 20, 0x01)
$input_received = GUICtrlCreateInput("", 10, 90, 280, 20, 0x0800)
GUISetState(@SW_SHOW)
; launch child - trade handles
GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA_ReceiveData") ; register WM_COPYDATA
ShellExecute("child.exe", $hParent) ; start child process - send parent handle
While Not $received ; wait for child process to return it's handle
Sleep(50)
WEnd
$hChild = HWnd($received); assign child hamdle
$received = ""
; main loop
While 1
$msg = GUIGetMsg()
If $msg = -3 Then ExitLoop
If $msg = $btn Then WM_COPYDATA_SendData($hChild, GUICtrlRead($input_send)) ; send to child
If $received Then
GUICtrlSetData($input_received, $received) ; receive form child
$received = ""
EndIf
WEnd
ProcessClose("child.exe")
Exit
;===================================================================================================================================
Func WM_COPYDATA_ReceiveData($hWnd, $MsgID, $wParam, $lParam) ;
Local $tCOPYDATA = DllStructCreate("dword;dword;ptr", $lParam)
Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3))
$received = DllStructGetData($tMsg, 1)
EndFunc
Func WM_COPYDATA_SendData($hWnd, $sData)
Local $tCOPYDATA = DllStructCreate("dword;dword;ptr")
Local $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]")
DllStructSetData($tMsg, 1, $sData)
DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1)
DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg))
$Ret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA))
EndFunc
Child.au3#NoTrayIcon
#include <WindowsConstants.au3>
Global $hParent, $hChild, $received
Global $hParent = HWnd($CmdLine[1]) ; get parent handle from commandline
; child gui
$hChild = GUICreate("Child", 300, 150, 440, Default, 0x00800000)
$input_send = GUICtrlCreateInput("", 10, 10, 280, 20)
$btn = GUICtrlCreateButton("SEND TO PARENT", 80, 35, 140, 20, 0x0001)
GUICtrlCreateLabel("RECEIVED FROM PARENT", 10, 75, 280, 20, 0x01)
$input_received = GUICtrlCreateInput("", 10, 90, 280, 20, 0x0800)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA_ReceiveData") ; register WM_COPYDATA
WM_COPYDATA_SendData($hParent, $hChild) ; return child handle to parent
; main loop
While 1
$msg = GUIGetMsg()
If $msg = $btn Then WM_COPYDATA_SendData($hParent, GUICtrlRead($input_send)) ; send to parent
If $received Then
GUICtrlSetData($input_received, $received) ; receive from parent
$received = ""
EndIf
WEnd
Exit
;===================================================================================================================================
Func WM_COPYDATA_ReceiveData($hWnd, $MsgID, $wParam, $lParam) ;
Local $tCOPYDATA = DllStructCreate("dword;dword;ptr", $lParam)
Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3))
$received = DllStructGetData($tMsg, 1)
EndFunc
Func WM_COPYDATA_SendData($hWnd, $sData)
Local $tCOPYDATA = DllStructCreate("dword;dword;ptr")
Local $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]")
DllStructSetData($tMsg, 1, $sData)
DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1)
DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg))
$Ret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA))
EndFunc 代码打包:
|