Delphi Administrator rights D7 W7 [duplicate]

一个人想着一个人 提交于 2019-12-02 22:32:03

问题


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 pressing "Disable UAC for this application" button).


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6977893/delphi-administrator-rights-d7-w7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!