Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture windows messages generated by an MFC app in plain C++ dll

First of all: Is this even possible?

I have a third party dll that interfaces some hardware. It's written in MFC. I received (from the dll vendors) a sample Visual Studio 2010 solution which has only one project: An MFC application (.exe) which calls the third party dll in question. It works fine.

When I try to use the third party dll from my dll (which is plain C++, no MFC, no .NET), I can call its functions fine, but there's a catch: the sample MFC app seems to "override" MessageProc in order to capture certain messages that the third party dll generates. And though the dll has a function called "RegisterFuncCallback" and I use it, my callback never gets called.

So here's the problem: How can I capture those messages without creating an MFC app? (Is it even possible?)

like image 486
dario_ramos Avatar asked May 16 '11 19:05

dario_ramos


2 Answers

Alright, I made it. Here's how:

  1. Create a class which inherits from CWnd
  2. Declare a message map associating the desired messages and their handlers
  3. When creating the Window, use the CreateEx function (I did it in my class's constructor), and pass it the HWND_MESSAGE flag in the last but one parameter. This will create the window as a "Message Window", that is, invisible.
  4. Once I'm done initializing the window and the MFC dll, I call RunModalLoop on my hidden window, in a separate thread, since it's blocking. This fires up the message pump, and starts receiving the MFC dll's messages.

Edit: I could finally do it using just Win32 API. Here's my story, code included: Programate Algo Blog. Don't worry, it's in English.

like image 182
dario_ramos Avatar answered Sep 22 '22 14:09

dario_ramos


If the DLL works with Win32 messages you won't get around them. But you do not neccessarily need MFC for that, a simple WinAPI solution would suffice. MFC just wraps the Win32 API. If those messages aren't Win32 messages, you do not need a Win32 application.

like image 37
Christian Rau Avatar answered Sep 24 '22 14:09

Christian Rau