title: OpenProcessToken failing with Access Denied (5) on Windows XP
body: <p>My prototype code works fine on every Windows OS I've been able to test on, except Windows XP.  </p>
  
  <p>When I run this on Windows XP as Administrator, I'm given <em>Access is denied (5)</em> when calling <em>OpenProcessToken</em>.  </p>
  
  <p>Is there some difference I'm not aware of?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;Windows.h&gt;
  #include &lt;userenv.h&gt;
  
  #pragma comment(lib, "userenv")
  
  void DisplayError(LPWSTR pszAPI)
  {
      LPVOID lpvMessageBuffer;
  
      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
          FORMAT_MESSAGE_FROM_SYSTEM,
          NULL, GetLastError(),
          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
          (LPWSTR)&amp;lpvMessageBuffer, 0, NULL);
  
      wprintf(L"ERROR: API        = %s.\n", pszAPI);
      wprintf(L"       error code = %d.\n", GetLastError());
      wprintf(L"       message    = %s.\n", (LPWSTR)lpvMessageBuffer);
  
      LocalFree(lpvMessageBuffer);
  
      ExitProcess(GetLastError());
  }
  
  void SetDebugPrivileges()
  {
      void* tokenHandle;
  
      TOKEN_PRIVILEGES privilegeToken;
      LookupPrivilegeValue(0, SE_DEBUG_NAME, &amp;privilegeToken.Privileges[0].Luid);
      privilegeToken.PrivilegeCount = 1;
      privilegeToken.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
      OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;tokenHandle);
      AdjustTokenPrivileges(tokenHandle, 0, &amp;privilegeToken, sizeof(TOKEN_PRIVILEGES), 0, 0);
      CloseHandle(tokenHandle);
  }
  
  void wmain(int argc, WCHAR *argv[])
  {
      DWORD     dwSize;
      HANDLE    hToken;
      LPVOID    lpvEnv;
      PROCESS_INFORMATION pi = { 0 };
      STARTUPINFO         si = { 0 };
      WCHAR               szUserProfile[256] = L"";
  
      si.cb = sizeof(STARTUPINFO);
  
      if (argc != 4)
      {
          wprintf(L"Usage: %s [user@domain] [password] [cmd]", argv[0]);
          wprintf(L"\n\n");
          return;
      }
  
      if (!LogonUser(argv[1], NULL, argv[2], LOGON32_LOGON_INTERACTIVE,
          LOGON32_PROVIDER_DEFAULT, &amp;hToken))
          DisplayError(L"LogonUser");
  
      if (!CreateEnvironmentBlock(&amp;lpvEnv, hToken, TRUE))
          DisplayError(L"CreateEnvironmentBlock");
  
      dwSize = sizeof(szUserProfile) / sizeof(WCHAR);
  
      if (!GetUserProfileDirectory(hToken, szUserProfile, &amp;dwSize))
          DisplayError(L"GetUserProfileDirectory");
  
      if (!CreateProcessWithLogonW(argv[1], NULL, argv[2],
          LOGON_WITH_PROFILE, NULL, argv[3],
          CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile,
          &amp;si, &amp;pi))
          DisplayError(L"CreateProcessWithLogonW");
  
      if (!DestroyEnvironmentBlock(lpvEnv))
          DisplayError(L"DestroyEnvironmentBlock");
  
      //Sleep(5000);
  
      SetDebugPrivileges();
  
      HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pi.dwProcessId);
      if(process == NULL)
          DisplayError(L"OpenProcess");
  
      // Not working on Windows XP
      HANDLE token;
      if(!OpenProcessToken(process, TOKEN_QUERY, &amp;token))
          DisplayError(L"OpenProcessToken");
  
      CloseHandle(token);
      CloseHandle(process);
      CloseHandle(hToken);
      CloseHandle(pi.hProcess);
      CloseHandle(pi.hThread);
  }
  </code></pre>