如何确定程序是否需要提升权限?

How to determine if a program needs elevated rights?

想象一下以下场景:

我在某处有一个 exe 文件 "DoINeedAdmin.exe",想检查该程序是否需要管理员权限才能启动。

尝试启动它并通过某些失败 return 值确定它是可以的。

由于 Windows 如果需要提升权限,显然可以通过将管理图标添加到 exe 来做到这一点,我希望它也可以通过编程实现。

简而言之:Application Manifest. This is an XML file embedded into an executable, and of particluar interest there is the <requestedExecutionLevel level="requireAdministrator" /> tag.

总之,没有万全之策

如果在其清单中指定,

Windows 将以提升的方式启动程序。如果程序名称包含 Setup, Install, Update, Patch (maybe more) (unless this is disabled by a local policy or a manifest file).

,他们也会以提升的方式启动它

除此之外,应用程序还可以从代码中请求提升。您无法在外部程序中检测到它。

shell32(序号 865)中有一个未记录的函数,它采用 LPCWSTR 路径和 returns 如果路径需要提升,则为非零 BOOL。我不知道这个函数是否检查外部清单。