tags:

views:

66

answers:

3

I have a ATLCOM Shell Extension which adds Right Click Extension to Windows Explorer. How Can I pass a message from my DLL to another MFC application.

To Sumarize, I want to pass a Message from DLL to MFC application.

+3  A: 

You can use Windows API SendMessage or PostMessage.

vulkanino
@vulkanino, How do I retrieve the mssage in MFC application? While sending or posting the message where or to whom should I send or post?
Subhen
@Subhen: with a message map. See http://vcfaq.mvps.org/mfc/12.htm for an explanation. I assume you already know the other application (i.e. it runs, and you have its HWND. Can't send messages to zombies after all)
MSalters
+1  A: 

Have you tried using Windows messages?

You can define you own custom messages like this:

const UINT WM_YOUR_CUSTOM_MESSAGE = ::RegisterWindowMessage(_T("Your_custom_message"));

You receive the message in a standard WindowProc() function:

WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 

You can add one by overriding the CWnd::WindowProc() function in your receiving application (use classwizard in either the dialog in an MFC dialog app, or the MainFrm in a Single / Multiple Document MFC app)

You send the message to all windows like this:

ULONG ulRC = BSM_APPLICATIONS;
BroadcastSystemMessage(BSF_IGNORECURRENTTASK | BSF_FORCEIFHUNG, // do not send message to this process and don't hang
                        &ulRC,                  // broadcast only to applications
                        WM_YOUR_CUSTOM_MESSAGE,     // message registered in previous step
                        0,                      // wParam message-specific value
                        0);                     // lParam message-specific value

If you need to pass some information with the message, you can also make use of the wParam and lParam values in the message.

Big GH
@Big GH, But How will I retrive this mesaage from another application
Subhen
+1  A: 

There is one big assumption in the current answers. You normally send messages to processes, not applications. This is a huge difference. There could be zero, one or more instances of the MFC application running.

In the case of zero applications, the DLL will have to call CreateProcess. CreateProcess allows the DLL to pass command-line arguments to your MFC app.

In the case of one MFC application, the message-based solutions offered above will work.

In the case of more than one running MFC application, they'll have different HWNDs. THe DLL picks the application(s) by picking which HWND to send the message to. The logic by which an HWND is picked is outside the scope of this question. One common solution is to just send the message to the HWND's of all running instances.

Finding out how many instances of your application are running is achieved by Process Enumeration

MSalters