AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing strings between SendMessage & OnMessage

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TL



Joined: 01 Nov 2006
Posts: 9

PostPosted: Thu Mar 01, 2007 8:50 pm    Post subject: Passing strings between SendMessage & OnMessage Reply with quote

I need some help sending and returning strings using the SendMessage() and OnMessage() calls. My objective is to send a text string via lParam and have a string returned through lParam. I'm basing this approach on the following statement under the "PostMessage / SendMessage" entry in the AutoHotkey Help:
---------
A string may be sent via wParam or lParam by specifying the address of a variable. The following example uses the address operator (&) to do this:
SendMessage, 0xC, 0, &MyVar, ClassNN, WinTitle
In v1.0.43.06+, a string put into MyVar by the receiver of the message is properly recognized without the need for extra steps.

---------
Based on this statement I assume that AHK is handling the memory allocation and pointer issues related to passing strings between scripts.

My code looks something like this:

Sending script:
Code:
   ...
   SendString := "String to send"
   SendMessage, MessageNumber, Ignore, &SendString, , ahk_id %WindowID%
   ReceiveString := SendString
   MsgBox, ReceiveString = %ReceiveString%
   ...

Receiving script:
Code:
   MessageHandler( wParam, lParam )
   {
      ReceivedText := lParam
      MsgBox, ReceivedText = %ReceivedText%
      lParam := "String being returned"
   }

The MsgBox displayed from within the MessageHandler routine displays a large numeric value which I assume is the address of the SendString variable. Is it straight forward to load a variable with the string pointed to by lParam?
After the message handler routine exits the MsgBox displayed from the sending script indicates that the SendString variable still contains the text "String to send".

Any help would be appreciated.

TL
Back to top
View user's profile Send private message
Helpy
Guest





PostPosted: Fri Mar 02, 2007 11:32 am    Post subject: Reply with quote

Perhaps a DllCall to lstrcpy...
There might be a simpler way, but I don't see one right now.
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Sat Mar 10, 2007 8:15 pm    Post subject: Reply with quote

As you know, a script can send a string to another script via SendMessage, OnMessage, and WM_COPYDATA (for anyone else reading, a working example is at the bottom of the OnMessage page).

By contrast, a script cannot simply send the address of one of its variables or strings to another script because each script is a separate process with its own address space. There are several ways to work around this, but probably the easiest is to use WM_COPYDATA to go in both directions: sender to receiver, and vice versa.
Back to top
View user's profile Send private message Send e-mail
Barney9



Joined: 02 Mar 2007
Posts: 10

PostPosted: Sat Mar 24, 2007 1:14 am    Post subject: Reply with quote

Please help me understand this script. With lstrcpy you create a buffer that all processes can access? Then you send its memory adress to the receiver script via sendmessage? Who deletes the buffer after your script has finished using it?

Thank you!
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat Mar 24, 2007 9:09 am    Post subject: Reply with quote

I suppose you are talking about the script referenced by Chris...
lstrcpy doesn't create a buffer, that's the VarSetCapacity just above which does that, and it is local to the script.
Note that the lstrcpy is in the Receive part, it is to get the memory whose address in given in the COPYDATA message.
The Send part puts the address of a string in the script in the sent message. The buffer referenced by the message is seen as short-lived, receiver must copy the data to private memory as soon as possible.
Microsoft tells "The lParam parameter is valid only during the processing of the message." which, I suppose, means than upon processing the next Windows message (whatever it is), the buffer it points to is no longer available.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group