Need Help Making a DLL in C++

Discussion in 'C++' started by fyndr, Aug 31, 2008.

  1. fyndr

    fyndr New Member

    Joined:
    Aug 31, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Firstly, let me say that I have only very limited experience with C++.

    I am writing a program in Java that is supposed to recognize and respond to global input, i.e. keyboard and mouse events, even when the form is not in focus. I have searched for examples of keyboard and mouse hooks for Windows using the Java Native Interface. The only decent example I could find on the internet was at forums.sun.com, where the following code is offered to create a JNI DLL in C++:

    Code:
    // syshook.cpp
    /*
     *	SysHook - 7/17/05
     *	Jacob Gohlke
     *	
     *	JNI Interface for setting a Keyboard Hook and monitoring
     *	it Java-side
     *
     *	(c) Copyright 2005 Jacob Gohlke
     *	
     *	Feel free to use and learn from this code, royalty-free!
     *	I only ask you acknkowlege what library you are using
     *	and who made it. Thanks, and happy hooking!
     */
     
    #include <windows.h>
    #include <winuser.h>
    #include "jni.h"
    #include "syshook.h"
     
    #pragma data_seg(".HOOKDATA") //Shared data among all instances.
    static HHOOK hkb = NULL;
    static HANDLE g_hModule = NULL;
    static WPARAM g_wParam = NULL;
    static LPARAM g_lParam = NULL;
     
    JNIEXPORT void NotifyJava(JNIEnv *env, jobject obj, WPARAM wParam, LPARAM lParam)
    {		
    	jclass cls = env->GetObjectClass(obj);
    	jmethodID mid;
     
    	mid = env->GetMethodID(cls, "Callback", "(ZIZZ)V");
    	if (mid == NULL) 
    			return;
     
    	if( (HIWORD( lParam ) & KF_UP) )
    		env->CallVoidMethod(obj, mid, (jboolean)FALSE, (jint)(wParam), (jboolean)(HIWORD( lParam ) & KF_ALTDOWN), (jboolean)(HIWORD( lParam ) & KF_EXTENDED));
    	else
    		env->CallVoidMethod(obj, mid, (jboolean)TRUE, (jint)(wParam), (jboolean)(HIWORD( lParam ) & KF_ALTDOWN), (jboolean)(HIWORD( lParam ) & KF_EXTENDED));
    }
     
    #pragma data_seg() 
     
    #pragma comment(linker, "/SECTION:.HOOKDATA,RWS")
     
    JNIEXPORT LRESULT CALLBACK HookKeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam) 
    {
        if (nCode < 0)  // do not process message 
    		return CallNextHookEx(hkb, nCode, wParam, lParam); 
     
    	g_wParam = wParam;
    	g_lParam = lParam;
    	return CallNextHookEx(hkb, nCode, wParam, lParam);
    } 
     
    JNIEXPORT void JNICALL Java_PollThread_checkKeyboardChanges(JNIEnv *env, jobject obj)
    {
    	if(g_wParam != NULL && g_lParam != NULL)
    	{
    		NotifyJava(env, obj, g_wParam, g_lParam);
    		g_wParam = NULL;
    		g_lParam = NULL;
    	}
    }
     
    static void Init()
    {
    	hkb = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)HookKeyboardProc, (HINSTANCE)g_hModule, 0 );
    }
     
    static void Cleanup()
    {
    	if( hkb != NULL )
    		UnhookWindowsHookEx( hkb );
     
    	hkb = NULL;
    }
     
    BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
    {
    	switch(ul_reason_for_call)
    	{
    		case DLL_PROCESS_ATTACH:
    			g_hModule = hModule;
    			Init();
    			return TRUE;
     
    		case DLL_PROCESS_DETACH:
    			Cleanup();
    			return TRUE;
    	}
     
        return TRUE;
    }
    

    Code:
    // syshook.h
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include "jni.h"
    /* Header for class PollThread */
     
    #ifndef _Included_PollThread
    #define _Included_PollThread
    #ifdef __cplusplus
    extern "C" {
    #endif
    #undef PollThread_MIN_PRIORITY
    #define PollThread_MIN_PRIORITY 1L
    #undef PollThread_NORM_PRIORITY
    #define PollThread_NORM_PRIORITY 5L
    #undef PollThread_MAX_PRIORITY
    #define PollThread_MAX_PRIORITY 10L
    /*
     * Class:     PollThread
     * Method:    checkKeyboardChanges
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_PollThread_checkKeyboardChanges
      (JNIEnv *, jobject);
     
    #ifdef __cplusplus
    }
    #endif
    #endif
    
    Unfortunately, I have not even the slightest clue as to how to create a DLL in C++. Does anyone know of any step-by-step guides to create a DLL with the following code for Microsoft Visual C++ 2008 Express Edition, preferably written with C++-illiterate people in mind? Alternatively, could someone create a DLL with the given C++ code? I know it is a bit of an inconvenience, but I have been searching the Web for days and found nothing that can assist me.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice