?
Solved

Interprocess Messaging in Windows

Posted on 2004-09-12
3
Medium Priority
?
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

764 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