以管理员身份运行此程序(或在必要时刻如何获取UAC管理员许可证) [英] Run this program as an administrator (Or how to get the licence UAC Admin during the necessary moment)

查看:249
本文介绍了以管理员身份运行此程序(或在必要时刻如何获取UAC管理员许可证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第二次尝试收到一个问题的答案。非常抱歉,但是我的英文很糟糕,所以我很难解释一下我想要的,特别是技术问题:_(



我再次尝试这个时候有一个具体的例子,对我来说是有必要的。



DelphiXe,Win7x64。Windows Uac是On,用户使用管理员的权限。

应该从另一个地方复制文件的程序(示例)
她应该以通常的方式启动(不是代表管理员)因此,不需要粘贴到一个宣言(*。rc)EXE文件(其中授予管理员的权限 - 更多的启动请求)
复制应该以常规方式执行 - 管理员的权限只有在需要的情况下才应该被请求,而不重新启动程序。



问题(代码中标有*):
1.如何定义Windows UAC是否存在于系统中以及是否启用
2.如何获取管理员权限(的dedu ce消息Windows UAC)仅在需要的情况下实际获得该程序的许可证而不重新启动它



示例 - 文件管理器的远程管理器(或总指挥官 ')可以这样做 - 他们在通常的启动时复制文件(而不是代表管理员),并且只有当业务涉及系统文件夹时才引起查询UAC。因此,程序不会重新启动,并首先发出预防。



P.S。非常感谢我的帖子的帮助和编辑。



程序:表单,一个按钮,opendialog,savedialog



<单位Unit1; pre>

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,窗体,
对话框,StdCtrls;

type
TForm1 = class(TForm)
OpenDialogFROM:TOpenDialog;
Button1:TButton;
SaveDialogTO:TSaveDialog;
procedure Button1Click(Sender:TObject);
procedure FormCreate(Sender:TObject);
private
{私有声明}
public
{公开声明}
函数TestPathWrite(path:string):bool;
结束

var
Form1:TForm1;

实现

{$ R * .dfm}

函数IsWindowsUAC_Enabled:bool; //测试Windows UAC打开(*)
begin
结果:= false;
// ????
//如何定义,UAC是否包含在系统启用中(我们承认,我们在操作系统中工作的是更高级的XP)
end;

函数TurnOnAdminRight:bool; //激活管理员操作的权限(*)
begin
结果:= false;
// ????
//如何激活消息Windows UAC(大概允许将此程序更改为计算机或类似的东西)
//并获得该程序的管理员的许可证?
结束

函数TForm1.TestPathWrite(path:string):bool;
var f:file; Err:integer;
begin
结果:= false; assignfile(f,IncludeTrailingPathDelimiter(path)+'$$ TestFile $$。tmp');
{$ I-}
重写(f);
{$ I +}
Err:= IoResult;
如果Err = 0然后开始
如果Err = 5然后开始//访问拒绝
如果IsWindowsUAC_Enabled然后// Windows UAC是ON
如果TurnOnAdminRight = True然后TestPathWrite(路径); //重复检查,否则退出whith错误消息
end;
Showmessage('错误写入路径:'+路径+',错误:'+ inttostr(Err));
退出;
结束
CloseFile(f);擦除(f);结果:= true;
结束

procedure TForm1.Button1Click(Sender:TObject);
begin
//可以要求管理员权限的测试程序
//它也可以是Windows注册表中的记录过程或可以要求权限的其他记录过程,不能要求
//只有在需要时才要求权限(并包含)的问题

如果OpenDialogFROM.Execute然后如果SaveDialogTO.Execute然后
if FileExists(OpenDialogFROM.FileName)= true then
如果TestPathWrite(ExtractfilePath(SaveDialogTO.FileName))= true then
如果CopyFile(Pchar(OpenDialogFROM.FileName),Pchar(SaveDialogTO.FileName),true)= true然后
Showmessage('File:'+ OpenDialogFROM.FileName +'它成功复制为:'+ SaveDialogTO.FileName);
结束

procedure TForm1.FormCreate(Sender:TObject);
begin
SaveDialogTo.Options:= [ofNoTestFileCreate,ofEnableSizing,ofDontAddToRecent]; // SaveDialog不执行记录
end;

结束。

例如在



从e:\MyNew.txt中的d:\MyTest.txt



而有关权限查询UAC的消息应该出现在例如



从d:\MyTest.txt c:\Windows\MyNew.txt

解决方案

您可以使用此功能检查UAC是否活动

 界面

使用
注册表,SysUtils;

函数IsUACActive:Boolean;

实现

函数IsUACActive:Boolean;
var
注册:TRegistry;
begin
结果:= FALSE;

//有一个机会是活跃的,因为我们在Vista或Windows 7上。现在检查注册表
如果CheckWin32Version(6,0)然后
开始
Reg:= TRegistry.Create;
try
Reg.RootKey:= HKEY_LOCAL_MACHINE;

如果Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System')然后
begin
if(Reg.ValueExists('EnableLUA '))和(Reg.ReadBool('EnableLUA'))然后
结果:= TRUE;
结束
finally
FreeAndNil(Reg);
结束
结束
结束

您可以使用以下功能运行升级的过程:

  ... 

接口

使用
Windows,ShellAPI,Forms;

type
TExecuteFileOption =(
eoHide,
eoWait,
eoElevate
);
TExecuteFileOptions = TExecuteFileOption的集合;

函数Exec​​uteFile(Handle:HWND; const Filename,Paramaters:String; Options:TExecuteFileOptions):Integer;

实现

函数Exec​​uteFile(Handle:HWND; const Filename,Paramaters:String; Options:TExecuteFileOptions):Integer;
var
ShellExecuteInfo:TShellExecuteInfo;
ExitCode:DWORD;
begin
结果:= -1;

ZeroMemory(@ShellExecuteInfo,SizeOf(ShellExecuteInfo));
ShellExecuteInfo.cbSize:= SizeOf(TShellExecuteInfo);
ShellExecuteInfo.Wnd:= Handle;
ShellExecuteInfo.fMask:= SEE_MASK_NOCLOSEPROCESS;

if(eoElevate in Options)和(IsUACActive)then
ShellExecuteInfo.lpVerb:= PChar('runas');

ShellExecuteInfo.lpFile:= PChar(文件名);

如果Paramaters<> ''然后
ShellExecuteInfo.lpParameters:= PChar(参数);

//显示或隐藏窗口
如果选项中的eoHide然后
ShellExecuteInfo.nShow:= SW_HIDE
else
ShellExecuteInfo.nShow:= SW_SHOWNORMAL;

如果ShellExecuteEx(@ShellExecuteInfo)然后
结果:= 0;

if(Result = 0)和(eoWait in Options)然后
begin
GetExitCodeProcess(ShellExecuteInfo.hProcess,ExitCode);

while(ExitCode = STILL_ACTIVE)and
(not Application.Terminated)do
begin
sleep(50);

GetExitCodeProcess(ShellExecuteInfo.hProcess,ExitCode);
结束

结果:= ExitCode;
结束
结束

要运行高级隐藏进程并等待退出:

$ b $执行文件(Self.Handle,'Filename','Parameters',[eoHide,eoWait,eoElevate]); b

  

希望这有助于


It is the second attempt to receive the answer on a question. Very much I apologise, but my English very bad, therefore it is very difficult to me to explain that I want, especially in technical questions :_(

I try once again, this time with a concrete example, that it is necessary for me.

DelphiXe, Win7x64. Windows Uac is On. The user works with the rights of the Administrator.

The program (example) which should copy files from one place in another is given. She should be started by usual way (not on behalf of the Admin). Therefore gluing to a manifes(*.rc) EXE-file (which grants the rights of the Admin - requests at start more truly) is not required. Copying should be carried out by usual way - the rights of Admin should be requested only in case of need and without restart of the program.

Problem (in a code are marked "*"): 1. How to define that Windows UAC is present at system and whether it is Enabled 2. How to get the licence of Admin right (to deduce message Windows UAC) only in case of need and actually to get this licence for the program not restarting it

Example - file managers 'Far manager' (or 'Total Commander') can so to do - they copy files at usual start (not on behalf of the Admin), and cause inquiry UAC only when business concerns system folders. Thus programs are not restarted and at first give out the prevention.

P.S. It is grateful for the help and editing of my posts.

Program: Form, one button, opendialog, savedialog

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialogFROM: TOpenDialog;
    Button1: TButton;
    SaveDialogTO: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Function TestPathWrite(path:string):bool;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function IsWindowsUAC_Enabled:bool; // Test Windows UAC turn on (*)
begin
Result:=false;
// ????
// How to define, whether function UAC is included in system - enabled (we will admit, that we work in OS is more senior XP)
end;

Function TurnOnAdminRight:bool; // To activate the rights of the Administrator to operation (*)
begin
Result:=false;
// ????
// How to activate message Windows UAC (approximately "To allow to make to this program changes to the computer?" or something similar)
// and to get the licence of the Administrator for this program?
end;

Function TForm1.TestPathWrite(path:string):bool;
var f:file;Err:integer;
begin
Result:=false;assignfile(f,IncludeTrailingPathDelimiter(path)+'$$TestFile$$.tmp');
{$I-}
Rewrite(f);
{$I+}
Err:=IoResult;
If Err<>0 then begin
   if Err=5 then begin // Access denided
      if IsWindowsUAC_Enabled then // Windows UAC is ON
         if TurnOnAdminRight=True then TestPathWrite(path); // Repeated check, else exit whith error message
   end;
Showmessage('Error write to path: '+path+', Error: '+inttostr(Err));
Exit;
end;
CloseFile(f);Erase(f);Result:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
// Test procedure by which it can be demanded the rights of the Administrator
// It also could be record procedure in windows register or another by which the rights can be demanded, and can't be demanded
// The problem to request the rights (and to include) only when they are necessary

if OpenDialogFROM.Execute then if SaveDialogTO.Execute then
if FileExists(OpenDialogFROM.FileName)=true then
if TestPathWrite(ExtractfilePath(SaveDialogTO.FileName))=true then
if CopyFile(Pchar(OpenDialogFROM.FileName),Pchar(SaveDialogTO.FileName),true)=true then
Showmessage('File: '+OpenDialogFROM.FileName+' it is successfully copied as: '+SaveDialogTO.FileName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SaveDialogTo.Options:=[ofNoTestFileCreate,ofEnableSizing,ofDontAddToRecent]; // SaveDialog does not do check on record
end;

end.

Copying will be normal for example at

From d:\MyTest.txt in e:\MyNew.txt

And the message with inquiry of rights UAC should to appear for example at

From d:\MyTest.txt in c:\Windows\MyNew.txt

解决方案

You can check if UAC is active using this function

interface

uses
  Registry, SysUtils;

function IsUACActive: Boolean;

implementation

function IsUACActive: Boolean;
var
  Reg: TRegistry;
begin
  Result := FALSE;

  // There's a chance it's active as we're on Vista or Windows 7. Now check the registry
  if CheckWin32Version(6, 0) then
  begin
    Reg := TRegistry.Create;
    try
      Reg.RootKey := HKEY_LOCAL_MACHINE;

      if Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System') then
      begin
        if (Reg.ValueExists('EnableLUA')) and (Reg.ReadBool('EnableLUA')) then
          Result := TRUE;
      end;
    finally
      FreeAndNil(Reg);
    end;
  end;
end;

You can run an elevated process using the following function:

...

interface

uses
  Windows, ShellAPI, Forms;

type
  TExecuteFileOption = (
    eoHide,
    eoWait,
    eoElevate
  );
  TExecuteFileOptions = set of TExecuteFileOption;

function ExecuteFile(Handle: HWND; const Filename, Paramaters: String; Options: TExecuteFileOptions): Integer;

implementation

function ExecuteFile(Handle: HWND; const Filename, Paramaters: String; Options: TExecuteFileOptions): Integer;
var
  ShellExecuteInfo: TShellExecuteInfo;
  ExitCode: DWORD;
begin
  Result := -1;

  ZeroMemory(@ShellExecuteInfo, SizeOf(ShellExecuteInfo));
  ShellExecuteInfo.cbSize := SizeOf(TShellExecuteInfo);
  ShellExecuteInfo.Wnd := Handle;
  ShellExecuteInfo.fMask := SEE_MASK_NOCLOSEPROCESS;

  if (eoElevate in Options) and (IsUACActive) then
    ShellExecuteInfo.lpVerb := PChar('runas');

  ShellExecuteInfo.lpFile := PChar(Filename);

  if Paramaters <> '' then
    ShellExecuteInfo.lpParameters := PChar(Paramaters);

  // Show or hide the window
  if eoHide in Options then
    ShellExecuteInfo.nShow := SW_HIDE
  else
    ShellExecuteInfo.nShow := SW_SHOWNORMAL;

  if ShellExecuteEx(@ShellExecuteInfo) then
    Result := 0;

  if (Result = 0) and (eoWait in Options) then
  begin
    GetExitCodeProcess(ShellExecuteInfo.hProcess, ExitCode);

    while (ExitCode = STILL_ACTIVE) and
          (not Application.Terminated) do
    begin
      sleep(50);

      GetExitCodeProcess(ShellExecuteInfo.hProcess, ExitCode);
    end;

    Result := ExitCode;
  end;
end;

To run an elevated, hidden process and wait for it to exit:

ExecuteFile(Self.Handle, 'Filename', 'Parameters', [eoHide, eoWait, eoElevate]);

Hope this helps

这篇关于以管理员身份运行此程序(或在必要时刻如何获取UAC管理员许可证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆