这是一个非常有趣的问题。在 native 代码打印其中的一些语句后,我得到了提到的异常。抛出的异常是:
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;
}
可能是什么原因? Output on the CMD
答案 0 :(得分:1)
DllMain
应返回BOOL
,表示它是否成功。来自MSDN:
当系统调用带有
DllMain
值的DLL_PROCESS_ATTACH
函数时,如果成功则返回TRUE
;如果初始化失败,则返回FALSE
。如果在调用FALSE
时返回值为DllMain
,因为该流程使用LoadLibrary
函数,LoadLibrary
会返回NULL
。 (系统立即使用DLL_PROCESS_DETACH
调用您的入口点函数并卸载DLL。)如果在进程初始化期间调用FALSE
时返回值为DllMain
,则进程将终止并显示错误。要获取扩展错误信息,请致电GetLastError
。
您的函数缺少必要的return
语句。