如何将 UAC 集成到我的 VB6 程序中?
我需要一些代码,将管理员权限图标添加到命令按钮,并在单击此类按钮时显示提示。我怎样才能在VB6中做到这一点?某些操作需要管理员权限,因为它们会替换 Windows Vista/7 不允许程序正常访问文件的文件和内容。
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? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面是 ShellExecuteEx 的 VB6 示例,它允许您选择使用管理员权限执行任何进程。您可以将其放入模块或类中。
Here's a VB6 example of ShellExecuteEx that will allow you to optionally execute any process with admin permissions. You can drop this into a module or class.
代码示例确实可以继续运行,但这里有一个简单的代码示例,展示了“我的第二个实例”方法。
该程序有一个启动静态模块,其中包含一些公共功能,包括“提升操作”处理程序,以及一个只有一个 CommandButton 的表单:
Module1.bas
Form1.frm
该应用程序有一个简单的“asInvoker”清单,选择 Common Controls 6.0集会。
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:
Module1.bas
Form1.frm
The application has a simple "asInvoker" manifest selecting the Common Controls 6.0 assembly.
首先,获取当有人单击按钮时运行的代码,并将其放入单独的 exe 中。更改按钮单击代码以使用 ShellExecute 启动 exe。其次,为每个新 exe 构建外部清单并指定 requireAdministrator。第三,向按钮发送 BCM_SETSHIELD 消息(您可能需要查找消息 ID 的数值)以使防护罩出现在按钮上。
First, take the code that runs when someone clicks the button, and put it in a separate exe. Change your button-click code to launch the exe using ShellExecute. Second, build external manifests for each new exe and have it specify 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.
为了强制 Vista 及更高版本在 UAC 中以管理员身份运行 VB6 exe,您必须将清单 xml 作为资源嵌入其中。步骤如下:
创建清单文件。将其命名为“YourProgram.exe.manifest”,它应包含以下内容。重要的一行是“requestedExecutionLevel”。更改属性以匹配您的 exe。
<块引用>
创建一个名为“YourProgram.exe.manifest.rc”的文件。它应包含以下内容。
<块引用>
#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
#define RT_MANIFEST 24 CREATEPROCESS_MANIFEST_RESOURCE_ID
RT_MANIFEST“YourProgram.exe.manifest”
使用 rc.exe 编译您的资源。默认情况下,它位于 C:\Program Files\Microsoft Visual Studio\COMMON\MSDev98\Bin 中。这将创建一个名为 YourProgram.exe.manifest.RES 的文件。语法是;
<块引用>
rc /r YourProgram.exe.manifest.rc
将 .RES 文件添加到您的项目中。使用 VB6 中的资源编辑器插件执行此操作。工具栏上的图标看起来像绿色块。如果您没有该图标,请确保在插件管理器中启用它。如果插件管理器中没有,则需要在 C:\Program Files\Microsoft Visual Studio\VB98\Wizards\Resedit.dll 上进行 regsvr32。打开资源编辑器后,单击“打开”并选择您的 .RES 文件。
编译您的项目。
要仔细检查清单是否正确嵌入,您可以使用名为 InspectExe。在资源管理器中,转到 exe 的属性,如果嵌入了清单,您应该有一个清单选项卡(.Net 程序集也将有此清单选项卡)。
尝试在 Vista 或更高版本上运行您的程序。如果确实启用了 UAC,它应该立即出现提示。
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. Steps follow;
Create the manifest file. Name it "YourProgram.exe.manifest" it should contain the following. The important line is the "requestedExecutionLevel". Change the attributes in to match your exe.
Create a file named "YourProgram.exe.manifest.rc". It should contain the following.
Compile your resource using rc.exe. It is located by default in C:\Program Files\Microsoft Visual Studio\COMMON\MSDev98\Bin. This will create a file called YourProgram.exe.manifest.RES. The syntax is;
Add the .RES file to your project. Do this using the Resource Editor Add-In in 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. Once you've got the resource editor open, click open and select your .RES file.
Compile your project.
To double check that the manifest was embedded properly, you can use a tool called 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).
Try running your program on Vista or later. If UAC is indeed enabled, it should come up with the prompt right away.