捕获由简单的C ++ DLL中的MFC应用程序生成的Windows消息 [英] Capture windows messages generated by an MFC app in plain C++ dll

查看:140
本文介绍了捕获由简单的C ++ DLL中的MFC应用程序生成的Windows消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:这是否可能?

我有一个第三方dll可以接口一些硬件。它是用MFC写的。我收到(从dll厂商)一个示例Visual Studio 2010解决方案只有一个项目:MFC应用程序(.exe)调用第三方dll有问题。它工作正常。

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.

当我尝试使用我的dll(这是普通的C ++,没有MFC,没有.NET)的第三方dll,我可以调用它的功能好的,但有一个抓住:样本MFC应用程序似乎覆盖MessageProc,以捕获第三方dll生成的某些消息。虽然dll有一个名为RegisterFuncCallback的函数,我使用它,我的回调永远不会被调用。

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.

所以这里是问题:如何捕获这些消息而不创建一个MFC应用程序? (甚至可能吗?)

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

推荐答案

好的,我做了。以下是如何:

Alright, I made it. Here's how:


  1. 创建一个从CWnd继承的类

  2. 声明一个消息映射,将所需的消息和他们的处理程序

  3. 创建窗口时,使用CreateEx函数(我在我的类的构造函数中这样做),并将其传递给最后一个参数中的HWND_MESSAGE标志。这将创建窗口作为消息窗口,即不可见。

  4. 完成初始化窗口和MFC DLL之后,我在我的隐藏窗口中调用RunModalLoop一个单独的线程,因为它被阻止。这将启动消息泵,并开始接收MFC DLL的消息。

  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.

编辑:我可以终于只需使用Win32 API即可。这是我的故事,代码包括:
计划Algo博客。别担心,这是英文。

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.

这篇关于捕获由简单的C ++ DLL中的MFC应用程序生成的Windows消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆