Delphi Administrator rights D7 W7 [duplicate]

柔情痞子 提交于 2019-12-08 09:50:35

问题


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.



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

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