Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

MouseHook not working correctly

Hello. I'm trying to write an application, which detects whether mouse button was released in some time period. It's called from Java through JNI, but I am wondering if this C++ code is correct before I blame Java/JNI. I have this code:

[code]#define _WIN32_WINNT 0x0500
#include
#include
#include

#include "jni.h"
#include "GlobalKeyListener.h"

static HHOOK MouseHook = NULL;
static HANDLE g_hModule = NULL;
FILE *fp = fopen("C:\projekt1_print2.txt", "w");
bool released = FALSE;


LRESULT CALLBACK HookMouseProc(int nCode, WPARAM wParam, LPARAM lParam){
PKBDLLHOOKSTRUCT k = (PKBDLLHOOKSTRUCT)(lParam);
fprintf(fp, "HookMouseProc spusten
");
if(wParam == WM_LBUTTONUP)
{
fprintf(fp, "RELEASE BUTTONU
");
released = TRUE;
}
return CallNextHookEx(MouseHook,nCode,wParam,lParam);
}

static void Cleanup()
{
fprintf(fp, "Cleanup spusten
");
if (MouseHook != NULL)
UnhookWindowsHookEx(MouseHook);
MouseHook = NULL;
}

JNIEXPORT void JNICALL Java_GlobalKeyListener_StayAlive(JNIEnv *env, jobject obj) {
fprintf(fp, "MesageLoop pres JNI spusten
");
MSG message;
while(GetMessage(&message,NULL,0,0)){
TranslateMessage(&message);
DispatchMessage(&message);
}
}

DWORD WINAPI StayAlive(LPVOID lpParameter) {
fprintf(fp, "MesageLoop spusten
");
MSG message;
while(GetMessage(&message,NULL,0,0)){
TranslateMessage(&message);
DispatchMessage(&message);
}
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
fprintf(fp, "DllMain spusten
");
SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);

switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (g_hModule == NULL) {
g_hModule = hModule;
DWORD ThreadId;
int *lpArgPtr;

lpArgPtr = (int *)malloc(sizeof(int));
CreateThread(NULL,0,StayAlive,lpArgPtr,0,&ThreadId);
return TRUE;
}
case DLL_PROCESS_DETACH:
Cleanup();
return TRUE;
}
return TRUE;
}



JNIEXPORT jboolean JNICALL Java_GlobalKeyListener_WasButtonReleased(JNIEnv *env, jobject obj)
{
fprintf(fp, "WasButtonReleased
");
return released;
}

JNIEXPORT void JNICALL Java_GlobalKeyListener_StartMonitoring(JNIEnv *env, jobject obj)
{
if (MouseHook == NULL) {
MouseHook = SetWindowsHookEx(WH_MOUSE, HookMouseProc, (HINSTANCE)g_hModule, 0);
}
fprintf(fp, "StartMonitoring
");
released = FALSE;
}

JNIEXPORT void JNICALL Java_GlobalKeyListener_Destroy(JNIEnv *env, jobject obj)
{
fprintf(fp, "Javove volan cleanup
");
Cleanup();
fclose(fp);
}

[/code]

The problem is, that CallNextHookEx doesn't seem to be working - mouse event is catched by my application, but it is not passed to the original program (any element of hook chain), so clicks does nothing. I also tried to replace WH_MOUSE for WH_MOUSE_LL, but it's not working at all - HookMouseProc is not even called, so it's even worse than WH_MOUSE.
Sorry about my ugly code - I've never done any C/C++ application.
Thak you for your time.

Cicin
Sign In or Register to comment.