Avatar of reza666
reza666
 asked on

How can i Send Message between 2 Application?

Hi everyone,
This is the first time i'm using this forum so i'm not sure how this is done, anyway my qustion is as the topic says.

I'm trying to send a simple message like "hello" from let's say Application1 to Application2 in "Borland C++ Builder 6"

1) how do i send a message
2) how do i receive the message

I been reading and searching and so far the only thing i been able to do is to find the handle for a application by using:

HANDLE hwnd = FindWindow(0,"Application1");

I think the function "SendAppMessage" could be used for sending a message but there is not much help or example for this :(

I realy appreciate it if anyone could help me with this problem.

Thanks
/reza666
Microsoft Development

Avatar of undefined
Last Comment
reza666

8/22/2022 - Mon
PlanetCpp

well i'm not sure where you want to send the text, you can send it to another applications edit control to display it, but it sounds like you want to send a private message to be seen and interpreted by the other app. you got the handle, first half done..
now you can send a message just like any other windows message, and since you created both application1 and 2 you can make your own message. for instance:
somewhere in app1 and app2 globally declare a message like
#define WM_MYMESSAGE WM_APP+1
you can also use WM_USER+1
now both apps now the value of WM_MYMESSAGE
next from app1:
SendMessage(app2handle,WM_MYMESSAGE,0,(LPARAM)(LPSTR)"hello");
now here's the part im not sure of cause your using a builder program. i'm not sure if you can see the window proc for the app2, if you can you have to look for the message. in win32 api you would ass another case to the switch statment in the window proc
IE.
........
case WM_PAINT
'BLAH BLAH IM JUST SHOWING THIS AS AN EXAMPLE
break;
case WM_MYMESSAGE:
MessageBox(NULL,(char*)lParam,"this is what you sent",MB_OK);
break;
case .......
..
...
now this might not apply to you builders are known to hide the specifics of the window proc from you which is bad as you can see (if yours does..)
btw if you did want to send the text to an edit then you have to get the handle to that edit
FindWindowEx is used for that you have to narrow a search down for it depending on where it is in the form
for example
edit = FindWindowEx(handletomainwidow,0,"edit",NULL);
then again i doubt a builder would have the class be the normal edit, it might be something like "TEdit"
then you would use
SendMessage(edit,WM_SETTEXT,0,(LPARAM)(LPCTSTR)"hello");

hope it helps..
reza666

ASKER
Hi,
Thanks for the quick response. I did almost do the thing
you said but still no luck.

Here's what i have done:

in App1 (.h file)
========
#define WM_MYMESSAGE WM_APP+400

in App1 (.cpp file)
========
when i press a button i send this:

HANDLE hwnd = FindWindow(0,"Test");
SendMessage(hwnd,WM_MYMESSAGE,0,(LPARAM)(LPSTR)"hello";

in App2 (.h file)
========
#define WM_MYMESSAGE WM_APP+400
also
void __fastcall WndProc(TMessage& Message); //Used to override the windows proc handling.

in App2 (.cpp file)
========
//---------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage& Message)
//---------------------------------------------------------
{
    //char* text;
    int LParam;
   
    // test (saw this on an example :)
    if(Message.Msg == WM_LBUTTONDOWN)
            TWinControl::WndProc(Message);

    //This is where i should get my msg when i press the        //button from App1 right? but it don't work :(
    if(Message.Msg == WM_MYMESSAGE)
    {
        ShowMessage("Yiippiiii!");
        MessageBox(NULL, (char*)LParam;, MB_OK);
    }
}

Also not sure what you mean by -->this is what you sent"
Do i receive it as a char pointer?

It seems as when ever i activate the app2 form (bring it to front) the "WndProc" function gets active and receives messages all the time but it don't respond at all to the "SendMessaga".
Am i doing something wrong?
ASKER CERTIFIED SOLUTION
PlanetCpp

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
reza666

ASKER
AHA

Now it works ;)

I only change from

static const WM_MYMESSAGE = (int) QEventType_ClxUser + 50; // QEventType not QCLXEventType

TO:
#define WM_MYMESSAGE WM_APP+400

So you were right about the define thing :)

Also a word of advice for everyone else!

When i'm debuging the method:
HANDLE hwnd = FindWindow(0,"Test");
do not work sometimes! But works when i run the program outside of borland.
Instead i have to use
HANDLE hwnd = Form->Handle;

Thanks for the help

Regards
/Reza666
Your help has saved me hundreds of hours of internet surfing.
fblack61