Jump to content
JeanCremers

communicate between 2 progs with sendmessage.

Recommended Posts

Ok, so one prog does for instance 


HWND hwnd = FindWindow(NULL, "myprog");
if (hwnd)
  SendMessage(hwnd, WM_USER, 0, 0);

 

And what exactly should the other prog do? I have this, but it's not working.


in form constructor Application->HookMainWindow(&AppHook);

 

bool __fastcall TMainForm::AppHook(TMessage &Message)
{
if (Message.Msg == WM_USER)
  {

  Application->MessageBox("", "", MB_OK);
  return true;
  }
return false;
}

 

But i'm not getting any msg there.

 

Thanks.

Edited by JeanCremers
spelling

Share this post


Link to post
On 10/27/2023 at 3:08 PM, JeanCremers said:

HWND hwnd = FindWindow(NULL, "myprog");

 

Is "myprog" the title for your program's TApplication window or TForm window?  It makes a big difference.  I'm suspecting the latter.

Quote

I have this, but it's not working.

...

i'm not getting any msg there.

You are installing a message handler for the TApplication window.  To handle messages for the TForm window, override the TForm's virtual WndProc() method instead:

void __fastcall TMainForm::WndProc(TMessage &Message)
{
  if (Message.Msg == WM_USER)
  {
    Application->MessageBox("", "", MB_OK);
    Message.Result = 1;
  }
  else
    TForm::WndProc(Message);
}

That being said, WM_USER is not a good choice for inter-process communications.  Use WM_APP or RegisterWindowMessage() instead.

Edited by Remy Lebeau

Share this post


Link to post

Thanks Remy.

I'm getting EOSError 'System Error. Code 1411, Class does not exist.'

This is c++builder 6 🙂

 

Edit, i also tried 


void __fastcall TAstroClockMainForm::ApplicationEvents1Message(tagMSG &Msg,
      bool &Handled)
{
if (Msg.message == WM_APP)
  Application->MessageBox("", "", MB_OK);

}


and

 


HWND hwnd = FindWindow("TAstroclockMainForm", NULL);
if (hwnd)
  SendMessage(hwnd, WM_APP, 0, 0);

Edited by JeanCremers

Share this post


Link to post
18 hours ago, JeanCremers said:

Thanks Remy.

I'm getting EOSError 'System Error. Code 1411, Class does not exist.'

Sorry, my bad. I left out the part where the overriden WndProc() needs to pass unhandled messages to the default handler. I have corrected my example.

Quote

This is c++builder 6 🙂

What I showed applies to all C++Builder versions.

Quote

i also tried 


void __fastcall TAstroClockMainForm::ApplicationEvents1Message(tagMSG &Msg,
      bool &Handled)
{
if (Msg.message == WM_APP)
  Application->MessageBox("", "", MB_OK);

}

That won't work. That event is triggered only for messages that are posted to the main thread message queue. Which your example message is not.

Quote

and


HWND hwnd = FindWindow("TAstroclockMainForm", NULL);
if (hwnd)
  SendMessage(hwnd, WM_APP, 0, 0);

That likely won't work, either, because your MainForm's class name is TMainForm not TAstroclockMainForm (unless what you have shown is not your real code).

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×