开发者

Delphi Administrator rights D7 W7 [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Delphi: Prompt for UAC elevation when needed

My application written in Delphi 7 for Windows 7 requires administrator privileges for some functionality. How can I elevate it to Administrator from source code?

I check the user rights with this code:

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 started as administrator, then it returns True; if not, then False. Now if I have False as the result I want to automatically elevate the program to Administrator.

I tried with manifest elevate to administrator, but if I start the application, then I see a UAC prompt and if I answer "No", then application will not run at all.

Any chance for help?

I need administrator rights for raw access to the physical drive.

EDIT:

I also tried to disable UAC only for this application (ParamStr(0)) also from code (after p开发者_运维问答ressing "Disable UAC for this application" button).


Processes receive their token at startup and then cannot change them. Thus if you want an app that appears to elevate for some subset of its functionality, that functionality must involve a new process. What you cannot do is elevate an existing process.


If you want it to have administrator rights, then you have to go through UAC. You can't elevate to administrator without it showing the UAC prompt, unless UAC is disabled. Obviously, you have to choose YES on the UAC prompt for it to be given administrator privileges.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜