CreateProcessWithLogon and managed Processes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Livingston

    #1

    CreateProcessWithLogon and managed Processes

    Sample code below:

    Dim iRet As Integer
    Dim si As New STARTUPINFO
    Dim pi As PROCESS_INFORMA TION

    si.cb = Marshal.SizeOf( si)

    iRet = CreateProcessWi thLogonW(UserNa me, DomainName, Password, _
    LOGON_WITH_PROF ILE, Nothing, CommandLine, _
    NORMAL_PRIORITY _CLASS, 0, StartIn, si, pi)

    If iRet = 0 Then
    Throw New System.Componen tModel.Win32Exc eption
    Else
    Try
    p = Process.GetProc essById(pi.dwPr ocessId)
    AddHandler p.Exited, AddressOf ProcessExitHand ler
    p.EnableRaising Events = True

    Catch ex As ArgumentExcepti on
    'Process ID no longer exists. Don't worry about it.
    End Try

    CloseHandle(pi. hThread)
    CloseHandle(pi. hProcess)

    End If

    CreateProcessWi thLogonW is called with credentials for an account belonging
    to the Administrators group. The code above is run from a non-administrative
    account. The problem is, I cannot get the managed Process class to access
    any of the exit information for the process. Setting EnableRaisingEv ents
    generates another Win32Exception, as well as calling WaitForExit or checking
    the HasExited property of the process class.

    Is there any way to check the exit state of the process in this situation?
    I have other code that needs to execute, but only after the process has
    finished.

    Thanks!
  • Peter Huang [MSFT]

    #2
    RE: CreateProcessWi thLogon and managed Processes

    Hi

    What is the concret win32exception do you get?

    Is it similar with below?

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.Componen tModel.Win32Exc eption: Access is denied


    If so I think it is because the user accout did not have the enough
    privilegeto open the process.
    In the underlying level, the Process class will try to call the OpenProcess
    API so that it can monitor the Process status.

    You can look into the OpenProcess API in MSDN.
    Remarks
    To open a handle to another another process and obtain full access rights,
    you must enable the SeDebugPrivileg e privilege. For more information, see
    Changing Privileges in a Token.

    The handle returned by the OpenProcess function can be used in any function
    that requires a handle to a process, such as the wait functions, provided
    the appropriate access rights were requested.

    When you are finished with the handle, be sure to close it using the
    CloseHandle function.

    I think you may try to Impersonalte the administrator account to do the
    privilege job.
    319615 PRB: "Unable to Impersonate User" Error Message When You Use
    Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.


    248187 How to impersonate a user from Active Server Pages
    Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.



    Best regards,

    Peter Huang
    Microsoft Online Partner Support

    Get Secure! - www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Eric Livingston

      #3
      RE: CreateProcessWi thLogon and managed Processes

      I also managed to get it to work as needed by calling the native
      WaitForSingleOb ject using the process handle. This caused my current thread
      to wait until the process had completed, then I was able to fire an event
      after that call to do what I need.

      Thanks for the help!

      Comment

      • Peter Huang [MSFT]

        #4
        RE: CreateProcessWi thLogon and managed Processes

        Hi

        I am glad that you have figured out the problem.

        Best regards,

        Peter Huang
        Microsoft Online Partner Support

        Get Secure! - www.microsoft.com/security
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        Working...