title: How can I set cursor position from mouse hook in Windows?
body: <p>I'm trying to create a little app that will detect when the mouse cursor moves to the edge of the screen and will move it to the opposite edge, to create a continuous desktop effect, if that makes sense.</p>
  
  <p>Below is some code from someone else (the mouse hook part) I adapted by added the SetCursorPos to move the mouse to a fixed position for now.  When I run it, SetCursorPos returns true, which I assume means the call succeeded, but the mouse does not move.  I read somewhere something about security constraints in later Windows version preventing stuff like this, which would make sense, but the source was unclear to how true this is.  Does anyone know why this would not work?</p>
  
  <p>Thanks, code below:</p>
  
  <pre><code>#define _WIN32_WINNT 0x0400
  #pragma comment( lib, "user32.lib" )
  
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  
  HHOOK hMouseHook;
  
  __declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
  {
      MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
      if (pMouseStruct != NULL)
      {
          if (pMouseStruct-&gt;pt.x &lt; -1900)
          {
              BOOL r = SetCursorPos(
                  500,
                  500
              );
  
              printf("Trigger %d.  Response %d", pMouseStruct-&gt;pt.x, r);
          }
      }
  
      return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
  }
  
  void MessageLoop()
  {
      MSG message;
      while (GetMessage(&amp;message,NULL,0,0)) {
          TranslateMessage( &amp;message );
          DispatchMessage( &amp;message );
      }
  }
  
  DWORD WINAPI MyMouseLogger(LPVOID lpParm)
  {
      HINSTANCE hInstance = GetModuleHandle(NULL);
      if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm); 
      if (!hInstance) return 1;
  
      hMouseHook = SetWindowsHookEx (  
          WH_MOUSE_LL,
          (HOOKPROC) KeyboardEvent,  
          hInstance,                 
          NULL                       
          );
      MessageLoop();
      UnhookWindowsHookEx(hMouseHook);
      return 0;
  }
  
  int main(int argc, char** argv)
  {
      HANDLE hThread;
      DWORD dwThread;
  
      hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
          MyMouseLogger, (LPVOID) argv[0], NULL, &amp;dwThread);
      if (hThread)
          return WaitForSingleObject(hThread,INFINITE);
      else return 1;
  }
  </code></pre>