问题描述
在我的VB6应用程序中,我打开其他EXE文件。我的应用程序在没有任何UAC提示的情况下运行,但我有一个检查软件更新的EXE。这将提示UAC提示。那么,Windows如何决定是否显示UAC提示符?我看到了这个link。那么,这取决于我在应用程序中编写的代码吗?有趣的是,我的应用程序(即主EXE文件)不提示UAC,而一个检查和下载更新的小EXE提示UAC。我已经对所有的EXE文件进行了数字签名。我已经浏览了以下链接:http://msdn.microsoft.com/en-us/library/windows/desktop/aa511445.aspx
http://technet.microsoft.com/en-us/library/cc505883.aspx和其他一些。
但我还是不清楚。
推荐答案
您几乎肯定会遇到WindowsInstaller Detection Technology兼容性试探法。 Windows将尝试检测应用程序何时是安装程序,可能需要提升。
安装程序检测仅适用于:
32位可执行文件 没有requestedExecutionLevel
的应用程序
以启用Lua的标准用户身份运行的交互进程
在创建32位进程之前,将检查以下属性以确定它是否为安装程序:
文件名包括"安装"、"安装"、"更新"等关键字。 以下版本控制资源字段中的关键字:供应商、公司名称、产品名称、文件描述、原始文件名、内部名称和导出名称。 可执行文件中嵌入的并排清单中的关键字。 可执行文件中链接的特定StringTable条目中的关键字。 可执行文件中链接的RC数据中的关键属性。 可执行文件中的目标字节序列。那么,就像你说的:
但我有一个可用来检查软件更新的exe
我猜这CheckForUpdates.exe
正在触发兼容性试探法。
正确的做法是将程序集清单添加到"检查"可执行文件中,通知Windows它应该而不是提升该实用程序。这是通过清单中的requestedExecutionLevel
asInvoker
完成的:
Assembly Manifest.xml:
<?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="ITReasearchAssociates.Contoso.Updater"
type="win32"
/>
<description>Update checker</description>
<!-- Run as standard user. Disable file and registry virtualization -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
这样您的"检查更新"应用程序将永远不会提升,并且永远不会错误地获得管理权限。
如果希望更新程序实际应用更新(需要管理权限的更新),则应以管理员身份启动更新程序应用程序。
示例代码
//Check if there are updates available
if (!CheckForUpdatesAvailable())
return; //no updates. We're done
//If the user is an administrator, then get the update
if (IsUserAnAdmin())
{
//Maybe throw in a "Hey, user, wanna get the update now?" dialog
DownloadAndApplyUpdates();
return;
}
//The user is not an admin.
//Relaunch ourselves as administrator so we can download the update
//Maybe throw in a "Hey, user, wanna get the update now?" dialog. A button with a UAC shield on it
ExecuteAsAdmin(Application.ExecutablePath, "/downloadUpdate");
使用helper函数:
private Boolean IsUserAnAdmin()
{
//Public domain: no attribution required
//A user can be a member of the Administrator group, and yet not be an administrator.
//Conversely, the user can be an administrator while not being a member of the administrators group.
var identity = WindowsIdentity.GetCurrent();
return (null != identity && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator));
}
private void ExecuteAsAdmin(string Filename, string Arguments)
{
//Public domain: no attribution required
ProcessStartInfo startInfo = new ProcessStartInfo(Filename, Arguments);
startInfo.Verb = "runas";
System.Diagnostics.Process.Start(startInfo);
}
然后在启动时,您只需查找/downloadUpdate
命令行参数即可知道您的工作是实际执行工作:
public Form1()
{
InitializeComponent();
//Ideally this would be in program.cs, before the call to Application.Run()
//But that would require me to refactor code out of the Form file, which is overkill for a demo
if (FindCmdLineSwitch("downloadUpdate", true))
{
DownloadAndApplyUpdates();
Environment.Exit(0);
}
}
注意:任何代码都会发布到公有领域。不需要属性。