Solved

Interprocess Messaging in Windows

Posted on 2004-09-12
3
445 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now