Solved

Interprocess Messaging in Windows

Posted on 2004-09-12
3
447 Views
Last Modified: 2013-11-13
We are facing a problem in sending messages between two different EXEs on the same machine using WIN API.

The sending EXE uses PostMessage or PostThreadMessage to send a user defined message to the target process. This operation completes successfully, as per the errorcode.
But the target EXE, which uses PeekMessage API to read the message never gets this message.

It would be great if someone could throw some light on what could be the problem or a better way to implement this. We are using Delphi 6.0 to implement this.

Thanks in advance.
0
Comment
Question by:ashish_me2
3 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12042879
I came across something simillar a while ago and eventually used CreateFileMapping as the main transport between the processes. I actually used both as in my scenario, many processes talked amongst themselves so I used the shared file as the data transport system and posting messages to inform target processes of pending messages. If this is something you could use, let me know and I'll post some source.

Paul
0
 
LVL 22

Expert Comment

by:cookre
ID: 12045539
If your message code is some WIN_USER+x, I'd suggest using RegisterWindowMessage():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/registerwindowmessage.asp?frame=true

MS is starting to use some codes above WIN_USER, so custom codes now have to be explicitly registered.
0
 
LVL 3

Accepted Solution

by:
Validor earned 50 total points
ID: 12049574
The sending of the message is probably working just fine.  The problem is likely in the PeekMessage.  The main window message loop gets the message and removes it from the queue before it ever becomes available to PeekMessage().  The only case where it wouldn't be is where you write your own message loop as in C++ or FreePascal.  

Instead, just write a message handler for your form.  It will intercept every message immediatley and do whatever you want with it.  If you wanted to store them all and process them after a period, just store them in a stringlist, integer array or TList or other container and process that list on a timer.

I have written two projects to demonstrate how to do this.  The first is a Delphi 7 project (should compile in Delphi 6 just the same) that receives messages and displays their values.  The second is a Delphi 6 app that sends the messages.  The first app shows its window handle in a label.  Just type that handle into the second app and enter any integer.  When you click OK, the value will appear in the other window.  Remember NEVER to pass pointer values between processes!  

//receiver
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

const WM_MYMESSAGE = WM_APP + 166;

type
  TForm1 = class(TForm)
    lblHandle: TLabel;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    procedure handlemessage(var msg : tmessage); message WM_MYMESSAGE;
    procedure displaymessage(msg: tmessage);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  lblHandle.caption := inttostr(self.handle);
end;

procedure TForm1.handlemessage(var msg: tmessage);
begin
  displaymessage(msg);
end;

procedure TForm1.displaymessage(msg: tmessage);
begin
  listbox1.items.add('['+timetostr(now)+'] ');
  listbox1.items.add('  Wparam: '+inttostr(msg.wparam));
  listbox1.items.add('  Lparam: '+inttostr(msg.lparam));
end;

end.

//Sender
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const WM_MYMESSAGE : integer = WM_APP + 166;

type
  TForm1 = class(TForm)
    Button1: TButton;
    txtHandle: TEdit;
    txtValue: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  lParam, wParam : Integer;
  hwnd : THandle;
begin
  hwnd := strtoint(txthandle.Text);
  wParam := 0;
  lParam := strtoint(txtvalue.text);
  SendMessage(hwnd, WM_MYMESSAGE, wParam, lParam);
end;

end.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 108
wordmultiple challenge 12 135
autoit - check if option is checked in another program 2 134
Windows Service to Receive TCP Packets 4 148
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

803 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