3、requireAdministrator:在运行应用程序之前提示管理员权限
uses
System.Win.Registry, Winapi.Windows;
procedure isAdmin();
var
reg: TRegistry;
openResult: Boolean;
begin
reg := TRegistry.Create(KEY_READ);
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.Access := KEY_WRITE;
openResult := reg.OpenKey('Software\MyCompanyName\MyApplication\',True);
if not openResult = True then
begin
MessageDlg('无法写入注册表。您的应用程序没有管理员级权限。',
TMsgDlgType.mtError, mbOKCancel, 0);
end
else
begin
MessageDlg('允许写入注册表。您的应用程序具有管理员级权限。',
TMsgDlgType.mtInformation, mbOKCancel, 0);
end;
reg.CloseKey();
reg.Free;
end;
此测试之所以有效,是因为您需要管理员级权限才能写入注册表的HKEY_LOCAL_MACHINE\Software部分。代码段实际上不向注册表写入任何东西,它只请求写入权限。
确保您构建应用程序后,在IDE之外执行它,因为IDE默认情况下会用管理员级别的权限调试应用程序(不尊重应用程序清单)。
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.063119888305664 seconds