I found "Delphi Inter Process Communication (IPC) using SendMessage" with Google.
Here's a piece of the code from Sender to send a message for Receiver :
procedure TfrmClient.FormCreate(Sender: TObject);
begin
MyMsg := RegisterWindowMessage('MyMessage');
ServerApplicationHandle := FindWindow('TApplication', 'Project1');
end;
The problem is my receiver have random caption name. So how can I send a message to receiver? Any idea? My Sender is a DLL and my receiver is 开发者_如何学PythonExe.
One obvious solution is for the EXE to give the DLL the handle to a window that it should send messages to. The EXE will call a function in the DLL, so that's the perfect time to provide a window handle. Remove all the guesswork. (For that matter, the EXE could just give the DLL the address of a function to call and skip messages altogether.)
Another solution is to broadcast the message. Use the special window handle HWND_BROADCAST
when you call SendMessage
and the message will go to all the top-level windows in the system. Only other windows that have registered the same message ID will do anything when they receive that message; the others should simply and safely ignore it.
Further to Rob's answer, if you have a large amount of messages to exchange, use the first message to send a window handle, and start two way non-broadcast communication.
And unless you really need an actual control, I would use AllocateHWnd()
to give you a window handle to receive the broadcast.
I know it is not answer to your question but I thing the choice of IPC is not the best one in this case. Look at IPC based on named pipes. They are fater then messages and you don't have the problem, how to find the receiver:
http://www.cromis.net/blog/downloads/cromis-ipc/
Don't post to the TApplication, but to the main form, and give yor main form a meaningful name:
const
DLL_MESSAGE = WM_APP + 100;
type
TMyDLLMessageReceiverForm=class(TForm)
procedure ReceiveTheDLLsCustomMessage(var Msg: TMessage); message DLL_MESSAGE;
end;
From the DLL:
var
WndHandle: HWND;
...
WndHandle := FindWindow('TMyDLLReceiverForm', nil);
if WndHandle <> 0 then
PostMessage(WndHandle, DLL_MESSAGE, SomeParam, SomeOtherParam);
Keep in mind that elevation will impact your ability to communicate. An application with elevated privileges can not send/receive messages from a process that is not.
As Runner suggested, your better with named pipes or even Mailslots.
精彩评论