#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=ipc_sender.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include #include #include #include #include #include #include #include #include #include #include ; Author : Grosminet Global Const $FunctionIdentifier = 314145 Global Const $sAPP_me = "ipc_sender" Global Const $sAPP_other = "ipc_receiver" Global Const $tagCOPYDATASTRUCT = "struct;ulong_ptr dwData;dword cbData;ptr lpData;endstruct" Global $MyCDS = DllStructCreate($tagCOPYDATASTRUCT) Global $OwnWinHandle ; Create callback function. Global $guiw = 1000, $guih = 300, $guix = (@desktopwidth - $guiw - 50), $guiy = $guih + 150, $sp = 10, $x = $sp, $y = $sp, $w, $hbut = 28, $h Global $hParentGUI, $hSendBut, $hlocalPID, $hSendEdit, $hRecEdit Global $debug = true, $info, $PIDAppMe, $hOtherProcess ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo $hParentGui = GUICreate($sAPP_me, $guiw, $guih, $guix, $guiy) $w = ($guiw - 3*$sp) / 4 $h = ($guih - 3* $sp) / 2 $hSendBut = GUICtrlCreateButton("Send data", $x, $y, $w, $hbut) $y += $hbut + $sp $hlocalPID = GUIctrlCreateLabel("PID=", $x, $y, $w, $h) $x += $w + $sp $y = $sp $hSendEdit = GUIctrlCreateEdit("abcde", $x, $y, 3* $w, $h) $x = $sp $y += $h + $sp $hRecEdit = GUIctrlCreateEdit("", $x, $y, 4* $w, $h) GUICtrlSetFont(-1, 9, $FW_NORMAL, Default, "Courier New") ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo GUISetState(@SW_SHOW, $hParentGui) GUICtrlSetData($hlocalPID, "PID= " & @AutoItPID) $OwnWinHandle = WinGetHandle(@AutoItPID) ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo ; Get the RECEIVER application 'pid' ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo $PIDAppOther = WinGetProcess($sAPP_other) if $PIDAppOther = -1 then MsgBox($MB_SYSTEMMODAL, "ERROR", "Unable to know receiver PID " & $sAPP_other & " --> error= " & @error) exit Endif ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo ; Get the RECEIVER application 'process handle' ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Opt("WinTitleMatchMode", 1) $hOtherProcess = WinGetHandle($sAPP_other) if @error then MsgBox($MB_SYSTEMMODAL, "ERROR", "Unable to retrieve handle of " & $sAPP_other & ", error= " & @error) exit endif $info = " Receiver application --> " & $sAPP_other & " - PID= " & $PIDAppOther & " - Handle= " & $hOtherProcess & @crlf _ShowInfo($info) #cs ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo ; Get the RECEIVER application 'process handle' ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo $hOtherProcess = _WinAPI_OpenProcess($PROCESS_VM_READ, False, $PIDAppOther) if @error Then $info = "_WinAPI_OpenProcess() error: " & @error & @crlf _ShowInfo($info) exit endif $info = " Receiver application --> " & $sAPP_other & " - PID= " & $PIDAppOther & " - Handle= " & $hOtherProcess & @crlf _ShowInfo($info) #ce ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE _myExit() Case $hSendBut _SendDATA_to_X() EndSwitch WEnd ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Func _SendDATA_to_X() Local $sValue_To_Send = GUICtrlRead($hSendEdit) Local $iStringSize = StringLen($sValue_To_Send) + 1 local $TagInfoStruct = "struct;wchar buf[" & $iStringSize & "];endstruct" Local $tValue_To_Send = DllStructCreate($TagInfoStruct) DllStructSetData($tValue_To_Send, "buf", $sValue_To_Send) ; Local $pValue_To_Send = DllStructGetPtr($tValue_To_Send) Local $iSizeStruct = DllStructGetSize($tValue_To_Send) $info = '_SendDATA_to_X(): Pointer to text= ' & $pValue_To_Send & " - Size of text= " & $iStringSize & " - Size of structure= " & $iSizeStruct & @CRLF _ShowInfo($info) $info = _DumpStruct($pValue_To_Send, $iSizeStruct) _ShowInfo($info) ; ; ; Fill the COPYDATA structure ; ; Global Const $tagCOPYDATASTRUCT = "struct;ulong_ptr dwData;dword cbData;ptr lpData;endstruct" DllStructSetData($MyCDS, "dwData", $sValue_To_Send);$FunctionIdentifier) DllStructSetData($MyCDS, "cbData", $iSizeStruct) DllStructSetData($MyCDS, "lpData", $pValue_To_Send) local $pMyCDS = DllStructGetPtr($MyCDS) ; local $ret = _SendMessage($hOtherProcess, $WM_COPYDATA, $OwnWinHandle, $pMyCDS) If @error Then MsgBox($MB_SYSTEMMODAL, "ERROR", "_SendDATA_to_X(): " & $sAPP_me & " --> _SendMessage error= " & _WinAPI_GetLastErrorMessage()) else Local $sData_Sent = StringLeft(DllStructGetData($tValue_To_Send, "buf"), $iStringSize) $info = '................: --> Data sent = ' & $sData_Sent & @CRLF _ShowInfo($info) endif $pValue_To_Send = 0 $tValue_To_Send = 0 EndFunc ;==>_SendDATA_to_X ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Func _ShowInfo($msg) if $debug then ConsoleWrite($msg) GUICtrlSetData($hRecEdit, GUICtrlRead($hRecEdit) & $msg) Local $iEnd = StringLen(GUICtrlRead($hRecEdit)) _GUICtrlEdit_SetSel($hRecEdit, $iEnd, $iEnd) _GUICtrlEdit_Scroll($hRecEdit, $SB_SCROLLCARET) Endfunc ; _ShowInfo ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Func _myExit() GUIDelete() exit Endfunc ; _myExit ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Func _DumpStruct($p_STRUCT, $iSizeStruct) ; $iSizeStruct = the size of the struct in bytes (DllStructGetSize) Local $TagStructDump = "struct;align;byte[" & $iSizeStruct & "];endstruct" Local $t_Struct = DllStructCreate($TagStructDump, $p_STRUCT) Local $i Local $structInfo = "" _ConsoleWriteInfo($structInfo, "Structure size: " & $iSizeStruct & " byte(s):" & @crlf) for $i = 0 to $iSizeStruct - 1 _ConsoleWriteInfo($structInfo, hex(DllStructGetData($t_Struct, 1, $i), 2) & " ") if (Mod($i+1, 8) = 0) then _ConsoleWriteInfo($structInfo, @CRLF) Endif Next _ConsoleWriteInfo($structInfo, @CRLF) return $structInfo EndFunc ; _DumpStruct ; ooooooooooooooooooooooooooooooooooooooooooooooooooooooo Func _ConsoleWriteInfo(ByRef $msg, $txt) $msg &= $txt EndFunc ; _ConsoleWriteInfo #cs // ************ Globals ************ // #define MYDISPLAY 1 typedef struct tagMYREC { char s1[80]; char s2[80]; DWORD n; } MYREC; COPYDATASTRUCT MyCDS; MYREC MyRec; HRESULT hResult; BOOL CALLBACK InfoDlgProc( HWND, UINT, WPARAM, LPARAM ); // ************ Code fragment **************** // Get data from user. InfoDlgProc stores the information in MyRec. // DialogBox( ghInstance, "InfoDlg", hWnd, (DLGPROC) InfoDlgProc ); // // Copy data into structure to be passed via WM_COPYDATA. // Also, we assume that truncation of the data is acceptable. // hResult = StringCbCopy( MyRec.s1, sizeof(MyRec.s1), szFirstName ); if (hResult != S_OK) return False; hResult = StringCbCopy( MyRec.s2, sizeof(MyRec.s2), szLastName ); if (hResult != S_OK) return False; MyRec.n = nAge; // // Fill the COPYDATA structure // MyCDS.dwData = MYDISPLAY; // function identifier MyCDS.cbData = sizeof( MyRec ); // size of data MyCDS.lpData = &MyRec; // data structure // // Call function, passing data in &MyCDS // hwDispatch = FindWindow( "Disp32Class", "Hidden Window" ); if( hwDispatch != NULL ) SendMessage( hwDispatch, WM_COPYDATA, (WPARAM)(HWND) hWnd, (LPARAM) (LPVOID) &MyCDS ); else MessageBox( hWnd, "Can't send WM_COPYDATA", "MyApp", MB_OK ); #ce