Jump to content

Use PostMessage ... maybe?


 Share

Recommended Posts

Creating solutions to problems with AutoIt has been a great experience. AutoIt handles just about every application programming task that we need, including Regular Expressions, the DOM for Excel and IE, Registry management, and so forth. And because its syntax is similar to VBScript, the learning curve has been low.

But we have a question.

Currently we have an AutoIt program that contains several simple functions which are called by an external application. We do it like this:

AutoIt loops waiting for a value, which is a function name string, to be placed in a Registry key by that external program. It reads the string, does the function, writes the answer out to another Registry key, sets the key back to a blank string, and then goes back to looping and waiting.

Is there a better way to do this? Is there a better way to have AutoIt fire off an internal function that the external application needs the results from?

One of these external applications can make use of the PostMessage API call (but not SendMessage). Furthermore, the message type can be an offset from WM_USER, an offset from WM_APP, or a RegisterWindowMessage.

Bear in mind that we know very little about Windows messaging and API calls, but I assume that we would need to compile the AutoIt app into an executable in order to know "what" to send the PostMessage message to. Yes? No? If we are way off base, then don't hesitate to say so!

If there is a better way, if we are on the right track, then any help or example would be peachy-keen.

Link to comment
Share on other sites

Search this forum for WM_CopyData

Yes, I was looking at some of those examples before. However, I was not refering to two AutoIt scripts communicating with each other, but rather an external, non-AutoIt application having an AutoIt script fire specific functions.

The only API call that the external app has available is the PostMessage command. So I was wondering if it could, or should, be used to call functions within an AutoIt Script. if so, how?

The external application's message type to send can be either an offset from WM_USER, an offset from WM_APP, or a RegisterWindowMessage. I assume we would want to use the latter, but how? Furthermore, do we, or can we, use the WParam and LParam fields in the message to tell AutoIt which function to fire? If so, how?

It may be that what we are doing now is as good as it can ever get, we simply do not know, but I thought someone here might be able to tell us.

Link to comment
Share on other sites

Is there any reason your external application can't use an AutoIt object or just call autoit functions from the dll? If it can, then it is an easy matter to exchange press buttons and exchange data between the two programs.

Another way is to have the AutoIt program monitor an editbox in the external application (it can be an invisible edit box), and get its instructions as to what to do from that box and return data to that box or another one.

Edited by BobK
Link to comment
Share on other sites

Is there any reason your external application can't use an AutoIt object or just call autoit functions from the dll?

If you are referring to AutoItX, I can't do anything like CreateObject( "AutoItX3.Control" ). It's not a programming-type app. But even if I could call a DLL, how would I convert the au3 file containing all the functions we created into a dll file?

Another way is to have the AutoIt program monitor an editbox in the external application (it can be an invisible edit box), and get its instructions as to what to do from that box and return data to that box or another one.

Well, it's not a programming-type app, so I can't add any controls to it.
Link to comment
Share on other sites

You can send stuff using wParam and lParam of PostMessage, but that would be numbers or pointers, as it basically 2 dwords at your disposal.

Quick example, run these two scripts, should be pretty self-explanatory:

#include <GUIConstants.au3>

$gui = GUICreate("Sender", 300, 150)
$btn = GUICtrlCreateButton("Send", 100, 50, 100, 50)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $btn
            sender()
    EndSwitch
WEnd

Func sender()
    If WinExists("Receiver") Then
        $hwnd = WinGetHandle("Receiver")
        DllCall("user32.dll", "int", "PostMessage", "hwnd", $hwnd, "uint", $WM_USER, "uint", 1, "uint", 2)
    Else
        MsgBox(0,"","Receiver window not found")
    EndIf
EndFuncoÝ÷ Ù«­¢+Ø¥¹±Õ±ÐíU%
½¹ÍѹÑ̹ÔÌÐì((ÀÌØíÕ¤ôU%
ÉÑ ÅÕ½ÐíI¥ÙÈÅÕ½Ðì°ÌÀÀ°ÄÔÀ¤)U%I¥ÍÑÉ5Í ÀÌØí]5}UMH°ÅÕ½Ðí=¹}]5}UMHÅÕ½Ðì¤)U%MÑMÑÑ ¤()]¡¥±U%Ñ5Í ¤±ÐìÐìÀÌØíU%}Y9Q}
1=M)]¹()Õ¹=¹}]5}UMH ÀÌØí¡]¹°ÀÌØí5Í°ÀÌØíÝAÉ´°ÀÌØí±AÉ´¤(%5Í ½à À°ÅÕ½Ðíå¼Ý½Ñ̵ÍÍÌÌìÅÕ½Ðì°ÅÕ½Ðí5ÍôÁàÅÕ½ÐìµÀì!à ÀÌØí5ͤµÀì
HµÀì|($$$$$$$$$ÅÕ½ÐíÝAÉ´ôÅÕ½ÐìµÀìÀÌØíÝAÉ´µÀì
HµÀì|($$$$$$$$$ÅÕ½Ðí±AÉ´ôÅÕ½ÐìµÀìÀÌØí±AÉ´¤)¹Õ¹

The question is, if that external app is capable of creating some kind of struct to keep that string you wanna pass, and send that pointer. Or maybe, if 8 bytes is enough for your message :)

"be smart, drink your wine"

Link to comment
Share on other sites

Siao -

Thanks. Your examples were spot on. They worked to perfection. I was able to post a message from the external app to the "receiver" script flawlessly.

The question is, if that external app is capable of creating some kind of struct to keep that string you wanna pass, and send that pointer. Or maybe, if 8 bytes is enough for your message

No, the external app does not have the ability to create a structure and generate a pointer to it, however, I can use the LParam and WParam as indexes to an array(s) of function names. That will work very well. Very well, indeed.

Oh, and thanks to PsaltyDS and BobK for your input, too. They've certainly given us some ideas we can use on other projects!

Link to comment
Share on other sites

  • Moderators

I'm curious... is there any particular reason for using PostMessage versus SendMessage (other than the obvious of not waiting until the 2nd app responds that it has received the information)?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm curious... is there any particular reason for using PostMessage versus SendMessage (other than the obvious of not waiting until the 2nd app responds that it has received the information)?

It's not a programming-type app and the only outside link it has is PostMessage in the form of a user input menu (which message to post, which handle to post it to, LParam, WParam). Heck, I'm not sure why it has that, unless the developer had intentions that were never carried through. SendMessage would have been nice, though!
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...