简体   繁体   English

全局挂接到MFC对象创建

[英]Hooking to MFC object creation globally

我有一个从CWnd继承的X类,我想从应用程序域外部连接到X Create,即程序A必须监视程序B中的X对象实例。有什么办法可以做到这一点?

  1. You can't monitor creation of an object of X (X ctor X::X() ). 您无法监视X对象的创建(X ctor X::X() )。

  2. You also can't monitor calls to X::Create . 您也不能监视对X::Create调用。

  3. But there might be a solution for you: 但是可能有一个解决方案:

Since X::Create calls CWnd::Create and this creates a window, you can hook up to window creation and might be able to detect when a X's window is created. 由于X::Create调用CWnd::Create并创建了一个窗口,因此您可以进行窗口创建,并且可能能够检测X窗口的创建时间。 But you will only be able to recognize this if there is something special about the windows created like a known window class used to create them. 但是,只有在创建的窗口有一些特殊的东西(例如用于创建它们的已知窗口类)时,您才能识别出这一点。 If this is the case, then: 如果是这样,则:

You already got the right answer in your previous question Hooking window creation in an MFC program . 您在上一个问题中已经获得了正确的答案在MFC程序中挂钩窗口创建

You can use SetWindowsHookEx (WH_CBT, lpfn, hMod, dwThreadId) to get notified when windows are created. 您可以使用SetWindowsHookEx (WH_CBT,lpfn,hMod,dwThreadId)在创建窗口时得到通知。

Some things to keep in mind: 注意事项:

  • The hook function must reside in a DLL, not in the process that calls SetWindowsHookEx . 挂钩函数必须驻留在DLL中,而不是在调用SetWindowsHookEx的进程中。 This is because windows will load/inject your DLL into every process that it monitors for window creation. 这是因为Windows会将您的DLL加载/注入到它监视的用于创建窗口的每个进程中。
  • If you want to monitor 32 bit processes, you need a 32 bit process and DLL. 如果要监视32位进程,则需要32位进程和DLL。
  • If you want to monitor 64 bit processes, you need a 64 bit process and DLL. 如果要监视64位进程,则需要64位进程和DLL。
  • If you want to monitor 32 bit and 64 bit processes, you need a 32 bit and a 64 bit process and DLL. 如果要监视32位和64位进程,则需要32位和64位进程以及DLL。
  • Your process must be a windows application, not a console application, and it must keep pumping messages. 您的进程必须是Windows应用程序,而不是控制台应用程序,并且必须保持泵送消息。
  • If you know the thread ID, you can pass that to SetWindowsHookEx. 如果您知道线程ID,则可以将其传递给SetWindowsHookEx。 If not, you can pass 0 and you will get notified for all windows on the same desktop. 如果没有,您可以传递0,并且您将收到同一桌面上所有窗口的通知。
  • Be cautious with what you do in the hook function and don't forget to call CallNextHookEx . 在挂钩函数中要谨慎操作,不要忘记调用CallNextHookEx
  • You will get notified for different events on different windows and you will have to filter out for nCode== HCBT_CREATEWND and the window classes (or whatever) that you are interested in. 您将在不同的窗口上收到有关不同事件的通知,并且必须过滤掉nCode == HCBT_CREATEWND和您感兴趣的窗口类(或其他)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM