CreateProcessWithLogonw错误需要提升

时间:2011-01-17 12:35:23

标签: windows winapi

所有 我使用CreateProcessWithLogon方法为非autherized用户安装软件安装软件,它对Windows XP用户工作正常,但同样的应用程序在Windows 7机器中显示如下错误。

System.ComponentModel.Win32Exception(0x80004005):请求的操作需要提升

请任何人建议我修改它。

我使用的代码如下

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
Private Const LOGON_WITH_PROFILE1 As Integer = 0


    Dim siStartup As STARTUPINFO
    Dim piProcess As PROCESS_INFORMATION
    Dim intReturn As Integer
    Dim bResult As Boolean = False
    Dim result As Integer
    Dim sFile, sArg As String

    IMP_USER_NAME = AppSettings("UserName")
    IMP_PASS_WORD = AppSettings("Password")
    IMP_DOMAIN_NAME = AppSettings("Domain")

    Try

        If sApplication.EndsWith(".msi") Then
            sApplication = sApplication & " " & """ALLUSERS=1"""
            sArg = "msiexec.exe /i """ & sApplication & """"
            sFile = vbNullString
        Else
            If bToExecute = False Then
                sArg = vbNullString
                sFile = sApplication
            Else
                sArg = "cmd /c """ & sApplication & """"
                sFile = vbNullString
            End If
        End If

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


        intReturn = CreateProcessWithLogon(IMP_USER_NAME, IMP_DOMAIN_NAME, IMP_PASS_WORD, LOGON_WITH_PROFILE, sFile, sArg, _
                        NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _
                        IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

谢谢, 塞特希

2 个答案:

答案 0 :(得分:1)

请参阅http://blogs.msdn.com/b/cjacks/archive/2010/02/01/why-can-t-i-elevate-my-application-to-run-as-administrator-while-using-createprocesswithlogonw.aspx:您需要一个执行ShellExec()的中间流程来提升使用CreateProcessWithLogon调用的应用程序的权限

Alternativeley,您可以使用CreateProcessWithLogon调用cmd /c cd <targetDir>&<targetDrive>:&<execName>以避免创建该中间过程。

答案 1 :(得分:0)

可能原因是您使用LOGON_WITH_PROFILE标志。在CreateProcessWithLogonW文档的“备注”部分中,您可以阅读以下内容

  

默认情况下,CreateProcessWithLogonW   不加载指定的用户   配置文件到HKEY_USERS注册表   键。这意味着访问   HKEY_CURRENT_USER中的信息   注册表项可能不会产生结果   这与正常情况一致   交互式登录。这是你的   负责加载用户   注册表配置单元之前进入HKEY_USERS   通过调用CreateProcessWithLogonW   使用LOGON_WITH_PROFILE,或者   调用LoadUserProfile函数。

因此,我建议您尝试使用不带LOGON_WITH_PROFILE标记的相同代码。如果您发现这是问题,并且您确实需要使用该标记,则应在代码中使用LoadUserProfile函数和UnloadUserProfile