本文整理自网络,侵删。
方法一:1、编辑文件UAC.manifest,内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
< trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
< /trustInfo>
< /assembly>
2、编辑文件uac.rc,内容如下:
1 24 UAC.manifest
3.将rc文件编译为res文件:
brcc32 UAC.rc //brcc32 在delphiX\Bin目录下
4、函数过程及编译参数:
{$R uac.res}
function RunAsAdmin(hWnd: hWnd; filename: string; Parameters: string): Boolean;
{
See Step 3: Redesign for UAC Compatibility (UAC)
http://msdn.microsoft.com/en-us/library/bb756922.aspx
}
var
sei: TShellExecuteInfo;
begin
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.Wnd := hWnd;
sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
sei.lpVerb := PChar('runas');
sei.lpFile := PChar(filename); // PAnsiChar;
if Parameters <> '' then
sei.lpParameters := PChar(Parameters); // PAnsiChar;
sei.nShow := SW_SHOWNORMAL; //Integer;
Result := ShellExecuteEx(@sei);
end;
方法2: 最新版的DELPHIxe 自带选项如图设置:
相关阅读 >>
Delphi 利用hook api函数openprocess与terminateprocess来防止任务管理器结束进程
Delphi自动以管理员身份在vista 和 windows7 下运行程序
Delphi 如何在toolbar中的toolbutton上显示文字
Delphi idhttp访问datasnap有密码验证的中间件
更多相关阅读请进入《Delphi》频道 >>