Jump to content

Send Message To A Process...


Recommended Posts

Hi!

I have searched on the Internet but it doesn't match my purpose. I wonder that how to send a message to a process.

For example:

I have 2 program ( A & B ) and a text file ©. A are in process but B not. When user open C, system will active B and B will have a command line ($CmdLine) including C address. I want B to send that to A and A will read C data. ;):)

Do you have an idea? ;):P

Thank you very much!!! o:) o:)

Link to comment
Share on other sites

Are (A) and/or (;) programs you have written yourself?

If they are 3rd party apps, could you specify them?

The way you can interact with a program depends on the program, so it's vital to know what you are working with to help you.

Link to comment
Share on other sites

Are (A) and/or (o:) programs you have written yourself?

If they are 3rd party apps, could you specify them?

The way you can interact with a program depends on the program, so it's vital to know what you are working with to help you.

Oh, yes! Both are written by me. o:) Do you have a suggestion??? ;):P;):)

Link to comment
Share on other sites

In that case there are a lot of ways to do it.

The easiest way is probably to:

* Add an Iniwrite() to program B, to write the path to a set file, in a set location.

* Give A a function that runs every second or so, by calling it from to a loop, or by using AdlibRegister()

* Make the function check the given location for the file using FileExist() (optional)

* Read the file using Iniread()

* Delete the file using FileDelete()

Link to comment
Share on other sites

In that case there are a lot of ways to do it.

The easiest way is probably to:

* Add an Iniwrite() to program B, to write the path to a set file, in a set location.

* Give A a function that runs every second or so, by calling it from to a loop, or by using AdlibRegister()

* Make the function check the given location for the file using FileExist() (optional)

* Read the file using Iniread()

* Delete the file using FileDelete()

Thank you very much! Can you tell me how to send it directly? ;):)

Link to comment
Share on other sites

Although there are loads of ways to get programs to comunicate I'm not completely sure how to do that.

StdinWrite and StdoutRead would do pretty much what you want, but I don't know if they can be made to work unless one process runs the other. (It's probably possible, but I don't know how)

The TCP functions will also be able to make the programs communicate without using a common file, but it's not really the intended purpose.

You could use a registry key instead of a file, but that's not any more direct.

The last option I can think of now is shared memory. I have know idea on how that works, but I there was a UDF for it a while ago in the example scripts forum.

I'm sure you're not the only one that asked this, so if you search the forum you might find something that will help you.

Link to comment
Share on other sites

These are pretty basic examples of using the WM_COPYDATA functions found around the forums.

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...