这是一个非常有趣的问题。在本机代码打印它的一些语句后,我得到了提到的异常。抛出的异常是:
hookThreadHandle is not NULL
hookThreadHandle is not NULL
Exception in thread "main" java.lang.UnsatisfiedLinkError: W:\elita\jnitesters\
workspace\c\MyHook\bin\Debug\MyHook.dll: A dynamic link library (DLL) initialization r
outine failed
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at keylogger.TestKeys.main(TestKeys.java:12)
前两个语句hookThreadHandle is not NULL
是dll
初始化失败的语句!为什么它在实际找到dll
然后打印语句后抛出异常。?
什么是A dynamic link library (DLL) initialization routine failed
异常?它意味着什么?
Java 代码:
package keylogger;
public class TestKeys {
private static int i = 0;
private native void setWinHook();
private native void unregisterWinHook();
public static void main(String args[]) {
//TestKeys o = new TestKeys();
System.loadLibrary("MyHook"); // load the library that registers the hook
Runnable r = new Runnable() {
@Override
public void run() {
try {
System.out.println("Sleeping for 10 seconds..");
Thread.sleep(10000);
}catch(Exception exc) {
exc.printStackTrace();
}
}
};
new Thread(r,"new thread").start();
}
}
C代码:
#include <stdio.h>
#include <windows.h>
#include <w32api.h>
#include "keylogger_TestKeys.h"
static HHOOK handleKeyboardHook = NULL;
HINSTANCE hInst = NULL;
static DWORD hookThreadId = 0;
static HANDLE hookThreadHandle = NULL;
BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved);
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
printf("You pressed the key !\n");
return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
}
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
LPTHREAD_START_ROUTINE lpStartAddress = &installHook;
hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &hookThreadId);
if(hookThreadHandle == NULL) {
printf("\nhookThreadHandle is NULL\n");
} else {
printf("\nhookThreadHandle is not NULL\n");
}
}
JNIEXPORT void JNICALL Java_keylogger_TestKeys_unregisterWinHook
(JNIEnv *env, jobject obj) {
if(handleKeyboardHook != NULL) {
UnhookWindowsHookEx(handleKeyboardHook);
printf("Keyboard hook successfully unregistered !");
} else {
printf("Coudn't Unhook the keyboard hook !");
}
}
BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
可能是什么原因 ?CMD 上的输出