如何使DELPHI程序在Win7下自动请求以管理员身份运行

来源:互联网 发布:邮箱如何注册淘宝账号 编辑:程序博客网 时间:2024/05/19 03:19

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;

0 0
原创粉丝点击