开发者

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

开发者 https://www.devze.com 2023-03-06 13:20 出处:网络
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 onl

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 withou开发者_运维百科t creating an MFC app? (Is it even possible?)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消