Link to home
Start Free TrialLog in
Avatar of Cariarer
Cariarer

asked on

Message que question...

Hi everyone...

I would like to know if it is possible to send a string between programs... The TMsg structure only allows me to send two integers along with the message (lParam & wParam).Can I create my "own" message structure, or am I bound to this format? I could use a shared memory area and only transmit the handle and size, but I would like to use a more "elegant" way, if all possible. Any comments are welcome :-)

Thanks, Car...
Avatar of Cariarer
Cariarer

ASKER

Edited text of question.
Car, you won't come around the shared memory area as you want obviously send the string to another process. Usually different processes cannot access memory of each other.

Ciao, Mike
The message structure can not be changed. Actually message structure members are used as function parameters, so changing (enlarging) message structure will have no effect.
 
You can only share string between two applications if the string located in shared memory. This can be done using GlobalAlloc and sending the memory handler received from GlobalAlloc..

You can also pass the string using clipboard.

Another possibility is using global atom table. This restricts string length (upto 255 characters) and puts some restrictions on string contents (atom names mus be unique, but atoms have usage counter so it's OK).
To use this approach You create an atom using
Atom:=GlobalAddAtom(PChar(AString));
and then send received value
SendMessage(ReceiverHandle,CustomMsg,0,Atom).

CustomMsg is id for Custom Window Message, see Q.10184790 there is detailed explanation of implementing custom message.

In receiver application the string can be accessed by GlobalGetAtomName function (see Win32 help for details).
After atom name retrieval (the string) the atom should be deleted using GlobalDeleteAtom.

Instead of using "dynamic" custom message it's possible to define "static" custom message : const CustomMessage = WM_USER+1;
Any way this should be done in all involved applications.
It's also possible to use existing message, but be carefull there could be problems.

Roman.
There are two good possibilites:

(1) Use WM_COPYDATA (internally it uses memory mapped files) or
(2) use memory mapped files.

CreateFileMapping(dword(-1),...)
MapFileOfView

Regards, Madshi.
Here is some pseudo code to give you the general idea (it's from my memory, args might be ordered wrong, function names may be wrong...)

procedure SendString(s: string; hWndOtherApp: HWND);
var p: PChar;
    MsgID: UINT;
begin
  GlobalAlloc(p, Length(s)), GMEMSHARED or GMEMFIXED or something);
  StrPCopy(p, s);
  MsgID := RegisterWindowMessage("SendString");
  SendMessage(hWndOtherApp, MsgID, 0, lParam(p));
  GlobalFree(p);
end;

You don't have to use RegisterWindowMessage, you could use WM_USER + xxx if you want.

When your other picks up the message, it should cast lParam as a pchar and copy it to a string variable with StrPas.

Cheers,
Phil.

I can send you a working example with WM_COPDYDATA

..-=ViKtOr=-..
Hi Everyone...

I think I decided to use the shared memory method with GlobalAlloc anyway. I will use PostMessageTimeOut to transmit the message, because the allocated memory for this string has to be freed afterwards and I also don't want to send further strings if the other process is either not responding or not existent anymore. Since I allready was knowing how to do that, I propose to give the points to the person who can answer me the following question first: How  can I use the RegisterWindowsMessage function... I can see how I have to use it to send a message, since Phil used it in his code, but how can I use it to "get" the message. Usally I use something like:
procedure WMIBInit(var Message : TcustomMsg); message WM_IBINIT;
But the WM_IBINIT has to be a constant. So how do I get that message? With a global message handler? Anyway, the person who submits the answer first, get's the points...

Thanks, Car...
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial