开发者

CreateProcessWithLogon Error requires elevation

开发者 https://www.devze.com 2023-02-05 15:08 出处:网络
all I am using CreateProcessWithLogon method to installing softwares for non autherized users to installing software, it was working fine for windows XP users, but same application shows the error as

all I am using CreateProcessWithLogon method to installing softwares for non autherized users to installing software, it was working fine for windows XP users, but same application shows the error as follows in windows 7 machine.

System.ComponentModel.Win32Exception(0x80004005): the requested operation requires elevation

please any one suggest me any idea to fix it.

i am using the code as follows

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
                sF开发者_如何学JAVAile = 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)

Thanks, Senthil


See 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 : you need an intermediate process that execute ShellExec() to elevate the priveleges of an application called with CreateProcessWithLogon

Alternativeley, you can call cmd /c cd <targetDir>&<targetDrive>:&<execName> with CreateProcessWithLogon to avoid creating that intermediate process.


Probably the reason is that you use LOGON_WITH_PROFILE flag. In the "Remark" part of the CreateProcessWithLogonW documentation you can read following

By default, CreateProcessWithLogonW does not load the specified user profile into the HKEY_USERS registry key. This means that access to information in the HKEY_CURRENT_USER registry key may not produce results that are consistent with a normal interactive logon. It is your responsibility to load the user registry hive into HKEY_USERS before calling CreateProcessWithLogonW, by using LOGON_WITH_PROFILE, or by calling the LoadUserProfile function.

So I recommend you to try the same code without LOGON_WITH_PROFILE flag. If you will see that it was the problem and you do need to use the flag you should use LoadUserProfile function and UnloadUserProfile in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消