1

这是一个非常有趣的问题。在本机代码打印它的一些语句后,我得到了提到的异常。抛出的异常是:

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 NULLdll初始化失败的语句!为什么它在实际找到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 上的输出

4

1 回答 1

1

DllMain应该返回一个BOOL指示它是否成功的。来自MSDN

当系统DllMain使用该值调用函数时,如果成功或初始化失败DLL_PROCESS_ATTACH,则函数返回。如果返回值是when因为进程使用函数而被调用,则返回。(系统会立即使用您的入口点函数调用并卸载 DLL。)如果返回值是在进程初始化期间调用的when ,则进程会因错误而终止。要获取扩展的错误信息,请调用。TRUEFALSEFALSEDllMainLoadLibraryLoadLibraryNULLDLL_PROCESS_DETACHFALSEDllMainGetLastError

您的函数缺少必要的return语句。

于 2012-05-30T10:50:59.953 回答