简体   繁体   English

SetWindowsHook支持32位和64位应用程序

[英]SetWindowsHook to support both 32 bit and 64 bit application

I am using a windows hook in my application to determine whether another application has been re-sized. 我在应用程序中使用Windows钩子来确定是否已重新调整另一个应用程序的大小。 I then take some action depending on the new size of the other application. 然后,我将根据其他应用程序的新大小采取一些措施。

This works fine in pure 32 bit environment and pure 64 bit environment (hook dll complied as 64 bit and 32 bit separately as in the MSDN docs). 这在纯32位环境和纯64位环境(钩子dll如MSDN文档中分别编译为64位和32位)中正常工作。

However here is the problem. 但是,这就是问题所在。 Because of a mix of 32bit and 64bit application in say Window 64bit OS, if I launch the 64bit version of my application, I cannot monitor 32bit applications and vice versa. 由于在Window 64位操作系统中混合使用32位和64位应用程序,因此,如果启动应用程序的64位版本,则无法监视32位应用程序,反之亦然。

Is there any good way to do this. 有什么好的方法可以做到这一点。 It can be a completely different mechanism. 这可能是完全不同的机制。

Unfortunately, you are only able to inject 32-bit DLLs to a 32-bit processes and a 64-bit DLLs to 64-bit processes. 不幸的是,您只能将32位DLL注入到32位进程,而将64位DLL注入到64位进程。 This means you will need 2 versions of your DLL. 这意味着您将需要2个版本的DLL。 On top of this, SetWindowsHookEx() must be invoked from the same family of portable executable (that is, it will have to be invoked from 32-bit code for 32-bit processes and similarly for 64-bit processes). 最重要的是,必须从同一系列的可移植可执行文件中调用SetWindowsHookEx()(也就是说,对于32位进程,它必须从32位代码中调用;对于64位进程,则必须从32位代码中调用)。

Given your application is 64-bit, you'll have to launch an instance of your 32-bit program and have it set the hook. 鉴于您的应用程序是64位的,因此您必须启动32位程序的实例并设置它的钩子。 You would probably want to have one of these as a child which notifies the parent when the event you are interested in has been trapped. 您可能希望将其中之一作为child ,以便在您感兴趣的事件被捕获时通知parent

From the docs: 从文档:

SetWindowsHookEx can be used to inject a DLL into another process. SetWindowsHookEx可用于将DLL注入另一个进程。 A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. 无法将32位DLL注入到64位进程中,并且不能将64位DLL注入到32位进程中。 If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. 如果应用程序需要在其他进程中使用钩子,则需要32位应用程序调用SetWindowsHookEx才能将32位DLL注入32位进程,而64位应用程序调用SetWindowsHookEx可以注入64位。 DLL转换为64位进程。 The 32-bit and 64-bit DLLs must have different names. 32位和64位DLL必须具有不同的名称。

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

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