Click to See Complete Forum and Search --> : Interprocess communication problem


DeepT
February 24th, 2006, 11:29 AM
I have two unrelated processes that need to talk to each other. The communication needed is very simple, just some basic commands like start, stop, pause, etc..

This needs to work under windows 98/ME as well as XP. I thought named pipes would be easy to use, but they do not work under 98/ME. Anonymous pipes might work, but I do not know how the other process is supposed to get a handle to the pipe.

I could create a socket, but that seems to be overkill, as well as some shared memory.

Does anyone have some suggestions on how to do this?

golanshahar
February 24th, 2006, 11:59 AM
You can use ::SendMessage(..) with WM_COPYDATA (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/dataexchange/datacopy/datacopyreference/datacopymessages/wm_copydata.asp) ;)

Cheers

DeepT
February 24th, 2006, 12:00 PM
No I cant. No messge pump, no windows.

ovidiucucu
February 24th, 2006, 12:04 PM
[ Redirected thread ]

philkr
February 24th, 2006, 12:07 PM
You can use application defined message ids:

#define WM_START WM_APP+1
#define WM_STOP WM_APP+2
#define WM_PAUSE WM_APP+3
// etc..
SendMessage(hwndOtherApp, WM_START, 0, 0);


EDIT: You can create a dummy window which only handles messages by specifying HWND_MESSAGE as parent window in CreateWindow().

DeepT
February 24th, 2006, 12:47 PM
How does the other app know which hwnd to send it to?

golanshahar
February 24th, 2006, 01:07 PM
How does the other app know which hwnd to send it to?

You can use ::FindWindow(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp) to locate it ;)

Cheers

ovidiucucu
February 24th, 2006, 01:11 PM
message-only windows (created by passing HWND_MESSAGE as parent handle) require Windows 2000/XP.
to send message between applications send registerd messages rather than application defined messages (see RegisterWindowMessage function).
you can set an unique class name for the receiver window, then use that class name in FindWindowEx to get its handle.
another way is to send the message passing HWND_BROADCAST as first parameter in SendMessage function (the message will be sent to all top-level windows from the system).

DeepT
February 24th, 2006, 01:27 PM
Oh in that case, I can't use it. It must work under 98/ME. Any other suggestions?

MikeAThon
February 24th, 2006, 04:25 PM
In Windows, the following types of interprocess communications (IPC) are available to you:

The following IPC mechanisms are supported by Windows:
Clipboard
COM
Data Copy
DDE
File Mapping
Mailslots
Pipes
RPC
Windows Sockets
See "Interprocess Communications" at http://msdn.microsoft.com/library/en-us/ipc/base/interprocess_communications.asp

In your case where you do not have a message loop, and you do not want sockets, it's probably best to use shared memory (via a file mapping). I know you said that you didn't want to use that either, but based onyour posts above we're running out of options. Each process would also need a worker thread that waits on a named event (or some other type of synchronization object). The first process would signal the event when it sent data to the second process, which would trigger the worker thread in the second process to retrieve the data.

Mike

kirants
February 24th, 2006, 04:29 PM
If all you want is to signal commands, with no data to be passed, you can go for kernel objects like manual/auto reset events ( named )
You could have events named , say MyOwnStartEvent,MyOwnStopEvent and set/reset these events from process A and use WaitForSingle(Multiple)Object(s) in the other process.

Please look at:
CreateEvent
SetEvent
ResetEvent
PulseEvent
WaitForSingleObject
WaitForMultipleObjects

DeepT
February 24th, 2006, 04:37 PM
I looked at events which might work although having 4 or 5 of them might get messy. I looked at mail slots and other things to, however I ran into one common problem:

How do process A and B get a handle to the same object? IE: I create a mail slot, or an anonymous pipe, or whatever in Program A. How does program B get access to it? How do I get the handle to program B?

kirants
February 24th, 2006, 05:09 PM
Named objects , as I already mentioned. Named objects are the way to share among processes. So, if you specify the same name, the kernel simply gives you a handle to an existing object and doesn't create a new one.

Arjay
February 24th, 2006, 08:19 PM
Another technique is to use named events and a mutex along with a memory mapped file (MMF) where the data is shared between both processes using the MMF and each process signals the other process when the data changes using the named events. The mutex is used to synchronize the data access between processes.

The articles listed below contain source for two examples: a LogSnd and LogRcv which utilitize this technique to pass a structure of logging data between two processes.

Win32 Thread Synchronization, Part I: Overview (http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9823/)

Win32 Thread Synchronization, Part 2: Helper Classes (http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9825/)

Arjay