title: hooking to modify keyboard input
body: <p>I'm trying to write a DLL which intercepts keyboard input and modifies the scan/vkey codes prior to the values being presented to the process event loop. The simplified dll code (generated by VS 2010):</p>
  
  <pre><code>#include "stdafx.h"
  #include "hookdll.h"
  
  
  #ifdef _DEBUG
  #define new DEBUG_NEW
  #endif
  
  static const char *LogFileName = "c:/tmp/hook_report.txt";
  #define LogOpen() \
  {\
          freopen(LogFileName, "w", stdout); \
  }
  #define Log(fmt, ...)   if ( 1 )\
  {\
          fprintf(stdout, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__);  \
          fflush(stdout); \
  }
  
  
  static HHOOK HookHandle;
  
  char*
  DbHookToString(LPARAM lParam)
  {
      static char tmp[128];
      KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
              sprintf(tmp,
              "vkCode=0x%02X"
              ", scanCode=0x%02X"
              ", flags=0x%08X"
              ", time=%d"
              ", dwExtraInfo=0x%08X",
              hs-&gt;vkCode
              , hs-&gt;scanCode
              , hs-&gt;flags
              , hs-&gt;time
              , hs-&gt;dwExtraInfo);
      return tmp;
  }
  
  
  LRESULT CALLBACK
  keyHandler(int nCode, WPARAM wParam, LPARAM lParam) 
  {
      // Checks whether params contain action about keystroke
      if(nCode == HC_ACTION)
      {
          KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
          Log("IN:  %d: %s", wParam, DbHookToString(lParam));
          switch(hs-&gt;vkCode)
          {
              default:
                  break;
              case 0x31:
                  ++hs-&gt;vkCode;
                  ++hs-&gt;scanCode;
                  break;
              case 0x32:
                  //--hs-&gt;vkCode;
                  //--hs-&gt;scanCode;
                  break;
          }
          Log("OUT: %d: %s\n", wParam, DbHookToString(lParam));
      }
  
      return CallNextHookEx(HookHandle, nCode, wParam, lParam);
  }
  
  
  BEGIN_MESSAGE_MAP(ChookdllApp, CWinApp)
  END_MESSAGE_MAP()
  
  
  ChookdllApp::ChookdllApp()
  {
  }
  
  
  
  ChookdllApp theApp;
  
  
  
  BOOL ChookdllApp::InitInstance()
  {
      LogOpen();
  
      CWinApp::InitInstance();
  
      if (!AfxSocketInit())
      {
          Log("AfxSocketInit failed");
          return FALSE;
      }
  
      HookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, NULL, 0);
      if(HookHandle == NULL) 
      {
          Log("SetWindowsHookEx failed");
          return FALSE;
      }
  
      return TRUE;
  }
  </code></pre>
  
  <p>The app code, in skeleton form, is a C++ windows form app containing a single text box in a single form; problem is the input characters, specifically the number '1' key, are coming through unaltered even though the DLL is reporting in a log file that it is being modified. Help anyone?</p>
  
  <blockquote>
    <p>...hookdll.cpp:84 IN:  256: vkCode=0x31, scanCode=0x02, flags=0x00000000, time=11326265, dwExtraInfo=0x00000000
    ...\hookdll.cpp:100 OUT: 256: vkCode=0x32, scanCode=0x03, flags=0x00000000, time=11326265, dwExtraInfo=0x00000000</p>
    
    <p>...\hookdll.cpp:84 IN:  257: vkCode=0x31, scanCode=0x02, flags=0x00000080, time=11326374, dwExtraInfo=0x00000000
    ...\hookdll.cpp:100 OUT: 257: vkCode=0x32, scanCode=0x03, flags=0x00000080, time=11326374, dwExtraInfo=0x00000000</p>
  </blockquote>
  
  <p>...snip..</p>
  
  <pre><code>private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
  {
      HINSTANCE hinstDLL = LoadLibrary("hookdll.dll");
      if ( hinstDLL == NULL )
      {
          volatile int err = GetLastError();
          err += 1;
      }
  }
  </code></pre>
  
  <p>...snip...
  Many thanks.</p>