Delphi 10.4.2:为什么控制台应用程序需要管理权限?

Delphi 10.4.2: Why does console application require administrative privileges?

为什么简单的控制台应用程序需要管理权限?

program LTUpdate;

{$APPTYPE CONSOLE}

begin
  WriteLn('Hello World');
end.

项目中是否有某个复选框将应用程序设置为需要管理权限?

(最终程序将连接到数据库,获取一些字段并将其更新到其他地方,现在我可以通过 VCL 完成...但我想这次我会尝试一个简单的控制台应用程序。)

您的应用正在编译为 32 位,并且缺少包含 requestedExecutionLevel 值的 UAC 清单,因此 UAC 的“安装程序检测”功能启动,这就是您的应用需要提升的原因:

https://docs.microsoft.com/en-us/windows/security/identity-protection/user-account-control/how-user-account-control-works

Installer detection technology

Installation programs are apps designed to deploy software. Most installation programs write to system directories and registry keys. These protected system locations are typically writeable only by an administrator in Installer detection technology, which means that standard users do not have sufficient access to install programs. Windows 10 and Windows 11 heuristically detect installation programs and requests administrator credentials or approval from the administrator user in order to run with access privileges. Windows 10 and Windows 11 also heuristically detect updates and programs that uninstall applications. One of the design goals of UAC is to prevent installations from being run without the user's knowledge and consent because installation programs write to protected areas of the file system and registry.

Installer detection only applies to:

  • 32-bit executable files.
  • Applications without a requested execution level attribute.
  • Interactive processes running as a standard user with UAC enabled.

Before a 32-bit process is created, the following attributes are checked to determine whether it is an installer:

  • The file name includes keywords such as "install," "setup," or "update.
  • "Versioning Resource fields contain the following keywords: Vendor, Company Name, Product Name, File Description, Original Filename, Internal Name, and Export Name.
  • Keywords in the side-by-side manifest are embedded in the executable file.
  • Keywords in specific StringTable entries are linked in the executable file.
  • Key attributes in the resource script data are linked in the executable file.
  • There are targeted sequences of bytes within the executable file.

解决此问题的最简单方法是将 UAC 清单添加到您的应用以指定执行级别:

  • 转到“项目选项 | 应用程序 | 清单”
  • 启用“自动生成”
  • 根据需要设置“执行级别”(在这种情况下,“作为调用者”就足够了)。

否则,您将不得不将您的应用程序重新编译为 64 位,或者更改其名称和版本资源以避免指定的关键字。