-1

I am sending message WM_COPYDATA to my application which has a lot of forms, but I cannot receive it. Simple Application with one form works fine.

I have this code in main form. No message received

private
 procedure ReceiveMessage(var Msg: TWMCopyData); message WM_COPYDATA;
...
procedure TForm1.ReceiveMessage;
begin
  ShowMessage(PAnsiChar(Msg.CopyDataStruct.lpData));
end;

I checked Application and main form handles. So handles looks like:

Form1.Handle (main form) = 3348672
FindWindow result = Application.Handle = 7148290
FindWindowEx(Application.Handle,0,nil,nil) = 0 

I read here(Delphi: What is Application.Handle?) in Don's answer that messages which were sent to Application handle are redirected to main form, but in my situation it doesnt happen. How can I receive messages? Why messages are not redirected to main form?

Testing under Delphi XE8, Windows 10

Community
  • 1
  • 1
  • 3
    *".. messages which were sent to Application handle are redirected to main form .."* - Nope. – Sertac Akyuz Aug 27 '15 at 00:06
  • *How can I receive messages?*: By writing a message handler for that message. *Why messages are not redirected to main form?*: How can we tell? You've not shown how (or from where) the message is being sent. Please provide a MCVE for a small app that sends the message, as well as a small app that is intended to receive the message that doesn't do so. – Ken White Aug 27 '15 at 02:28
  • Don's answer says messages are forwarded to the main form "as appropriate." It's really just the handful of messages that are necessary for the main form and the application window to pretend to be a single window. The wm_CopyData message isn't among them. You could check in *Forms.pas* to confirm Don's assertion. – Rob Kennedy Aug 27 '15 at 04:59
  • 2
    Create a hidden window (by `AllocateHWnd`) and `RegisterWindowMessage` on both sides (unique enough). On the sender side broadcast that registered message with the handle of the sender's hidden window. When the receiving sides receive that message, they respond by posting (because it's better to avoid synchronous `SendMessage(Timeout)`) the same message to the received sender's window with their own handle (and optionally store the sender's handle so they will know each other). And the sender receives their handles and build a collection of receivers. – TLama Aug 27 '15 at 07:36
  • If you want to receive messages using TApplication.OnMessage event, you will need to handle it in a separate class ( type TAppMessageHandler = class... OnMessage...) , Application.OnMessage = MyMessageHandler.OnMessage; once you get the message, you can send it to your Application.MainForm – nurettin Apr 27 '18 at 11:57

2 Answers2

1

Except the fact that you don't provide any real code that you need help with, your question is probably wrong. If I assume correctly, you can send the message, you can recieve it but you send it to wrong place.

If your form has dynamic title, you can find its handle like this. Set some fixed part of the title "MyWindowTitle" and find that with this function.

function FindFormHandle: HWND;
var
  NextHandle: HWND;
  NextTitle: array[0..260] of char;
begin
  Result:=0;
  NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);

  while NextHandle > 0 do
  begin
    GetWindowText(NextHandle, NextTitle, 255);

    if Pos('MyWindowTitle', NextTitle)>0 then
      Exit(NextHandle);

    NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
  end;
end;
smooty86
  • 1,112
  • 7
  • 13
-3

I found the solution to receive messages: I set Main form's Caption equal to Application.Title. So now FindWindow returns Main form's handle. But now I cannot use dynamic caption of main form.

  • 1
    So your problem was in code which we cannot see? How is this any different than when you test with an empty application? My guess is that something else in your application "swallows" the message you're trying to receive. – Jerry Dodge Aug 26 '15 at 23:50
  • As you yourself note (*But now...*) this is a hack guaranteed to break later. Solve it the right way, see other answer. – Jan Doggen Aug 27 '15 at 07:23