Message que question...

Posted on 1999-07-22
Medium Priority
Last Modified: 2010-04-04
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...
Question by:Cariarer

Author Comment

ID: 1390464
Edited text of question.
LVL 10

Expert Comment

ID: 1390465
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

Expert Comment

ID: 1390466
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
and then send received value

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.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 20

Expert Comment

ID: 1390467
There are two good possibilites:

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


Regards, Madshi.

Expert Comment

ID: 1390468
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;
  GlobalAlloc(p, Length(s)), GMEMSHARED or GMEMFIXED or something);
  StrPCopy(p, s);
  MsgID := RegisterWindowMessage("SendString");
  SendMessage(hWndOtherApp, MsgID, 0, lParam(p));

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.


LVL 10

Expert Comment

ID: 1390469
I can send you a working example with WM_COPDYDATA


Author Comment

ID: 1390470
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...
LVL 10

Accepted Solution

Lischke earned 150 total points
ID: 1390471
Hi Car,

Handling this kind of message is a bit different than predefined ones. You have overwrite WndProc of the control in question and check the arriving messages

with an if statement:

procedure TMyControl.WndProc(var Msg: TMessage);

  if Msg.Msg = WM_IBINIT then
  else inherited;

Ciao, Mike

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question