title: Desktop problem with using CreateProcessAsUser from a service on Vista
body: <p>I am using VC++ to create a process from a service on Vista using CreateProcessAsUser. The process creation is succeeding when i execute this code by login on the machine locally. The same code is failing when I am using Remote Desktop (mstsc) to login in to my machine from another machine and launch my App.</p>
  
  <p>I can understand that when I do remote login the active desktop is taken as that of Remote Desktop. Can some one help me how to get the desktop name that of currently login users desktop and not that of Remote desktop.</p>
  
  <p>Here is my code</p>
  
  <pre><code>ACTIVECONSOLESESSIONIDFUNC lpfnProc;    // WTSGetActiveConsoleSessionId function pointer
  HMODULE hModule = NULL;         // Instance for kernel32.dll library
  DWORD dwSessionId = 0;          // Session ID
  HANDLE hToken = NULL;           // Active session token
  HANDLE hDupToken = NULL;        // Duplicate session token
  WCHAR szErr[1024] = {0};
  
  LPVOID lpEnvironment = NULL;            // Environtment block
  
  // Get the active session ID
  hModule = LoadLibrary(KERNEL32LIB);
  if(!hModule)
  {
     //wsprintf(szErr, L"LoadLibrary Error: %d", GetLastError());
     return;
  }
  
  lpfnProc = (ACTIVECONSOLESESSIONIDFUNC)GetProcAddress(hModule,"WTSGetActiveConsoleSessionId"); 
  
  dwSessionId = lpfnProc();
  
  // Get token of the logged in user by the active session ID 
  BOOL bRet = WTSQueryUserToken(dwSessionId, &amp;hToken);
  
  if (!bRet)
  {       
      //wsprintf(szErr, L"WTSQueryUserToken Error: %d", GetLastError());
      return;
  }
  
  // Get duplicate token from the active logged in user's token
  bRet = DuplicateTokenEx(hToken,     // Active session token
                   MAXIMUM_ALLOWED,           // Desired access
                           NULL,                      // Token attributes                                         
                           SecurityIdentification,    // Impersonation level
                           TokenPrimary,              // Token type
                           &amp;hDupToken);               // New/Duplicate token
      if (!bRet)
      {
          //wsprintf(szErr, L"DuplicateTokenEx Error: %d", GetLastError());
          return;
      }
  
      // Get all necessary environment variables of logged in user
      // to pass them to the process
  
      bRet = CreateEnvironmentBlock(&amp;lpEnvironment, // Environment block
                                  hDupToken,        // New token
                                  FALSE);           // Inheritence
      if(!bRet)
      {
          //wsprintf(szErr, L"CreateEnvironmentBlock Error: %d", GetLastError());
          return;
      }
  
      HDESK hdeskInput=OpenInputDesktop(0, FALSE, 0); // does not set GetLastError(), so GetLastError() is arbitrary if NULL is returned
      if( hdeskInput==NULL ) {
          TRACE( "hdeskInput==NULL" );
          return false;
      }
  
      // Initialize Startup and Process info  
      startupInfo-&gt;cb = sizeof(STARTUPINFO);
      startupInfo-&gt;lpDesktop = TEXT("winsta0\\default");
  
  
  
      // Start the process on behalf of the current user 
  
      BOOL returnCode = CreateProcessAsUser(hDupToken, 
                                  applicationName, 
                                  commandLine, 
                                  NULL,
                                  NULL,
                                  FALSE,
                                  NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,
                                  lpEnvironment,
                                  NULL,
                                  startupInfo,
                                  &amp;processInformation);
  </code></pre>
  
  <p>Thanks,
  F</p>