How to communicate between two applications with SendMessage() ?

Posted on 1998-02-28
Medium Priority
Last Modified: 2013-11-20
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 ?

Question by:atari
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
  • 3
  • 3

Accepted Solution

jtwine100697 earned 320 total points
ID: 1316667
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.

Author Comment

ID: 1316668
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.

Expert Comment

ID: 1316669
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.
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


Author Comment

ID: 1316670
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.

Expert Comment

ID: 1316671
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.

Author Comment

ID: 1316672
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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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