简体   繁体   English

如何将UAC集成到我的VB6程序中?

[英]How do I integrate UAC into my VB6 program?

I need some code that will add the admin rights icon to command buttons and display the prompt when such buttons are clicked. 我需要一些代码,将管理员权限图标添加到命令按钮,并在单击这些按钮时显示提示。 How can I do this in VB6? 我怎么能在VB6中这样做? Some actions require admin rights because they replace files and stuff where Windows Vista/7 don't allow the program normal access to the files. 某些操作需要管理员权限,因为它们会替换Windows Vista / 7不允许程序正常访问文件的文件和内容。

Here's a VB6 example of ShellExecuteEx that will allow you to optionally execute any process with admin permissions. 这是ShellExecuteEx的VB6示例,它允许您选择性地执行具有管理员权限的任何进程。 You can drop this into a module or class. 您可以将其放入模块或类中。

Option Explicit

Private Const SEE_MASK_DEFAULT = &H0

Public Enum EShellShowConstants
        essSW_HIDE = 0
        essSW_SHOWNORMAL = 1
        essSW_SHOWMINIMIZED = 2
        essSW_MAXIMIZE = 3
        essSW_SHOWMAXIMIZED = 3
        essSW_SHOWNOACTIVATE = 4
        essSW_SHOW = 5
        essSW_MINIMIZE = 6
        essSW_SHOWMINNOACTIVE = 7
        essSW_SHOWNA = 8
        essSW_RESTORE = 9
        essSW_SHOWDEFAULT = 10
End Enum

Private Type SHELLEXECUTEINFO
        cbSize        As Long
        fMask         As Long
        hwnd          As Long
        lpVerb        As String
        lpFile        As String
        lpParameters  As String
        lpDirectory   As String
        nShow         As Long
        hInstApp      As Long
        lpIDList      As Long     'Optional
        lpClass       As String   'Optional
        hkeyClass     As Long     'Optional
        dwHotKey      As Long     'Optional
        hIcon         As Long     'Optional
        hProcess      As Long     'Optional
End Type

Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpSEI As SHELLEXECUTEINFO) As Long

Public Function ExecuteProcess(ByVal FilePath As String, ByVal hWndOwner As Long, ShellShowType As EShellShowConstants, Optional EXEParameters As String = "", Optional LaunchElevated As Boolean = False) As Boolean
    Dim SEI As SHELLEXECUTEINFO

    On Error GoTo Err

    'Fill the SEI structure
    With SEI
        .cbSize = Len(SEI)                  ' Bytes of the structure
        .fMask = SEE_MASK_DEFAULT           ' Check MSDN for more info on Mask
        .lpFile = FilePath                  ' Program Path
        .nShow = ShellShowType              ' How the program will be displayed
        .lpDirectory = PathGetFolder(FilePath)
        .lpParameters = EXEParameters       ' Each parameter must be separated by space. If the lpFile member specifies a document file, lpParameters should be NULL.
        .hwnd = hWndOwner                   ' Owner window handle

        ' Determine launch type (would recommend checking for Vista or greater here also)
        If LaunchElevated = True Then ' And m_OpSys.IsVistaOrGreater = True
            .lpVerb = "runas"
        Else
            .lpVerb = "Open"
        End If
    End With

     ExecuteProcess = ShellExecuteEx(SEI)   ' Execute the program, return success or failure

    Exit Function
Err:
    ' TODO: Log Error
    ExecuteProcess = False
End Function

Private Function PathGetFolder(psPath As String) As String
    On Error Resume Next
    Dim lPos As Long
    lPos = InStrRev(psPath, "\")
    PathGetFolder = Left$(psPath, lPos - 1)
End Function

Code examples can really run on, but here is a trivial one showing the "second instance of me" approach. 代码示例可以真正运行,但这里有一个显示“我的第二个实例”方法的简单例子。

The program has a startup static module with a few public functions including an "elevated operation" handler, and a Form with just one CommandButton on it: 该程序有一个启动静态模块,有一些公共函数,包括一个“提升操作”处理程序,以及一个只有一个CommandButton的Form:

Module1.bas Module1.bas

Option Explicit

Private Const BCM_SETSHIELD As Long = &H160C&

Private Declare Sub InitCommonControls Lib "comctl32" ()

Private Declare Function IsUserAnAdmin Lib "shell32" () As Long

Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByRef lParam As Any) As Long

Private Declare Function ShellExecute Lib "shell32" _
    Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As VbAppWinStyle) As Long

Private mblnIsElevated As Boolean

Public Function IsElevated() As Boolean
    IsElevated = mblnIsElevated
End Function

Public Sub OperationRequiringElevation(ByRef Params As Variant)
    MsgBox "Insert logic here for: " & vbNewLine _
         & Join(Params, vbNewLine)
End Sub

Public Sub RequestOperation( _
    ByVal hWnd As Long, _
    ByVal Focus As VbAppWinStyle, _
    ByRef Params As Variant)

    ShellExecute hWnd, "runas", App.EXEName & ".exe", _
                 Join(Params, " "), CurDir$(), Focus
End Sub

Public Sub SetShield(ByVal hWnd As Long)
    SendMessage hWnd, BCM_SETSHIELD, 0&, 1&
End Sub

Private Sub Main()
    If Len(Command$()) > 0 Then
        'Assume we've been run elevated to execute an operation
        'specified as a set of space-delimited strings.
        OperationRequiringElevation Split(Command$(), " ")
    Else
        mblnIsElevated = IsUserAnAdmin()
        InitCommonControls
        Form1.Show
    End If
End Sub

Form1.frm Form1.frm

Option Explicit

Private Sub Command1_Click()
    Dim Params As Variant

    Params = Array("ReplaceFile", "abc", "123")
    If IsElevated() Then
        OperationRequiringElevation Params
    Else
        RequestOperation hWnd, vbHide, Params
    End If
End Sub

Private Sub Form_Load()
    If Not IsElevated() Then
        SetShield Command1.hWnd
    End If
End Sub

The application has a simple "asInvoker" manifest selecting the Common Controls 6.0 assembly. 该应用程序有一个简单的“asInvoker”清单,选择Common Controls 6.0程序集。

First, take the code that runs when someone clicks the button, and put it in a separate exe. 首先,获取有人单击按钮时运行的代码,并将其放在单独的exe中。 Change your button-click code to launch the exe using ShellExecute. 更改按钮单击代码以使用ShellExecute启动exe。 Second, build external manifests for each new exe and have it specify requireAdministrator. 其次,为每个新exe构建外部清单,并指定requireAdministrator。 Third, send your buttons the BCM_SETSHIELD message (you will probably have to look up the numerical value of the message ID) to make the shield appear on them. 第三,向您的按钮发送BCM_SETSHIELD消息(您可能需要查找消息ID的数值)以使屏蔽显示在它们上面。


  1. Move all of the code that requires elevation into external processes. 将需要提升的所有代码移动到外部进程中。
  2. Send your buttons the BCM_SETSHIELD message to add the shield icon. 向您的按钮发送BCM_SETSHIELD消息以添加盾牌图标。
  3. Embed manifests into those processes, telling Windows that they require elevation. 将清单嵌入到这些进程中,告诉Windows它们需要提升。 See below. 见下文。



In order to force Vista and higher to run a VB6 exe as administrator in UAC, you must embed a manifest xml as a resource inside of it. 为了强制Vista和更高版本在UAC中以管理员身份运行VB6 exe,您必须将清单xml作为资源嵌入其中。 Steps follow; 步骤如下;

  1. Create the manifest file. 创建清单文件。 Name it "YourProgram.exe.manifest" it should contain the following. 将其命名为“YourProgram.exe.manifest”,它应包含以下内容。 The important line is the "requestedExecutionLevel". 重要的一行是“requestedExecutionLevel”。 Change the attributes in to match your exe. 更改属性以匹配您的exe。

     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="YourProgram" type="win32" > <description>application description</description> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly> 
  2. Create a file named "YourProgram.exe.manifest.rc". 创建名为“YourProgram.exe.manifest.rc”的文件。 It should contain the following. 它应包含以下内容。

    #define CREATEPROCESS_MANIFEST_RESOURCE_ID 1 #define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
    #define RT_MANIFEST 24 CREATEPROCESS_MANIFEST_RESOURCE_ID #define RT_MANIFEST 24 CREATEPROCESS_MANIFEST_RESOURCE_ID
    RT_MANIFEST "YourProgram.exe.manifest" RT_MANIFEST“YourProgram.exe.manifest”

  3. Compile your resource using rc.exe. 使用rc.exe编译资源。 It is located by default in C:\\Program Files\\Microsoft Visual Studio\\COMMON\\MSDev98\\Bin. 它默认位于C:\\ Program Files \\ Microsoft Visual Studio \\ COMMON \\ MSDev98 \\ Bin中。 This will create a file called YourProgram.exe.manifest.RES. 这将创建一个名为YourProgram.exe.manifest.RES的文件。 The syntax is; 语法是;

    rc /r YourProgram.exe.manifest.rc

  4. Add the .RES file to your project. 将.RES文件添加到项目中。 Do this using the Resource Editor Add-In in VB6. 使用VB6中的资源编辑器加载项执行此操作。 The icon on the toolbar looks like green blocks. 工具栏上的图标看起来像绿色块。 If you do not have the icon, make sure it is enabled in the addin manager. 如果您没有该图标,请确保在插件管理器中启用该图标。 If it is not in the addin manager, you need to regsvr32 on C:\\Program Files\\Microsoft Visual Studio\\VB98\\Wizards\\Resedit.dll. 如果它不在插件管理器中,则需要在C:\\ Program Files \\ Microsoft Visual Studio \\ VB98 \\ Wizards \\ Resedit.dll上使用regsvr32。 Once you've got the resource editor open, click open and select your .RES file. 打开资源编辑器后,单击“打开”并选择.RES文件。

  5. Compile your project. 编译您的项目。

  6. To double check that the manifest was embedded properly, you can use a tool called InspectExe . 要仔细检查清单是否已正确嵌入,可以使用名为InspectExe的工具。 In explorer, go to the properties of the exe, and if the manifest was embedded you should have a manifest tab (.Net assemblies will also have this manifest tab). 在资源管理器中,转到exe的属性,如果嵌入了清单,则应该有一个清单选项卡(.Net程序集也将具有此清单选项卡)。

  7. Try running your program on Vista or later. 尝试在Vista或更高版本上运行程序。 If UAC is indeed enabled, it should come up with the prompt right away. 如果确实启用了UAC,它应该立即提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM