Forum Stats

  • 3,853,790 Users
  • 2,264,269 Discussions


Mouse hook from JNI doesn't work

802678 Member Posts: 1
edited Oct 3, 2010 9:44PM in Java Native Interface (JNI)

i am trying to register windows mouse hook(WH_MOUSE) from java using jni. (windows vista x64)
all is fine when i register hook from c++ application. strange things start to happen when the hook is registered inside jni.
i implemented a hook proc in 32bit dll, when it is injected into 32bit processes it looks ok, but 64bit applications stop responding. 64bit dll injected into all windows makes 32bit apps inactive for any mouse inputs, similar as in first scenario.

there are no problems with WH_KEYBOARD or WH_SHELL hooks

here is the code:

//dll loaded in jni function
extern "C" __declspec(dllexport) LRESULT HookMouseProc(int nCode,WPARAM wParam,LPARAM lParam)
return CallNextHookEx(NULL, nCode, wParam, lParam);

if(wParam==WM_RBUTTONDOWN && nCode==HC_ACTION)
AfxMessageBox("inside mouse proc");

return CallNextHookEx(NULL, nCode, wParam, lParam);

//jni dll
extern "C" JNIEXPORT jboolean JNICALL Java_winHook_WinHookUtil_setMouseHook(JNIEnv *env, jobject jObject)
HMODULE hModule=LoadLibrary("WinHook32.dll");
void* fnt=GetProcAddress(hModule, "HookMouseProc");

g_hookMouse=SetWindowsHookEx(WH_MOUSE, (HOOKPROC)fnt, hModule, NULL);

return JNI_TRUE;

i have no idea what is wrong. please help.

Edited by: user3530356 on 2010-10-03 16:19


  • EJP
    EJP Member Posts: 32,920 Gold Crown
    all is fine when i register hook from c++ application.
    What about when you register the hook from a DLL? A 32-bit DLL? a 64-bit DLL?

    This is clearly a 32/64 issue. Not a JNI issue.
This discussion has been closed.