locked
How can i lauch a process which has a UI from Windows service ? RRS feed

  • Question

  • I have created a service application and also a windows form application now i want to lauch windows application from the service. I know that in win7 because of service isoloation
    you con't do this directly so i  used 'CreateProcessAsUser' of  'advapi32.dll' method but it is able to create the process also  appearing in 'Task manager' but UI will not be displeyd to the user.  What is the reason ? anybody can help me out of this?

    Ok let me give the code which i have written


    [

    DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Ansi, EntryPoint = "CreateProcessAsUser")]

     

     

    public static extern bool CreateProcessAsUser(IntPtr hToken,string lpApplicationName,string lpCommandLine,ref SECURITY_ATTRIBUTES lpProcessAttributes,

     

     

    ref SECURITY_ATTRIBUTES lpThreadAttributes,bool bInheritHandles,int dwCreationFlags,string lpEnvironment,string lpCurrentDirectory,ref STARTUPINFO lpStartupInfo,ref PROCESS_INFORMATION lpProcessInformation);


     

    void LounchNewApplication()

    {

     

     try

    {

     

    string strAppName = @"D:\Working\UAC Demo\Tester\bin\Debug\Tester.exe";

     

     

    string strAppPath = @"D:\Working\UAC Demo\Tester\bin\Debug\";

     

     

    PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION();

     

     

    SECURITY_ATTRIBUTES lpProcessAttributes = new SECURITY_ATTRIBUTES();

    lpProcessAttributes.nLength = (

     

    uint)Marshal.SizeOf(lpProcessAttributes);

     

     

    STARTUPINFO lpStartupInfo = new STARTUPINFO();

    lpStartupInfo.cb =

     

    Marshal.SizeOf(lpStartupInfo);

    lpStartupInfo.lpDesktop =

     

    "WinSta0\\Default";

     

     

    IntPtr htoken = IntPtr.Zero;

    LogonUser(

     

    "myName", "DomineName", "password", 2, 0, out htoken);

     

     

    if (!CreateProcessAsUser(htoken, strAppName, null, ref lpProcessAttributes, ref lpProcessAttributes, true, 0, null, strAppPath, ref lpStartupInfo, ref lpProcessInformation))

     

    "Error in starting application", EventLogEntryType.Error);

     

    "Application launched successfully PID-" + lpProcessInformation.hThread, EventLogEntryType.Information);

     

     

    //CloseHandle(lpProcessInformation.hThread);

     

    //CloseHandle(lpProcessInformation.hProcess);

    }

     

    catch (Exception ex)

     

    EventLogEntryType.Error);

     

    }

    }

    I am calling LounchNewApplication() method OnStart of the service .

    {

    eventLogger.WriteEntry(ex.Message,

    }

    eventLogger.WriteEntry(

    {

    eventLogger.WriteEntry(

     

    Wednesday, October 21, 2009 7:01 PM

Answers

  • Since it is a software development question, please discuss it in our MSDN forum.

    Microsoft Developer Network


    Arthur Xie - MSFT
    • Marked as answer by Arthur Xie Tuesday, October 27, 2009 2:54 AM
    Thursday, October 22, 2009 8:48 AM
  • Dear Arthur,

    Thanks for your comment. I have solved my problem here is what i did

    I was launching the process as the user but in session 0, which is non-interactive. Don't use LogonUser to create a user token. So i used WTSQueryUserToken, passing in the session iwant to create the process in. This token has the correct session ID. We can use WTSEnumerateSessions to list all sessions on the machine, or handle session change notifications in your service handler.

    follow these links for more details  
    WTSQueryUserToken ->http://msdn.microsoft.com/en-us/library/aa383840%28VS.85%29.aspx
    WTSEnumerateSessions
      ->http://msdn.microsoft.com/en-us/library/aa383833%28VS.85%29.aspx

    • Marked as answer by Arthur Xie Tuesday, October 27, 2009 2:54 AM
    Thursday, October 22, 2009 3:12 PM

All replies

  • Since it is a software development question, please discuss it in our MSDN forum.

    Microsoft Developer Network


    Arthur Xie - MSFT
    • Marked as answer by Arthur Xie Tuesday, October 27, 2009 2:54 AM
    Thursday, October 22, 2009 8:48 AM
  • Dear Arthur,

    Thanks for your comment. I have solved my problem here is what i did

    I was launching the process as the user but in session 0, which is non-interactive. Don't use LogonUser to create a user token. So i used WTSQueryUserToken, passing in the session iwant to create the process in. This token has the correct session ID. We can use WTSEnumerateSessions to list all sessions on the machine, or handle session change notifications in your service handler.

    follow these links for more details  
    WTSQueryUserToken ->http://msdn.microsoft.com/en-us/library/aa383840%28VS.85%29.aspx
    WTSEnumerateSessions
      ->http://msdn.microsoft.com/en-us/library/aa383833%28VS.85%29.aspx

    • Marked as answer by Arthur Xie Tuesday, October 27, 2009 2:54 AM
    Thursday, October 22, 2009 3:12 PM