Sign in to follow this  
Followers 0
erezlevi

Win API send a message to an application

12 posts in this topic

hi,

does anyone knows how can i send a message and see if an application gets it ?

I am using the:

_WinAPI_PostMessage ($HWND_BROADCAST,"erez levi","","")

I want to send the "erez levi" string to all windows applications, but how can I test it?

is there a _winapi_getmessage because i can't see that.

thanks,

Share this post


Link to post
Share on other sites



hi,

does anyone knows how can i send a message and see if an application gets it ?

I am using the:

_WinAPI_PostMessage ($HWND_BROADCAST,"erez levi","","")

I want to send the "erez levi" string to all windows applications, but how can I test it?

is there a _winapi_getmessage because i can't see that.

thanks,

Are you doing this to learn about messages?


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

Are you doing this to learn about messages?

first yes, I am trying to understand how AutoIT works with windows messages, and to try to send a message using either _sendMessage or _WinAPI_PostMessage to all application so a trigger will eccur.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

first yes, I am trying to understand how AutoIT works with windows messages, and to try to send a message using either _sendMessage or _WinAPI_PostMessage to all application so a trigger will eccur.

Ok, I learnt to use messages in AutoIt by playing around too.

My advice would be to write a simple script, say script B which just sends a message to script A.

First thing to know, is that I don't think an application can receive a message unless it has a window. This I think is the reason that many apps have hidden windows that never get used. So I assume that the message handling code is part of the window structure.

Script A example, compile this and run it

#include <constants.au3>

$g = guicreate("123abc")
guisetstate()
$lab = guictrlcreatelabel("nothing yet",20,20,200,21)
GUIRegisterMsg($WM_USER + 300,"trythis")

while 1
 if guigetmsg() = -3 then exit
wend

func trythis($hWndGUI, $MsgID, $WParam, $LParam)
guictrlsetdata($lab,"Wp = " & $WParam & ", Lp = " & $LParam)
return $LParam + $WParam; just to show a return value
endfunc

Script B. Run this from Scite say

#include <constants.au3>
#Include <Misc.au3>

$n = 1
$p = 1
while 1
 $n = _sendmessage(WinGetHandle("123abc"),$WM_USER + 300,$n,$p)
Sleep(100)
WEnd
Edited by martin

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

Ok, I learnt to use messages in AutoIt by playing around too.

My advice would be to write a simple script, say script B which just sends a message to script A.

First thing to know, is that I don't think an application can receive a message unless it has a window. This I think is the reason that many apps have hidden windows that never get used. So I assume that the message handling code is part of the window structure.

Script A example, compile this and run it

#include <constants.au3>

$g = guicreate("123abc")
guisetstate()
$lab = guictrlcreatelabel("nothing yet",20,20,200,21)
GUIRegisterMsg($WM_USER + 300,"trythis")

while 1
 if guigetmsg() = -3 then exit
wend

func trythis($hWndGUI, $MsgID, $WParam, $LParam)
guictrlsetdata($lab,"Wp = " & $WParam & ", Lp = " & $LParam)
return $LParam + $WParam; just to show a return value
endfunc

Script B. Run this from Scite say

#include <constants.au3>
#Include <Misc.au3>

$n = 1
$p = 1
while 1
 $n = _sendmessage(WinGetHandle("123abc"),$WM_USER + 300,$n,$p)
Sleep(100)
WEnd

Hi, first thanks alot. you are right about the fact of the window, the window havean handler:

magicHwnd. but, the script you sent me is to compicated (at least for me), can you post an example of a simple message like a string to send to magicHwnd process?

Share this post


Link to post
Share on other sites

Hi, first thanks alot. you are right about the fact of the window, the window havean handler:

magicHwnd. but, the script you sent me is to compicated (at least for me), can you post an example of a simple message like a string to send to magicHwnd process?

I can't because I have no idea what magicHwnd is.

To send a message to an application you have to know what messages it can receive and what information it expects to get in the message.

Messages only send numbers. The example I gave is about as simple as I could think of.

If you want to send a string you have to send the address of the string, and maybe the length of the string so it's more complicated.

But you can't just send a message to an application and expect it to do something with it. In the example I gave, the script was set to respond to $WM_USER + 300 and what to do with it was written in the script. If you change the number to 301 in one script but not the other then nothing will happen. No-one else would know, without being told, how to send a message to that script.


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

I can't because I have no idea what magicHwnd is.

To send a message to an application you have to know what messages it can receive and what information it expects to get in the message.

Messages only send numbers. The example I gave is about as simple as I could think of.

If you want to send a string you have to send the address of the string, and maybe the length of the string so it's more complicated.

But you can't just send a message to an application and expect it to do something with it. In the example I gave, the script was set to respond to $WM_USER + 300 and what to do with it was written in the script. If you change the number to 301 in one script but not the other then nothing will happen. No-one else would know, without being told, how to send a message to that script.

hi, thanks for your answers they really helped me. does sending a string invloving using an Array? and does the sendmessage shows the other application where to find the string in memory?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

hi, thanks for your answers they really helped me. does sending a string invloving using an Array? and does the sendmessage shows the other application where to find the string in memory?

Here's a string sending example.

You need NomadMemory.au3 from here.

You might have to comment out some Consts depending o the version of AutoIt you have.

Compile this one to exmess2.exe

#include <constants.au3>
#include <guiconstants.au3>
#include <nomadmemory.au3>
Global $iPID
$g = guicreate("123abc")
guisetstate()
$lab = guictrlcreatelabel("nothing yet",20,20,200,21)
GUIRegisterMsg($WM_USER + 300,"trythis")

while 1
    if guigetmsg() = -3 then exit
wend

func trythis($hWndGUI, $MsgID, $WParam, $LParam)
    switch $WParam
        case 20;we are being told the PID of the sender
            $iPID = Number($LParam);needed for the memory functions

        Case 80;we are being sent info on a string
            Local $MemAdd = $LParam
            Local $DBMem = _MemoryOpen($iPID)
            If $DBmem = 0 Then
                MsgBox(0,'pid =' & $iPID, 'Could not open the memory for Sender!')
                Return
            endif
            
    ;the memory address is a ptr to a struct int[2]
    ;first int is ptr to address of the string
    ;second int is length of string
            Local $StrAdd = _MemoryRead ($MemAdd,$DBMem,"int")
            $StrLen = _MemoryRead (AddrCalc($MemAdd,4),$DBMem,"int")
            guictrlsetdata($lab,_MemoryRead (AddrCalc($StrAdd,0),$DBMem,"char[" & $StrLen & "]"))
            _MemoryClose($DBMem)
    EndSwitch


endfunc


Func AddrCalc($StartAddr, $Offset)
    Return '0x' & Hex(number($StartAddr) + $Offset, 8)
EndFunc

Then run this one

#include <constants.au3>
#Include <Misc.au3>
$string = "It's nearly Christmas"
Run("exmess2.exe")
Sleep(2000)
;first we tell the other program what our PID is. It needs this to read our memory and this is an easy way for it to get the pid
_SendMessage(WinGetHandle("123abc"),$WM_USER + 300,20,@AutoItPID)
while $string <> ''
    Local $tempstruct = DllStructCreate('char[' & StringLen($string) + 1 & ']')
    
    DllStructSetData($tempstruct,1,$string)
    Local $MsgStruct = DllStructCreate("int;int")
    DllStructSetData($MsgStruct,1,DllStructGetPtr($tempstruct))
    DllStructSetData($MsgStruct,2,StringLen($string)+ 1)
    $n = _sendmessage(WinGetHandle("123abc"),$WM_USER + 300,80,DllStructGetPtr($MsgStruct))
    $string = InputBox('example string send','Enter the string to send')
WEnd

BTW, apologies for messy and inconsistent code, just done in a rush (story of my life).

Edited by martin

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

Script A :

#include <Constants.au3>
GUICreate("Test123")
GUISetState()
$Label = GUICtrlCreateLabel("Test", 20, 20, 200, 20)
GUIRegisterMsg($WM_USER+350, "Test")

Do
Until GUIGetMsg() = -3

Func Test($hWndGUI, $MsgID, $Param)
    GUICtrlSetData($Label, $Param)
EndFuncoÝ÷ Ù'+Ajëh×6#include <Constants.au3>
#Include <Misc.au3>
$i = 1
While 1
    _SendMessage(WinGetHandle("Test123"), $WM_USER+350, "Test " & $i)
    $i += 1
    Sleep(100)
WEnd

Why i cant send string? Script B send only that : "0x00000000"

Share this post


Link to post
Share on other sites

Script A :

#include <Constants.au3>
GUICreate("Test123")
GUISetState()
$Label = GUICtrlCreateLabel("Test", 20, 20, 200, 20)
GUIRegisterMsg($WM_USER+350, "Test")

Do
Until GUIGetMsg() = -3

Func Test($hWndGUI, $MsgID, $Param)
    GUICtrlSetData($Label, $Param)
EndFuncoÝ÷ Ù'+Ajëh×6#include <Constants.au3>
#Include <Misc.au3>
$i = 1
While 1
    _SendMessage(WinGetHandle("Test123"), $WM_USER+350, "Test " & $i)
    $i += 1
    Sleep(100)
WEnd

Why i cant send string? Script B send only that : "0x00000000"

well, I think that one application is only sending the other the position of the array within the memory address, then the second application should be pointed there to extract what the memory hold.

Share this post


Link to post
Share on other sites

Script A :

#include <Constants.au3>
GUICreate("Test123")
GUISetState()
$Label = GUICtrlCreateLabel("Test", 20, 20, 200, 20)
GUIRegisterMsg($WM_USER+350, "Test")

Do
Until GUIGetMsg() = -3

Func Test($hWndGUI, $MsgID, $Param)
    GUICtrlSetData($Label, $Param)
EndFuncoÝ÷ Ù'+Ajëh×6#include <Constants.au3>
#Include <Misc.au3>
$i = 1
While 1
    _SendMessage(WinGetHandle("Test123"), $WM_USER+350, "Test " & $i)
    $i += 1
    Sleep(100)
WEnd

Why i cant send string? Script B send only that : "0x00000000"

You can only send integers with a message not strings, so if you want to tell the other app about a string yu have to tell it the address of where the string is and the other app reads it from that memory address. My example a few posts back shows you how to do it.


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