#include <windows.h>
#include "SysHook.h"
#include <jni.h>
HINSTANCEhInst = NULL;
JavaVM * jvm = NULL;
jobject hookObj_kb = NULL;
jobject hookObj_ms = NULL;
jobject g_kl = NULL;
postthreadmessagejmethodID processKeyID_kb = NULL;
jmethodID processKeyID_ms = NULL;
DWORDhookThreadId = 0;
LONGg_mouseLocX = -1;// x-location of mouse position
LONGg_mouseLocY = -1;// y-location of mouse position
extern"C"
BOOLAPIENTRY DllMain(HINSTANCE_hInst,DWORDreason,LPVOIDreserved)
{
switch(reason)
{
caseDLL_PROCESS_ATTACH:
printf("C++: DllMain - DLL_PROCESS_ATTACH.\n");
hInst = _hInst;
break;
default:
break;
}
returnTRUE;
}
LRESULTCALLBACK MouseTracker(intnCode,WPARAMwParam,LPARAMlParam)
{
JNIEnv * env;
KBDLLHOOKSTRUCT * p = (KBDLLHOOKSTRUCT *)lParam;
if(jvm->AttachCurrentThread((void**)&env, NULL) >= 0)
{
if(nCode==HC_ACTION)
{
MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lParam;
if(pStruct->pt.x != g_mouseLocX || pStruct->pt.y != g_mouseLocY)
{
env->CallVoidMethod(hookObj_ms, processKeyID_ms, (jint)pStruct->pt.x,(jint)pStruct->pt.y, g_kl);
g_mouseLocX = pStruct->pt.x;
g_mouseLocY = pStruct->pt.y;
}
}
}
else
{
printf("C++: LowLevelKeyboardProc - Error on the attach current thread.\n");
}
returnCallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULTCALLBACK LowLevelKeyboardProc(intnCode,WPARAMwParam,LPARAMlParam)
{
JNIEnv * env;
KBDLLHOOKSTRUCT * p = (KBDLLHOOKSTRUCT *)lParam;
if(jvm->AttachCurrentThread((void**)&env, NULL) >= 0)
{
switch(wParam)
{
caseWM_KEYDOWN:
caseWM_SYSKEYDOWN:
env->CallVoidMethod(hookObj_kb, processKeyID_kb, (jboolean)TRUE, p->vkCode,g_kl);
break;
caseWM_KEYUP:
caseWM_SYSKEYUP:
env->CallVoidMethod(hookObj_kb, processKeyID_kb, (jboolean)FALSE, p->vkCode,g_kl);
break;
default:
break;
}
}
else
{
printf("C++: LowLevelKeyboardProc - Error on the attach current thread.\n");
}
returnCallNextHookEx(NULL, nCode, wParam, lParam);
}
voidMsgLoop()
{
MSG message;
while(GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
JNIEXPORTvoidJNICALL Java_SysHook_registerHook(JNIEnv * env, jobject obj,jobject kl)
{
HHOOKhookHandle_ms = SetWindowsHookEx(WH_MOUSE_LL, MouseTracker, hInst, 0);
HHOOKhookHandle_kb = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInst, 0);
g_kl = kl;
if(hookHandle_ms == NULL)
{
printf("C++: Java_SysHook_registerKeyHook - Hook failed!\n");
return;
}
else
{
printf("C++: Java_SysHook_registerKeyHook - Hook successful\n");
}
if(hookHandle_kb == NULL)
{
printf("C++: Java_SysHook_registerKeyHook - Hook failed!\n");
return;
}
else
{
printf("C++: Java_SysHook_registerKeyHook - Hook successful\n");
}
hookObj_kb = env->NewGlobalRef(obj);
jclass cls_kb = env->GetObjectClass(hookObj_kb);
processKeyID_kb = env->GetMethodID(cls_kb,"processKey","(ZILGlobalEventListener;)V");
hookObj_ms = env->NewGlobalRef(obj);
jclass cls_ms = env->GetObjectClass(hookObj_ms);
processKeyID_ms = env->GetMethodID(cls_ms,"mouseMoved","(IILGlobalEventListener;)V");
env->GetJavaVM(&jvm);
hookThreadId = GetCurrentThreadId();
MsgLoop();
if(!UnhookWindowsHookEx(hookHandle_kb))
{
printf("C++: Java_SysHook_registerKeyHook - Unhook failed\n");
}
else
{
printf("C++: Java_SysHook_registerKeyHook - Unhook successful\n");
}
if(!UnhookWindowsHookEx(hookHandle_ms))
{
printf("C++: Java_SysHook_registerKeyHook - Unhook failed\n");
}
else
{
printf("C++: Java_SysHook_registerKeyHook - Unhook successful\n");
}
}
JNIEXPORTvoidJNICALL Java_SysHook_unRegisterHook(JNIEnv *env, jobject object)
{
if(hookThreadId == 0)
return;
printf("C++: Java_SysHook_unRegisterKeyHook - call PostThreadMessage.\n");
PostThreadMessage(hookThreadId, WM_QUIT, 0, 0L);
}
SysHook.h:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。