title: Exception in thread "main" java.lang.UnsatisfiedLinkError : A dynamic link library (DLL) initialization routine failed
body: <p>This is a very interesting problem. I get the mentioned exception after the <em>native</em> code prints some of it's statements. The exception thrown is :</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The first two statements <code>hookThreadHandle is not NULL</code> is the one from the <code>dll</code> whose initialization failed ! Why does it throw the exception after actually finding the <code>dll</code> and then printing the statements. ?</p>
  
  <p>And what is <code>A dynamic link library (DLL) initialization routine failed</code> exception ? What does it signify ?</p>
  
  <p><em>Java Code :</em></p>
  
  <pre><code>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();       
   }
  }
  </code></pre>
  
  <p><em>C Code :</em></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;w32api.h&gt;
  #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 = &amp;installHook;
    hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &amp;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(&amp;msg, NULL, 0, 0))
  {
    TranslateMessage(&amp;msg);
    DispatchMessage(&amp;msg);
  }
  return msg.wParam;
  </code></pre>
  
  <p>}</p>
  
  <p>What could be the reason ? <a href="http://i49.tinypic.com/23ldvg5.jpg" rel="nofollow">Output on the CMD</a></p>