目前,我正在使用一个本土方法的运行过程中作为不同的用户在Vista,我不能逃脱这种感觉就是这黑客上下的和不理想的(此外,事实上,它掷骰子了UAC,崩溃我的应用程序的安全例外,并迫使我完全禁用UAC).我过程包括两个项目(使两个EXE文件)-"接口"和"发射stub"-这里的过程:

  1. 用户有一个快捷方式推出"Interface.exe notepad.exe"
  2. Interface.exe 有一个形式要求提供凭据,他们想要使用
  3. Interace.exe 使用ProcessStartInfo创建的一个实例LaunchStub.exe (LS)作为新的用户
  4. LS使用ProcessStartInfo(与ShellExecute设置为真正的),推出请求的文件,因为它已经运行为被请求的用户,这样是新的过程。

我有一个两个步骤的过程是我想要的用户能够右击任何文件的操作系统具有一个默认的行动(.EXE.SQL.MSC,等等),并启动它,并ProcessStartInfo仅支持,与"UseShellExecute"的启用,但是,关防止我从使用新的凭证,所以我只能一次一个。

这会导致一些问题--第一,用户已经存在计算机上,这意味着他们必须已登录在地。如果没有当地的配置文件,用户的要求的应用程序有时候会启动的,但我获得登记和档案例外,因为该应用程序预计事存在,还没有(如HKCU配置单元的注册表,其用户不必因为他们从来没有记录)。

我知道我应该可以只是"提升"的权利,我的应用程序的用户,他们的请求,启动我的新过程,然后撤消仰,但我无法找到一个很好的代码样本,我不知道,这将允许运行一个完全不同的用户。这是否有意义?我只是帮不觉得有更好的方式来做到这一点。


更新: 我只是试图 一些模拟码 我在网上找到,但无济于事。结合使用时与ProcessStartInfo,它似乎仍然启动过程中使用我的前登录,不是一个我已经提供,即使我已经被激活模拟使用所提供的全权证书。

有帮助吗?

解决方案

机会是,你必须创建自己的"壳"功能,使用Win32API。

使用CreateProcessWithLogonW API您可以创建新的过程在不同的凭证和选择地载的用户配置文件的信息。

在代码下面的,如果你更换

  • 用户名与你的用户名
  • 域与你域或"vbNullString"
  • 密码-你的密码
  • 参数的4-替代0'登录用配置文件载的指定用户配置文件。

请参阅文件 CreateProcessWithLogonW API 为进一步的细节。走这条路线你有完全的控制,并全面负责启动的应用程序。

再这只是个样本,你可能必须发挥它一点得到它来做什么你想要的。


Imports System.Runtime.InteropServices

Public Module modShell

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
        Public cb As Integer
        Public lpReserved As String
        Public lpDesktop As String
        Public lpTitle As String
        Public dwX As Integer
        Public dwY As Integer
        Public dwXSize As Integer
        Public dwYSize As Integer
        Public dwXCountChars As Integer
        Public dwYCountChars As Integer
        Public dwFillAttribute As Integer
        Public dwFlags As Integer
        Public wShowWindow As Short
        Public cbReserved2 As Short
        Public lpReserved2 As Integer
        Public hStdInput As Integer
        Public hStdOutput As Integer
        Public hStdError As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
        Public hProcess As IntPtr
        Public hThread As IntPtr
        Public dwProcessId As Integer
        Public dwThreadId As Integer
    End Structure

    Public Declare Unicode Function CreateProcessWithLogonW Lib "Advapi32" (ByVal lpUsername As String, ByVal lpDomain As String, ByVal lpPassword As String, ByVal dwLogonFlags As Int32, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, ByRef si As STARTUPINFO, ByRef pi As PROCESS_INFORMATION) As Integer
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer

    Public Const LOGON_WITH_PROFILE As Int32 = &H1

    Public Const NORMAL_PRIORITY_CLASS As Int32 = &H20&

    Public Const STARTF_USESHOWWINDOW As Int32 = &H1
    Public Const SW_HIDE As Int16 = 0
    Public Const SW_SHOW As Int16 = 5

    Public Function Shell(ByVal strCmdLine As String, ByVal strCurrentDirectory As String) As Boolean

        Dim pi As PROCESS_INFORMATION
        Dim si As New STARTUPINFO

        si.cb = Marshal.SizeOf(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_SHOW

        Dim result As Integer = CreateProcessWithLogonW("username", "domain", "password", 0, vbNullString, strCmdLine, NORMAL_PRIORITY_CLASS, IntPtr.Zero, strCurrentDirectory, si, pi)

        If result <> 0 Then
            Call CloseHandle(pi.hThread)
            Call CloseHandle(pi.hProcess)
        Else
            Return False
        End If

        Return True

    End Function

End Module

其他提示

你可以试试运行的运行方式从你的应用程序。一些实例和选项 在这里,.

试试这个模块:

Module Impersonation

#Region "API Structures"
    <StructLayout(LayoutKind.Sequential)> _
      Public Structure PROCESS_INFORMATION
        Dim hProcess As System.IntPtr
        Dim hThread As System.IntPtr
        Dim dwProcessId As Integer
        Dim dwThreadId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
     Public Structure STARTUPINFO
        Dim cb As Integer
        Dim lpReserved As System.IntPtr
        Dim lpDesktop As System.IntPtr
        Dim lpTitle As System.IntPtr
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize As Integer
        Dim dwYSize As Integer
        Dim dwXCountChars As Integer
        Dim dwYCountChars As Integer
        Dim dwFillAttribute As Integer
        Dim dwFlags As Integer
        Dim wShowWindow As Short
        Dim cbReserved2 As Short
        Dim lpReserved2 As System.IntPtr
        Dim hStdInput As System.IntPtr
        Dim hStdOutput As System.IntPtr
        Dim hStdError As System.IntPtr
    End Structure
#End Region

#Region "API Constants"
    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2
    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000
    Private Const CREATE_NEW_CONSOLE As Integer = &H10
    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200
    Private Const LOGON_WITH_PROFILE As Integer = &H1
#End Region

#Region "API Functions"
    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _
        (ByVal lpUsername As String, _
         ByVal lpDomain As String, _
         ByVal lpPassword As String, _
         ByVal dwLogonFlags As Integer, _
         ByVal lpApplicationName As String, _
         ByVal lpCommandLine As String, _
         ByVal dwCreationFlags As Integer, _
         ByVal lpEnvironment As System.IntPtr, _
         ByVal lpCurrentDirectory As System.IntPtr, _
         ByRef lpStartupInfo As STARTUPINFO, _
         ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As System.IntPtr) As Integer

#End Region

    Public Sub RunProgram(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal Application As String, ByVal CommandLine As String)

        Dim siStartup As STARTUPINFO
        Dim piProcess As PROCESS_INFORMATION
        Dim intReturn As Integer

        If CommandLine Is Nothing OrElse CommandLine = "" Then CommandLine = String.Empty

        siStartup.cb = Marshal.SizeOf(siStartup)
        siStartup.dwFlags = 0

        intReturn = CreateProcessWithLogon(UserName, Domain, Password, LOGON_WITH_PROFILE, Application, CommandLine, _
        NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _
        IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

        If intReturn = 0 Then
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If

        CloseHandle(piProcess.hProcess)
        CloseHandle(piProcess.hThread)

    End Sub

End Module

使用Runprogram()启动程序的用户/pw y等。程序手段。exe仅、参数ar写入"命令行"

如果你想要开始一个应用程序有不同的凭证,比目前的运行过程中,你可以使用。净 过程 类。

this.Process = new Process();

this.Process.StartInfo.Arguments = "Arguments";
this.Process.StartInfo.FileName = "C:\your.exe";
this.Process.StartInfo.UserName = "UserName";
string password = "some password";

this.Process.StartInfo.Password.Clear();
foreach (char c in password)
{
    this.Process.StartInfo.Password.AppendChar(c);
}


//allow the process to raise events
this.Process.EnableRaisingEvents = true;
this.Process.StartInfo.ErrorDialog = false;
//Method for handling the exit event
this.Process.Exited += new EventHandler(ApplicationProcess_Exited);

//Set the application directory as the current working directory
Environment.CurrentDirectory = System.IO.Directory.GetParent("C:\").ToString();

if (this.Process.Start())
{
    // Do something on start
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top