Solved

Interprocess Messaging in Windows

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This is about my first experience with programming Arduino.
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…
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…

730 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