dcsimg
Problem with Hooks
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   yan_alvan
Posted On:   Friday, September 19, 2003 03:31 AM

Hi all, I need your help. I have a DLL written in VC++. The DLL is to get mouse and keyboard event in all application running or screen. The DLL was work find if I'm using it in VC++ application. But when I tried in java application, the DLL couldn't work as I wanted. The DLL only capture mouse and keyboard action in java application not in other application or screen. My guess is there is something should to set up in DLL (program) so the JAVA can use it. Here is the DLL program I used to: ------------------------------------------------------------ IdleTracker.cpp ------------------------------------------------------------ /** * IdleTracker - a D   More>>

Hi all, I need your help. I have a DLL written in VC++. The DLL is to get mouse and keyboard event in all application running or screen. The DLL was work find if I'm using it in VC++ application.


But when I tried in java application, the DLL couldn't work as I wanted. The DLL only capture mouse and keyboard action in java application not in other application or screen.


My guess is there is something should to set up in DLL (program) so the JAVA can use it. Here is the DLL program I used to:



			
------------------------------------------------------------
IdleTracker.cpp
------------------------------------------------------------

/**
* IdleTracker - a DLL that tracks the user's idle input time
* system-wide.
*
* Usage
* =====
* - call IdleTrackerInit() when you want to start monitoring.
* - call IdleTrackerTerm() when you want to stop monitoring.
* - to get the time past since last user input, do the following:
* GetTickCount() - IdleTrackerGetLastTickCount()
*
* Author: Sidney Chong
* Date: 25/5/2000
* Version: 1.0
**/

#include
#include
#include "jni.h"
#include "movieloop.h"
/**
* The following global data is SHARED among all instances of the DLL
* (processes); i.e., these are system-wide globals.
**/
#pragma data_seg(".IdleTracker") // you must define as SHARED in .def
HHOOK g_hHkKeyboard = NULL; // handle to the keyboard hook
HHOOK g_hHkMouse = NULL; // handle to the mouse hook
DWORD g_dwLastTick = 0; // tick time of last input event
LONG g_mouseLocX = -1; // x-location of mouse position
LONG g_mouseLocY = -1; // y-location of mouse position
#pragma data_seg()
#pragma comment(linker, "/section:.IdleTracker,rws")

HINSTANCE g_hInstance = NULL; // global instance handle

/**
* Get tick count of last keyboard or mouse event
**/
__declspec(dllexport) DWORD IdleUIGetLastInputTime()
{
return g_dwLastTick;
}

/**
* Keyboard hook: record tick count
**/
LRESULT CALLBACK KeyboardTracker(int code, WPARAM wParam, LPARAM lParam) {
printf("keyboard gerak ");
if (code==HC_ACTION) {
g_dwLastTick = GetTickCount();
printf("now
");
}
return ::CallNextHookEx(g_hHkKeyboard, code, wParam, lParam); }

/**
* Mouse hook: record tick count
**/
LRESULT CALLBACK MouseTracker(int code, WPARAM wParam, LPARAM lParam) {
printf("mouse gerak abis ");
if (code==HC_ACTION) {
MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lParam;
//we will assume that any mouse msg with the same locations as spurious
if (pStruct->pt.x != g_mouseLocX || pStruct->pt.y != g_mouseLocY)
{
g_mouseLocX = pStruct->pt.x;
g_mouseLocY = pStruct->pt.y;
g_dwLastTick = GetTickCount();
}
printf("now
");
}
return ::CallNextHookEx(g_hHkMouse, code, wParam, lParam);
}

/**
* Initialize DLL: install kbd/mouse hooks.
**/
__declspec(dllexport) BOOL IdleUIInit()
{
if (g_hHkKeyboard == NULL) {
g_hHkKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardTracker, g_hInstance, 0);
}
if (g_hHkMouse == NULL) {
g_hHkMouse = SetWindowsHookEx(WH_MOUSE, MouseTracker, g_hInstance, 0);
}

_ASSERT(g_hHkKeyboard);
_ASSERT(g_hHkMouse);

g_dwLastTick = GetTickCount(); // init count

if (!g_hHkKeyboard || !g_hHkMouse)
return FALSE;
else
return TRUE;
}

/**
* Terminate DLL: remove hooks.
**/
__declspec(dllexport) void IdleUITerm()
{
BOOL bResult;
if (g_hHkKeyboard)
{
bResult = UnhookWindowsHookEx(g_hHkKeyboard);
_ASSERT(bResult);
g_hHkKeyboard = NULL;
}
if (g_hHkMouse)
{
bResult = UnhookWindowsHookEx(g_hHkMouse);
_ASSERT(bResult);
g_hHkMouse = NULL;
}
}

/**
* DLL's entry point
**/
int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
switch(dwReason) {
case DLL_PROCESS_ATTACH:
//DisableThreadLibraryCalls(hInstance);
//g_hInstance = hInstance;
//g_hInstance = NULL;
g_hInstance =(HINSTANCE)GetModuleHandle(NULL);
break;
case DLL_PROCESS_DETACH:
//we do an unhook here just in case the user has forgotten.
IdleUITerm();
break;
}
return TRUE;
}

/**
* This is to prevent the CRT from loading, thus making this a smaller
* and faster dll.
**/
extern "C" BOOL __stdcall _DllMainCRTStartup( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
return DllMain( hinstDLL, fdwReason, lpvReserved );
}


JNIEXPORT jint JNICALL Java_MovieLoop_isKeyMouseIdle(JNIEnv *, jobject) {
return (GetTickCount()-IdleUIGetLastInputTime())/1000;
}
JNIEXPORT jboolean JNICALL Java_MovieLoop_idleUIInit(JNIEnv *, jobject) {
printf("masuk inisitalisasi %s %s", g_hHkKeyboard, g_hHkMouse);
//g_hInstance=1;
if (g_hHkKeyboard == NULL) {
g_hHkKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardTracker, g_hInstance, 0);
}
if (g_hHkMouse == NULL) {
g_hHkMouse = SetWindowsHookEx(WH_MOUSE, MouseTracker, g_hInstance, 0);
}

_ASSERT(g_hHkKeyboard);
_ASSERT(g_hHkMouse);

g_dwLastTick = GetTickCount(); // init count

if (!g_hHkKeyboard || !g_hHkMouse)
return FALSE;
else
return TRUE;
}
JNIEXPORT void JNICALL Java_MovieLoop_IdleUITerm(JNIEnv *, jobject) {
printf("keluar deh ...
");
BOOL bResult;
if (g_hHkKeyboard)
{
bResult = UnhookWindowsHookEx(g_hHkKeyboard);
_ASSERT(bResult);
g_hHkKeyboard = NULL;
}
if (g_hHkMouse)
{
bResult = UnhookWindowsHookEx(g_hHkMouse);
_ASSERT(bResult);
g_hHkMouse = NULL;
}
}
JNIEXPORT jboolean JNICALL Java_MovieLoop_isIdleNow(JNIEnv *,jobject)
{
return true;
}

JNIEXPORT jint JNICALL Java_TestMouse_isKeyMouseIdle(JNIEnv *, jobject) {
return (GetTickCount()-IdleUIGetLastInputTime())/1000;
}
JNIEXPORT jboolean JNICALL Java_TestMouse_idleUIInit(JNIEnv *, jobject) {
printf("masuk inisitalisasi %s %s", g_hHkKeyboard, g_hHkMouse);
if (g_hHkKeyboard == NULL) {
g_hHkKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardTracker, g_hInstance, 0);
}
if (g_hHkMouse == NULL) {
g_hHkMouse = SetWindowsHookEx(WH_MOUSE, MouseTracker, g_hInstance, 0);
}

_ASSERT(g_hHkKeyboard);
_ASSERT(g_hHkMouse);

g_dwLastTick = GetTickCount(); // init count

if (!g_hHkKeyboard || !g_hHkMouse)
return FALSE;
else
return TRUE;
}
JNIEXPORT void JNICALL Java_TestMouse_IdleUITerm(JNIEnv *, jobject) {
printf("keluar deh ...
");
BOOL bResult;
if (g_hHkKeyboard)
{
bResult = UnhookWindowsHookEx(g_hHkKeyboard);
_ASSERT(bResult);
g_hHkKeyboard = NULL;
}
if (g_hHkMouse)
{
bResult = UnhookWindowsHookEx(g_hHkMouse);
_ASSERT(bResult);
g_hHkMouse = NULL;
}
}

JNIEXPORT jboolean JNICALL Java_TestMouse_isIdleNow(JNIEnv *,jobject)
{
return true;
}

-----------------------------------------------------------------------------------------
IdleTracker.h
-----------------------------------------------------------------------------------------
/**
* IdleTracker - a DLL that tracks the user's idle input time
* system-wide.
*
* Usage
* =====
* - call IdleTrackerInit() when you want to start monitoring.
* - call IdleTrackerTerm() when you want to stop monitoring.
* - to get the time past since last user input, do the following:
* GetTickCount() - IdleTrackerGetLastTickCount()
*
* Author: Sidney Chong
* Date: 25/5/2000
* Version: 1.0
**/

__declspec(dllimport) BOOL IdleUIInit();
__declspec(dllimport) void IdleUITerm();
__declspec(dllimport) DWORD IdleUIGetLastInputTime();

-----------------------------------------------------------------------------------------
IdleTracker.def
-----------------------------------------------------------------------------------------
; IdleTracker.def : Declares the module parameters for the DLL.

LIBRARY "IdleTracker"
DESCRIPTION 'IdleTracker Windows Dynamic Link Library'

EXPORTS
; Explicit exports can go here
SECTIONS .IdleTracker READ WRITE SHARED

   <<Less
About | Sitemap | Contact