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).
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.