title: WH_JOURNALRECORD hook in Windows (C++) - Callback never called.
body: <p>The following code has been giving me some troubles for the past few hours.
  I'm trying to write a small program (based on some tutorials from the web), that uses a WH_JOURNALRECORD windows hook to log keystrokes.</p>
  
  <p>Main code:</p>
  
  <pre><code>#include "StdAfx.h"
  #include &lt;tchar.h&gt;
  #include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  
  using std::cout;
  using std::endl;
  
  int _tmain(int argc, _TCHAR* argv[]) {  
      HINSTANCE hinst = LoadLibrary(_T("testdll3.dll")); 
      typedef void (*Install)();
      typedef void (*Uninstall)();
      Install install = (Install) GetProcAddress(hinst, "install");
      Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
  
      install();
      int foo;
      std::cin &gt;&gt; foo; 
  
      cout &lt;&lt; "Uninstalling" &lt;&lt; endl;
      uninstall();
      return 0;
  }
  </code></pre>
  
  <p>Code of the DLL:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;tchar.h&gt;
  
  HHOOK hhk;
  HHOOK hhk2;
  
  
  LRESULT CALLBACK journalRecordProc(int code, WPARAM wParam, LPARAM lParam) {  
      FILE * fileLog = fopen("journal.txt", "a+");
      fprintf(fileLog,"loggedJournal\n");
      fclose(fileLog);
      CallNextHookEx(hhk,code,wParam,lParam);
      return 0;
  }
  
  
  LRESULT CALLBACK wireKeyboardProc(int code,WPARAM wParam,LPARAM lParam) {  
   FILE * fileLog = fopen("keyboard.txt", "a+");
   fprintf(fileLog,"loggedKeyboard\n");  
   fclose(fileLog);
   CallNextHookEx(hhk,code,wParam,lParam);
   return 0;
  }
  
  extern "C" __declspec(dllexport) void install() {
      HINSTANCE thisDllInstance = LoadLibrary(_T("testdll3.dll"));
      hhk = SetWindowsHookEx(WH_JOURNALRECORD, journalRecordProc, thisDllInstance, NULL);
      hhk2 = SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc, thisDllInstance, NULL);
  }
  extern "C" __declspec(dllexport) void uninstall() {
      UnhookWindowsHookEx(hhk); 
      UnhookWindowsHookEx(hhk2); 
  }
  
  BOOL WINAPI DllMain(  __in  HINSTANCE hinstDLL, __in  DWORD fdwReason, __in  LPVOID lpvReserved) {
   return TRUE;
  }
  </code></pre>
  
  <p>For some reason, the keyboard hook (SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc,..)) works (the 'keyboard.txt' file is created), but the journaling hook (SetWindowsHookEx(WH_JOURNALRECORD, journalRecordProc,...)) doesn't. That is, the callback for the journaling hook is never called (journal.txt file is never created).</p>
  
  <p>I think this might have something to do with Windows' UAC (which I discovered while searching the web), but disabling UAC and running the program with administrative rights didn't help.</p>
  
  <p>I'm not sure what to do now. Can anyone help me?</p>
  
  <p>Thanks</p>
  
  <p>Joris</p>
  
  <p>Additional Info: I'm using Windows 7 + Visual Studio 2010</p>
  
  <p><strong>Edit</strong>:
  It turned out that this was indeed related to access rights. That is, in since Windows Vista, the journal hooks (WH_JOURNALRECORD) are disabled for security reasons (see also <a href="http://www.wintellect.com/CS/blogs/jrobbins/archive/2008/08/30/so-you-want-to-set-a-windows-journal-recording-hook-on-vista-it-s-not-nearly-as-easy-as-you-think.aspx" rel="nofollow noreferrer">this website</a>). In the end, we used a totally different approach to provide similar functionality in our application (which I won't go into detail here, as I'm editing this question 1.5 years after I asked this question and I don't recall all the details of our solution).</p>