locked
when launching UI based application from Service mode w/ Interactive Client Process[CreateProcessAsUser], displaying is abnormal RRS feed

  • Question

  • hello,

    :( I've read lots of articles about CreateProcess families but couldn't solve this problem...

    when i launch a ui based application in Service mode with CreateProcessAsUser[display to winsta0\\default], taskmanager indicates a process is started as what i want account prev.[i've watched on RDP session].
    but when logon with the account in console mode, the displaying of application is somewhat abnormal.
    [showing window title only or just some illustion, or just some lines.. yes, task windows shows on task bar]

    the code i've used code is below,
    [run in Windows 2003 Server]

    the distorted display is below[please follow exteral link]
    [http://yfrog.com/456hj]

    please any help?

    thanks in advance,

    8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<- 8<-

     if (!LogonUser(
      szUserName,
      NULL,
      szPassword,
      LOGON32_LOGON_INTERACTIVE,
      LOGON32_PROVIDER_DEFAULT,
      &hToken) )
     {
      _iError = GetLastError();
      goto Cleanup;
     }

     // Save a handle to the caller's current window station.

     if ( (hwinstaSave = GetProcessWindowStation() ) == NULL)
      goto Cleanup;

     // Get a handle to the interactive window station.

     hwinsta = OpenWindowStation(
      _T("winsta0"),                   // the interactive window station
      FALSE,                       // handle is not inheritable
      READ_CONTROL | WRITE_DAC);   // rights to read/write the DACL

     if (hwinsta == NULL)
      goto Cleanup;

     // To get the correct default desktop, set the caller's
     // window station to the interactive window station.

     if (!SetProcessWindowStation(hwinsta))
      goto Cleanup;

     // Get a handle to the interactive desktop.

     hdesk = OpenDesktop(
      _T("default"),     // the interactive window station
      0,             // no interaction with other desktop processes
      FALSE,         // handle is not inheritable
      READ_CONTROL | // request the rights to read and write the DACL
      WRITE_DAC |
      DESKTOP_WRITEOBJECTS |
      DESKTOP_READOBJECTS);

     // Restore the caller's window station.

     if (!SetProcessWindowStation(hwinstaSave))
      goto Cleanup;

     if (hdesk == NULL)
      goto Cleanup;

     // Get the SID for the client's logon session.

     if (!GetLogonSID(hToken, &pSid))
      goto Cleanup;

     // Allow logon SID full access to interactive window station.

     if (! AddAceToWindowStation(hwinsta, pSid) )
      goto Cleanup;

     // Allow logon SID full access to interactive desktop.

     if (! AddAceToDesktop(hdesk, pSid) )
      goto Cleanup;

     // Impersonate client to ensure access to executable file.

     if (! ImpersonateLoggedOnUser(hToken) )
      goto Cleanup;

     // Initialize the STARTUPINFO structure.
     // Specify that the process runs in the interactive desktop.

     ZeroMemory(&si, sizeof(STARTUPINFO));
     si.cb= sizeof(STARTUPINFO);
     si.lpDesktop = _T("winsta0\\default");

     // Launch the process in the client's logon session.

     BOOL _bResult = CreateProcessAsUser(
      hToken,            // client's access token
      NULL,              // file to execute
      _szCommandline,     // command line
      NULL,              // pointer to process SECURITY_ATTRIBUTES
      NULL,              // pointer to thread SECURITY_ATTRIBUTES
      FALSE,             // handles are not inheritable
      NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,   // creation flags
      NULL,              // pointer to new environment block
      NULL,              // name of current directory
      &si,               // pointer to STARTUPINFO structure
      &pi                // receives information about new process
      );

     // End impersonation of client.
     if ( TRUE == _bResult )
     {
      if ( NULL != pPid )
      {
       *pPid = pi.dwProcessId;
      }
      _hResult = S_OK;
     }
     else
     {
      _iError = GetLastError();
     }

     RemoveAceFromWindowStation( hwinsta, pSid );
     RemoveAceFromDesktop( hdesk, pSid );

     if ( TRUE == _bResult )
     {
      if ( NULL != pPid )
      {
       *pPid = pi.dwProcessId;
      }
      _hResult = S_OK;
     }
     else
     {
       GetExitCodeProcess( pi.hProcess, &_iError );
     }

     RevertToSelf();

     if ( _bResult && pi.hProcess != INVALID_HANDLE_VALUE )
     {
      WaitForSingleObject(pi.hProcess, INFINITE);
      CloseHandle(pi.hProcess);
     }

     if (pi.hThread != INVALID_HANDLE_VALUE)
      CloseHandle(pi.hThread);

    Cleanup:

     if (hwinstaSave != NULL)
      SetProcessWindowStation (hwinstaSave);

     // Free the buffer for the logon SID.

     if (pSid)
      FreeLogonSID(&pSid);

     // Close the handles to the interactive window station and desktop.

     if (hwinsta)
      CloseWindowStation(hwinsta);

     if (hdesk)
      CloseDesktop(hdesk);

     // Close the handle to the client's access token.

     if (hToken != INVALID_HANDLE_VALUE)
      CloseHandle(hToken);

     

    • Edited by belliny Tuesday, November 16, 2010 1:21 AM add external image link
    Tuesday, November 16, 2010 1:00 AM