Link to home
Start Free TrialLog in
Avatar of preetham_mp
preetham_mp

asked on

Service and CreateProcessAsUser

Hi All,
 I have written a service that would execute an under the admin login. However, i would like to display it
on my desktop.
Below is the code:
    LPTSTR lpszUsername = "administrator";
    LPTSTR lpszDomain   = "xxxxx";
    LPTSTR lpszPassword = "xxxxx";
    HANDLE hUser;

    BOOL bULogon = LogonUser( lpszUsername,      // string that specifies the user name
                              lpszDomain,      // string that specifies the domain or server
                              lpszPassword,      // string that specifies the password
                              LOGON32_LOGON_BATCH,      // specifies the type of logon operation
                              LOGON32_PROVIDER_DEFAULT      ,      // specifies the logon provider
                              &hUser);      // pointer to variable to receive token handle
      if(!bULogon)
      {
            DebugMessage("ERROR:Falied to logon");
      }

                STARTUPINFO startupInfo;
                PROCESS_INFORMATION processInformation;
      
      memset(&startupInfo, 0, sizeof(startupInfo));
      memset(&processInformation, 0, sizeof(processInformation));
                startupInfo.lpDesktop = _T("winsta0\\default");
      

    BOOL bProc = CreateProcessAsUser(hUser, // handle to a token that represents a logged-on user
                            NULL,      // pointer to name of executable module
                  "C:\\windows\\notepad.exe",
                            NULL,      // pointer to process security attributes
                            NULL,      // pointer to thread security attributes
                            FALSE,      // new process inherits handles
                            CREATE_NEW_CONSOLE,      // creation flags
                            NULL,      // pointer to new environment block
                            NULL,      // pointer to current directory name
                            &startupInfo,      // pointer to STARTUPINFO
                            &processInformation);       // pointer to PROCESS_INFORMATION
      if(!bProc)
      {
            DebugMessage("ERROR: Falied Creating process as user");
      }
      // Wait until application has terminated
      WaitForSingleObject(processInformation.hProcess, INFINITE);

When i start the service, the above code executes. It opens notepad in the administrators account while displaying
it on my desktop. However, the notepad window i see on my screen i not painted clearly. All i see is the titlebar and
the outer frame. Why is the problem.
Is there any way i can execute an application under the administrator and display it in my login desktop.
Appreciate the help.
Thank you,
Preetham.
Avatar of stsanz
stsanz

Why are you using CREATE_NEW_CONSOLE flag as Notepad is not a console application?
Try to call CreateProcessAsUser with dwCreationFlags = 0
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial