Sign in to follow this  
Followers 0
Markos

Inter-process communication (WM_MSG)

6 posts in this topic

I want to my scripts to comunicate.

First I tried Windows variable (envset) but it didnt work as I expected, then I tried TCP method to comunicate.

That one worked but I want to use little bit more "pro" method to communicate so I tried to use wm_messages.

I found out how to send a simple msg (number) from 1 process to another, now I need to send a string or possibly an array.

here is what I got so far:

;receiver, start 1st
#include <winapi.au3>

GUICreate('Msg_Catcher')
$iMsg = _WinAPI_RegisterWindowMessage('my_message')
GUIRegisterMsg($iMsg, '_Func')

While 1
    Sleep(1000)
WEnd

Func _Func($hWnd, $iMsg, $iwParam, $ilParam)
    MsgBox(0, Default,'It worked:' & @CRLF & $hWnd & @CRLF & $iMsg & @CRLF & $iwParam & @CRLF & $ilParam)
EndFunc

;sender
#include <winapi.au3>
#include <string.au3>

$str = StringToBinary('my test message')
$str2 = _StringToHex('my test message') ;tried to convert string to int but it didn't work


$HWND_BROADCAST = 0xFFFF
$iMsg = _WinAPI_RegisterWindowMessage('my_message')

_WinAPI_PostMessage($HWND_BROADCAST, $iMsg, $str2, $str2)
;~ _SendMessage($HWND_BROADCAST, $iMsg, $str, $str2)

;both postmessage and sendmessage work

I will appreciate any help from anybody that will help me to improve my code


Share this post


Link to post
Share on other sites



OK, so I am now using structures as you used in your script. This is something new for me.

If I have pointer to a struct, how do I retrieve the structs value?

simply calling DllStructGetData($ptr, 1) does not work.

Here is what I have:

;receiver
#include <winapi.au3>

GUICreate('Msg_Catcher')
$iMsg = _WinAPI_RegisterWindowMessage('my_message')
GUIRegisterMsg($iMsg, '_Func')

While 1
    Sleep(1000)
WEnd

Func _Func($hWnd, $iMsg, $iwParam, $ilParam)
    MsgBox(0, Default,'It worked:' & @CRLF & $hWnd & @CRLF & $iMsg & @CRLF & $iwParam & @CRLF & $ilParam)

;~  $iwParam = pointer to my struct
    $str = DllStructGetData($iwParam, 1)    ;this one does not return my value, why?
    MsgBox(0, '', $str)
EndFunc

;sender
#include <winapi.au3>

$message = 'this is my test message'

$struct = DllStructCreate('char[256]')
DllStructSetData($Struct,1,$message)

$pStruct = DllStructGetPtr($struct)     ;get pointer to my struct

$HWND_BROADCAST = 0xFFFF
$iMsg = _WinAPI_RegisterWindowMessage('my_message')

_SendMessage($HWND_BROADCAST, $iMsg, $pStruct, $pStruct)    ;send the pointer to the second script

Share this post


Link to post
Share on other sites

Go back to my other post.. martin replied on my thread with a question and answer about reading the struct in another process you have to use the WM_COPYDATA for the msgID so windows knows to make a copy

Share this post


Link to post
Share on other sites

With wm_copydata it is actually working, but the problem is that I have to send the message to specific window handle.

My original idea was to send to all windows thru wm_broadcast (0xFFFF) so more receiver scripts would be possible.

If I do that now, some other windows receive that message as well and cause some problems.

Thats why I want to use my own window message using _WinAPI_RegisterWindowMessage('my_message')

so only my window(s) would be able to accept the message.

Is possible to do that?

;sender
#include <winapi.au3>

$hwnd = WinGetHandle('Msg_Catcher')
$message = 'this is my test message'

$struct = DllStructCreate('char var1[24]')  ;my data struct
DllStructSetData($Struct,1,$message)
$pStruct = DllStructGetPtr($struct)


$struct2 = DllStructCreate('dword;dword;ptr');my copydata struct
DllStructSetData($struct2,1,0)
DllStructSetData($struct2,2,24)
DllStructSetData($struct2,3,$pStruct)
$pStruct2 = DllStructGetPtr($struct2)


;~ $iMsg = _WinAPI_RegisterWindowMessage('my_message')
;~ _SendMessage(0xFFFF, $iMsg, 0, $pStruct2)

_SendMessage($hwnd, 0x4A, 0, $pStruct2)

;receiver
#include <winapi.au3>

GUICreate('Msg_Catcher')

;~ $iMsg = _WinAPI_RegisterWindowMessage('my_message')
;~ GUIRegisterMsg($iMsg, '_Func')

GUIRegisterMsg(0x4A, '_Func')

While 1
    Sleep(1000)
WEnd

Func _Func($hWnd, $iMsg, $iwParam, $ilParam)
    MsgBox(0, Default,'It worked:' & @CRLF & $hWnd & @CRLF & $iMsg & @CRLF & $iwParam & @CRLF & $ilParam)

    $a = DllStructCreate('dword var1;dword var2;ptr var3', $ilParam)
    $b = DllStructGetData($a, 3)
    
    $c = DllStructCreate('char var1[24]',$b)
    $d = DllStructGetData($c, 1)
    
    MsgBox(0, 'receiver', $d)
EndFunc

Share this post


Link to post
Share on other sites

With wm_copydata it is actually working, but the problem is that I have to send the message to specific window handle.

My original idea was to send to all windows thru wm_broadcast (0xFFFF) so more receiver scripts would be possible.

If I do that now, some other windows receive that message as well and cause some problems.

Thats why I want to use my own window message using _WinAPI_RegisterWindowMessage('my_message')

so only my window(s) would be able to accept the message.

Is possible to do that?

;sender
#include <winapi.au3>

$hwnd = WinGetHandle('Msg_Catcher')
$message = 'this is my test message'

$struct = DllStructCreate('char var1[24]');my data struct
DllStructSetData($Struct,1,$message)
$pStruct = DllStructGetPtr($struct)


$struct2 = DllStructCreate('dword;dword;ptr');my copydata struct
DllStructSetData($struct2,1,0)
DllStructSetData($struct2,2,24)
DllStructSetData($struct2,3,$pStruct)
$pStruct2 = DllStructGetPtr($struct2)


;~ $iMsg = _WinAPI_RegisterWindowMessage('my_message')
;~ _SendMessage(0xFFFF, $iMsg, 0, $pStruct2)

_SendMessage($hwnd, 0x4A, 0, $pStruct2)

;receiver
#include <winapi.au3>

GUICreate('Msg_Catcher')

;~ $iMsg = _WinAPI_RegisterWindowMessage('my_message')
;~ GUIRegisterMsg($iMsg, '_Func')

GUIRegisterMsg(0x4A, '_Func')

While 1
    Sleep(1000)
WEnd

Func _Func($hWnd, $iMsg, $iwParam, $ilParam)
    MsgBox(0, Default,'It worked:' & @CRLF & $hWnd & @CRLF & $iMsg & @CRLF & $iwParam & @CRLF & $ilParam)

    $a = DllStructCreate('dword var1;dword var2;ptr var3', $ilParam)
    $b = DllStructGetData($a, 3)
    
    $c = DllStructCreate('char var1[24]',$b)
    $d = DllStructGetData($c, 1)
    
    MsgBox(0, 'receiver', $d)
EndFunc
As ChrisL said, you need to use WM_COPYDATA but you don't have to only use that. You can use _WinAPI_RegisterWindowMessage('my_message') and with that message number find the handle of the other window.

program 1 does

$iMsg = _WinAPI_RegisterWindowMessage('my_message')
do
$hWndProg2 = _SendMessage($HWND_BROADCAST, $iMsg, 100)
sleep(200)
until $hWndProg2 <> 0

;now we can send data using WM_COPYDATA to the correct window.

Program 2 does

$iMsg = _WinAPI_RegisterWindowMessage('my_message')

GUIRegisterMsg($iMsg, '_Func1')

Func _Func1($hW,$iMsg,$wparam,$lparam)
 if $wparam = 100;we are being asked for our window handle
  return $hWndforOurWindow
 endif
endfunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0