How to communicate between two applications with SendMessage() ?

I want to communicate between two applications with calling of ::SendMessage(). Where can I find the parameter description for SendMessage, which are allowed. And where must I implement the Message-Handler in my main program, that is getting the message, in CMainFrame or in one of the CView's and how must I implement this Message-Handler ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First, create a custom message by either #define-ing a message value (WM_USER + 0x24), or by using RegisterWindowMessage(...).

Then create an ON_MESSAGE, or ON_REGISTERED_MESSAGE, (see help for ON_MESSAGE/ON_REGISTERED_MESSAGE) handler.  Depending on how you want the application to respond determines where to place the handler:  If you want the app to respond when it is up, place it in CMainFrame.  If you want it to respond "while it has a document open", place it in the View class.

It would also help to know what type of data you wish to send between the applications.

-=- James.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
atariAuthor Commented:
I have found an example on codeguru-website under MISC for RegisterWindowMessage(). I have programmed a main-mdi-application and if i push a button in the toolbar of CMainFrame a second sdi-application will be launched implement as CListView. If the user select some items of this CListView and right-click on it a popup-menu appears with some options, maybe open a new CMDIChildWnd or a new View of my main-mdi-application. Also the sdi-application should be, after sending a SendMessage() to my main-application disappear to the task-bar in order to show my main-application again. I also implemented some drag-and-drop-operations by clicking on an item in my launched sdi-ListView-program to any of my mdi-windows in my main-program.

that's what I wanted to do. It's nearly the same as the 'history-window' called, if you press Strg-H in the Netscape browser.

Maybe you have still further ideas. Maybe I increase the points for it, if I get any good answer.
I am still not fully clear on what exactly you are trying to communicate between both applications.  Sounds like simple values will work fine, if you need to pass strings, register them as Atoms, and pass the Atom value between applications.

-=- James.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

atariAuthor Commented:
My application, that I have launched, shows items in a CListView as a list. If the user clicks one or several items in this list and then right click with the mouse on one of these selected items, a popup-menu appears, in which the user can select a menu-item, which creates a new child-window in my main-mdi-application, from where the CListView had been launched, showing in the new child-window the items as graphic-objects, that I selected. This is one way, that I want to communicate between my two applications. Also if I close my main-application the main-application must send a WM_CLOSE-message to my launched application and so on.
I used #define WM_MYMESSAGE   (WM_USER+0x0200) and send the message with
::SendMessage(hwndlaunchedprogram,WM_MYMESSAGE,0,0) and after some try and error work everything works fine. But there is a new problem. ::FindWindow(Null,"applicationname");
is very difficult to handle if you have a mdi-application with several windows, having all their own names, and can be maximized or not, so the CMainFrame-name of the application can change in many ways. I don't know the correct way for implementing AfxRegisterWindowClass, because I created with template-structure and not dynamically with Create(), so do you know, if there is another way to receive the window-handle of a mdi-application ? The CMainFrame-Name of my application never changes, but if there are mdi-child-windows maximized then their name is added to the CMainFrame-name and the ::FindWindow()-function is always searching for the whole CMainFrame-name. Is there any other way to find out a running application ?
After this I will grade your answer. Thank you so far.
Override CFrameWnd::PreCreateWindow(), in your worker (display) applications.

   To create, and use, a new (custom) WNDCLASS, do something like this:

static   CString gs_sNewClassName = "atari_MyDisplayAppFrameClass";

BOOL    CMainFrame::PreCreateWindow( CREATESTRUCT cs )
   WNDCLASS    wcOldWndClass;
   WNDCLASS    wcNewWndClass;

   GetClassInfo( NULL, AfxGetInstanceHandle(), &wcOldWndClass );
   wcNewWndClass = wcOldWndClass;

   wcNewWndClass.lpszClassName = gs_sNewClassName;

   if( AfxRegisterClass( wcNewWndClass ) )
      cs.lpszClass = gs_sNewClassName;

   return( CMDIFrameWnd::PreCreateWindow( cs ) );

   Then, when looking for your worker/display app's windows, use FindWindow( gs_sNewClassName, NULL );

-=- James.
atariAuthor Commented:
Hi, this was a great information. How did you find out that source code ?

I will try it and hope that it will work. Using ::FindWindow with application-name was hard work, because I had to loop all the possible names of my mdi-child-windows. The above code should be much easier.

Thank you, I will increase the points for this helpful answer since a lot of time, that I'm waiting  for other answers, that didn't come until yet.

I believe there is always a way in MFC, but you must find out in every specific case, what's the best way to do.

Thank you very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.