title: C - win32: AttachThreadInput & SetFocus, 64bit: no clue
body: <p>I've commented in most of what I know. I'm fairly confident the issue arises at AttachThreadInput. It was only designed to function under 32bit, I think. Believe me, if I could solve this myself, I would be happy to. I read the full documentation for events in Windows (<a href="http://msdn.microsoft.com/en-us/library/ms644927" rel="nofollow">here</a>) and I'm no closer to a solution. If you have any ideas I would love to hear them.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  
  int main()
  {
      //Structure prereqs for CreateProcess
      STARTUPINFO         siStartupInfo;
      PROCESS_INFORMATION piProcessInfo;
      memset(&amp;siStartupInfo, 0, sizeof(siStartupInfo));
      memset(&amp;piProcessInfo, 0, sizeof(piProcessInfo));
      siStartupInfo.cb = sizeof(siStartupInfo);
  
      if(CreateProcess("c:\\windows\\notepad.exe", "", 0, 0, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, &amp;siStartupInfo, &amp;piProcessInfo) == FALSE)
      {
          GetLastError();
      }
      Sleep(1000);
  
      //Target thread, I can't seem to get this to return anything !0
      DWORD dwTargetThread = GetWindowThreadProcessId(piProcessInfo.hProcess,NULL);
      //For example:
      //if(dwTargetThread == 0) return -1;
  
      //Print debugging info
      if (GetCurrentThreadId() == dwTargetThread) return -1; else printf("\nMy thread: %u\n\npiProcessInfo.hThread: %u\n\nDWORD dwTargetThread: %u\n\nunsigned int dwTargetThread: %u", GetCurrentThreadId(), piProcessInfo.hThread,dwTargetThread, GetWindowThreadProcessId(piProcessInfo.hProcess,NULL));
  
      //I've tried using piProcessInfo.hThread for AttachTo but I can't cast it to a DWORD as it's 64bit
      AttachThreadInput(GetCurrentThreadId(),dwTargetThread,TRUE);
      printf("\n\nAttached...\n");
      Sleep(1000);
  
      //Set the focus &amp; bring to foreground
      SetFocus(piProcessInfo.hProcess);
      printf("Focus set...\n");
      Sleep(1000);
      SetForegroundWindow(piProcessInfo.hProcess);
      printf("Brought to foreground...\n");
      Sleep(1000);
  
      //I know I shouldn't use PostMessage for keyboard input but it's just for the example
      PostMessage(piProcessInfo.hProcess, WM_CHAR,  'g', 0);
      printf("Message queued\n");
  
      //No better than SetForegroundWindow:
      //SetWindowPos(piProcessInfo.hProcess, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  }
  </code></pre>