?
Solved

How can i Send Message between 2 Application?

Posted on 2003-02-24
7
Medium Priority
?
500 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:reza666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8014327
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..
0
 

Author Comment

by:reza666
ID: 8015376
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?
0
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 200 total points
ID: 8017882
hmm sorry it didnt help, im win32 it works ive used that method. only thing i can suggest is maybe make a really high number and replace wm_user+400
it's supposed to work but i've had bad results sometimes with wm_user, i don;t know why thos messages are not really supposed to be already in use and both apps should know wm_users value 0x400 i think.
worth a shot. other then that in dont have builder so i cant help :O\
0
 

Author Comment

by:reza666
ID: 8033357
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
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question