【问题标题】:Running a DLL as administrator以管理员身份运行 DLL
【发布时间】:2021-08-10 14:16:39
【问题描述】:

我尝试在管理上下文中执行单个 DLL,以强制用户在调用方法时输入 UAC,以防他被验证为普通用户。

我在这里找到了建议:Visual Basic Program - Ask for Admin Permissions

我已尝试创建 Nathan M 建议的内容,但没有显示 UAC 控件。

这是我正在使用的代码:

主要应用:

Imports System
Imports System.Diagnostics.Eventing.Reader
Imports GetUninstallToken.UninstallToken

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim t As New GetToken
            t.Test()
        Catch ex As Exception
        End Try
    End Sub

我在项目中添加了一个简单的 DLL 文件,只是为了获取身份验证对话框:

Namespace UninstallToken
    Public Class GetToken
        Public Sub Test()
            MsgBox("Test")
        End Sub
    End Class
End Namespace

我已经创建了一个清单文件,该文件应该适用于具有此配置的 DLL

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
             If you want to change the Windows User Account Control level replace the 
             requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
            Remove this element if your application requires this virtualization for backwards
            compatibility.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

项目如下所示:

因此,当我运行程序并单击按钮时,将调用 DLL 并显示 MsgBox,但没有 UAC - 我没有以管理员身份运行 VS。

任何想法我错过了什么?似乎清单文件可能会被忽略...

我在 Win 10 19H2 上使用 Microsoft Visual Studio Community 2019 版本 16.5.4 - 我以普通用户身份执行。

【问题讨论】:

  • 这能回答你的问题吗? Requested Execution Level for a dll
  • 嗨@GSerg 感谢您提供的信息。实际上,当我搜索时并没有出现,答案实际上与我之前发现的不同(我认为)......将检查 rundll 的内容,看看是否有帮助。谢谢丹尼尔
  • 关于这个问题的第一条评论中链接的答案是正确的:没有“以管理员身份运行 DLL”之类的东西,因为 DLL 包含由进程执行的代码,只有一个进程可以升高。正如另一个答案所说:如果您需要 DLL 中的代码来运行提升,请编写一个调用 DLL 中代码的可执行文件,然后运行提升的可执行文件。

标签: vb.net admin manifest uac


【解决方案1】:

这是我最终想到的:

  1. 我生成了一个包含请求的子功能的新 EXE

  2. 我添加了一个清单文件,将执行上下文设置为“requireAdministrator”

  3. 我使用以下代码在主应用程序中调用该 exe:

    Try
         Dim process As System.Diagnostics.Process = Nothing
         Dim processStartInfo As System.Diagnostics.ProcessStartInfo
         processStartInfo = New System.Diagnostics.ProcessStartInfo()
         processStartInfo.FileName = ".\MyApplication.exe"
         processStartInfo.Verb = "runas"
    
         processStartInfo.Arguments = ""
         processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
         processStartInfo.UseShellExecute = True
         process = System.Diagnostics.Process.Start(processStartInfo)
    
     Catch ex As Exception
         MsgBox("You need to be administrator to request this function")
     End Try
    

这样我在调用函数时确实会收到 UAC 提示,如果没有,则会向用户显示错误消息。

可能不是最优雅的东西,但简单且有效;)

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 2016-09-20
    • 2010-11-26
    • 2012-05-28
    • 2011-03-24
    • 1970-01-01
    • 2022-01-08
    • 2015-01-22
    • 2017-05-19
    相关资源
    最近更新 更多