Delphi Administrator rights D7 W7 [duplicate]

2019-06-14 12:12发布

问题:

Possible Duplicates:
Delphi: Prompt for UAC elevation when needed
Delphi Administrator rights D7 W7

I'm using Delphi 7 on Windows 7. I need administrator privileges for some function in my application, which needs RAW access to the physical drive. How do I elevate to Administrator from source code? I.E. I chceck rights with:

function IsUserAdmin : boolean;
const CAdminSia : TSidIdentifierAuthority = (value: (0, 0, 0, 0, 0, 5));
var sid : PSid;
    ctm : function (token: dword; sid: pointer; var isMember: bool) : bool; stdcall;
    b1  : bool;
begin
  result := false;
  ctm := GetProcAddress(LoadLibrary('advapi32.dll'), 'CheckTokenMembership');
  if (@ctm <> nil) and AllocateAndInitializeSid(CAdminSia, 2, $20, $220, 0, 0, 0, 0, 0, 0, sid) then
  begin
    result := ctm(0, sid, b1) and b1;
    FreeSid(sid);
  end;
end;

If the application is started as administrator, then return True if not then False. Now if I have False as the result, I want to automatically elevate to Administrator.

I tried manifest elevation to administrator, but if I start the application, then I see the UAC prompt. If i answer NO, then application will not run at all.

EDIT: Or disable UAC only for this application (ParamStr(0)) also from code (after pressing "Disable UAC for this application" button

回答1:

You cannot just "Elevate" for your application mid-execution. What you would have to do is have another application or COM DLL that you call from your application, and you specify the required privileges when doing so.