CreateProcessWithLogonW

From: Todd B (ToddB_at_discussions.microsoft.com)
Date: 07/26/05

  • Next message: Joe Kaplan \(MVP - ADSI\): "Re: The server is not operational"
    Date: Tue, 26 Jul 2005 09:01:03 -0700
    
    

    Hello,

    I am developing an ASP.NET application that allows users to enter data using
    web forms, then run an EXE when they click the appropriate button. The EXE
    needs certain security rights not available to the ASPNET user.

    After playing around with impersonation, I found that even using
    impersonation, the child process will be run as ASPNET. So after more
    digging, I found the API call CreateProcessWithLogonW(). I'm using the RunAs
    class from http://www.codeproject.com/csharp/RunAs.asp and this did the trick
    (after much persuasion) and everything is now working beautifully...almost.

    When the process created with CreateProcessWithLogonW ends, we need to get
    the exit code of the program. Using the System.Diagnostics.Process class, I
    was previously able to do so as such:

       myProcess.Start();
       myProcess.WaitForExit();
       return myProcess.ExitCode;

    Unfortunately, starting the process using CreateProcessWithLogonW prevents
    me from getting the exit code; I get an access denied error. Which makes
    sense, since a user with stronger rights created the process, not the ASPNET
    account. I've tried impersonating the same user with which I create the
    process, but still get the same error.

    So my question is: does anyone know of a way to get the exit code of the
    process created with CreateProcessWithLogonW() in an ASP.NET application?

    A sample code listing follows:

    dim proc as System.Diagnostics.Process
    proc = VastAbyss.RunAs.StartProcess
       ( "username", "domain", "password", "c:\test\myprogram.exe" )

    'Here is where the trouble starts; the call to WaitForExit seems to
    'succeed, but an exception is thrown on the proc.ExitCode call.
    proc.WaitForExit()
    return proc.ExitCode

    Any insights would be greatly appreciated.


  • Next message: Joe Kaplan \(MVP - ADSI\): "Re: The server is not operational"