RunAsAdmin

program AdminCMD;

{$APPTYPE CONSOLE}

uses
  Windows,
  ShellApi,
  SysUtils;

function RunAsAdmin(const iExeName, iParam: String): Boolean;
var
  SEI: TShellExecuteInfo;
begin
  Result := False;

if (CheckWin32Version(6)) then
  begin
    ZeroMemory(@SEI, SizeOf(SEI));

with SEI do
    begin
      cbSize := SizeOf(SEI);
      Wnd := 0;
      fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
      lpVerb := 'runas';
      lpFile := PChar(iExeName);
      lpParameters := PChar(iParam);
      nShow := SW_SHOW;
    end;

Result := ShellExecuteEx(@SEI);
  end;
end;

var
  CmdPath: String;
begin
  CmdPath := StringOfChar(#0, MAX_PATH);
  ExpandEnvironmentStrings(
    PChar('%ComSpec%'),
    PChar(CmdPath),
    Length(CmdPath));

CmdPath := Trim(CmdPath);

RunAsAdmin(CmdPath, '');
end.

上一篇:Windows下文件加固


下一篇:FZU月赛20160416 ABEF