I have an NPAPI plugin which makes use of SetWindowsHookEx
under the hood to do some screen scraping. This plugin seems to run successfully on most machines I've tested. However, on one machine (32-bit Windows 7), when I'm running the plugin under Chrome (26.0.1410.43m), the call to SetWindowsHookEx
crashes the process in which the plugin is hosted. It's not that SetWindowsHookEx
returns an error - it simply crashes the process. And again, this only happens on one machine: it works fine on all the others we've tested.
But here's the weird thing. As you may know, Chrome has several ways of locating plugins. Normally, our setup.exe creates some registry entries that point to the plugin, like so (from the NSIS installer):
WriteRegStr HKLM "Software\MozillaPlugins\@alanta.com/WinVncCtl\" "Path" "$INSTDIR\npWinVnc.dll"
WriteRegStr HKLM "Software\MozillaPlugins\@alanta.com/WinVncCtl\" "ProductName" "Alanta Remote Desktop Server"
WriteRegStr HKLM "Software\MozillaPlugins\@alanta.com/WinVncCtl\MimeTypes\application/x-alanta-vnc" "Description" "Alanta's VNC Server NPAPI Plugin"
When Chrome loads the plugin from that location, it crashes on the SetWindowsHookEx
call.
However, if I uninstall the plugin, then copy exactly the same DLL to %ProgramFiles%\Mozilla Firefox\Plugins\
, then Chrome loads and runs the plugin just fine, and the call to SetWindowsHookEx()
succeeds.
For what it's worth, here's the actual (pretty bog-standard) call:
// Add the CallWnd hook
hCallWndHook = SetWindowsHookEx(
WH_CALLWNDPROC, // Hook in before msg reaches app
(HOOKPROC) CallWndProc, // Hook procedure
hInstance, // This DLL instance
0L // Hook in to all apps
// GetCurrentThreadId() // DEBUG : HOOK ONLY WinVNC
);
The debugger indicates that all the parameters are pointing to legitimate, valid things.
Any suggestions for troubleshooting this? Anybody aware of any weirdnesses in SetWindowsHookEx
that might show up in this scenario?
EDIT: The CallWndProc
looks like this:
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// Do we have to handle this message?
if (nCode == HC_ACTION)
{
// Process the hook if the WinVNC thread ID is valid
if (vnc_thread_id)
{
CWPSTRUCT *cwpStruct = (CWPSTRUCT *) lParam;
HookHandle(cwpStruct->message, cwpStruct->hwnd, cwpStruct->wParam, cwpStruct->lParam);
}
}
// Call the next handler in the chain
return CallNextHookEx (hCallWndHook, nCode, wParam, lParam);
}
Breakpoints in the CallWndProc
don't seem to get hit, so I suspect that the crash is happening during the setting of the hook rather than during its later processing.
HOOKPROC
. It should have that type already. If it doesn't, the compiler is trying to warn you of a potential problem. Don't just shut it up with a cast.CallWndProc
seems to have the correct signature, and everything compiles just fine without the cast. I added the code for the CallWndProc to my question.