Solved

Interprocess Messaging in Windows

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 76
word0 challenge 3 58
Path of Workbook 3 45
Scripting vs. Programming languages 25 115
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now