c++ - Global CBT hook is never called -


I am trying to create an application that is informed about the creation and destruction of top-level windows , System-wide I have created a code to take advantage of the CBT hook, the solution includes two projects, DLL and XE are the references to the DLL project in the EXE project. The hook is being set from a DLL EXE project has a message loop in the problem that the CBT hook is not working VS With the help of debugger, I think the hook has got out Callback is never called , while the return code from SetWindowsHookEx is non-zero which means the hook is set up I went. What's wrong How can I fix this?

Here's a minimum example. DLL, main.cpp:

#include & lt; Windows.h & gt; Typefiff Zero (* DECODERPROC) (Int Code, WARADAM Y PARAM, LPARAM lParam); Hinstes hInst = nullptr; HHOOK hHook = nullptr; DECODERPROC fpDecoder = nullptr; (Code & gt; 0 & amp; amp; fpDecoder) {fpDecoder (code, wParam, lParam), LRESULT callback cbtProc (integer code, wParam wParam, lParam lParam) {// FIXME: ; } Return CallNextHookEx (hHook, code, wParam, lParam); } __declspec (dllexport) bool InstallHook (DECODERPROC decoder) {returns (if hHook); FPDCoder = decoder; Return (hHook = SetWindowsHookEx (WH_CBT, cbtProc, hInst, 0))! = Null; } __declspec (dllexport) bool uninstluk () {if (! HHook) returned false; Bool res = Unexpected WindowHack (HHK)! = Null; If (res) hHook = NULL; Return ridge; } BOOL APIENTRY DllMain (HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {hInst = reinterpret_cast & lt; HINSTANCE & gt; (HModule); Return TRUE; }

EXE, main.cpp

#include & lt; Iostream & gt; #include & lt; Locale & gt; #include & lt; Windows.h & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Io.h & gt; using namespace std; Typefiff Zero (* DECODERPROC) (Int Code, WARADAM Y PARAM, LPARAM lParam); __declspec (dllimport) bool InstallHook (DECODERPROC); __declspec (dllimport) bool uninsthook (); Int main () {_setmode (_fileno (stdout), _O_U8TEXT); WNDCLASS windowClass = {}; WindowClass.lpfnWndProc = [] (HWWD, HWD, UINT Message, Varm Vaporam, LAPRM LPRAM) - & gt; LRESULT {if (message == WM_DESTROY) uninstalook (); Return DefWindowProc (hWnd, Message, wParam, lParam); }; LPCWSTR windowclassname = L "Fobar"; WindowClass.lpszClassName = windowClassName; If (register class (and window class)) {wcerr & lt; & Lt; L "failed to register window class" & lt; & Lt; Endl; Return 1; } Hwnd messwindows = makewindows (windowclassname, 0, 0, 0, 0, 0, 0, hwmsmms, 0, 0, 0); If (! MessageWindow) {wcerr & lt; & Lt; L "Failed to create message-only window" & lt; & Lt; Endl; Return 1; } InstallHook ([] (integer code, wParam wParam, lParam lParam) {wcout & lt; l & lt; l & lt; end 0) {TranslateMessage (& amp; msg); DispatchMessage (& amp; msg); } Return msg.wParam; } "itemprop =" text ">

If you are running on Windows 64-bit, you need 32- 64-bit version of DLL to bite the bit and every running process. A 32-bit DLL can not hook up a 64-bit process, and vice versa. You need to call SetWindowsHookEx () with a 32-bit process to hook 32-bit processes, and hook up 64-bit processes to hook 64-bit processes .

And the important thing is that, a different example of your DLL gets injected in every running process, so your fpDecoder callback pointer is zero in every example of DLL Except that your EXE is installed on InstallHook () then you interact with your main EXE using the inter communication process (window message, named pipes, mailslot, socket, etc.) to renew your hook Format is needed, so you use a function indicator Can not.

Depending on which process you are actually debugging, you might want to see that cbtProc () If you are debugging your main XE process, Once the hook is set up, your code is not doing anything to trigger any CBT activity in your ECE ECE process, and the debugger will not show any CBT activity to you in other processes, which There is no debugging.

Depending on what you actually see in your hook, you might consider using SetWinEventHook () instead, because it is used with DLL or Can be done without more flexible filtering capabilities than SetWindowsHookEx () .


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -