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

对着背影说爱祢 提交于 2019-12-23 10:21:49

问题


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?)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6022087/capture-windows-messages-generated-by-an-mfc-app-in-plain-c-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!