for delphi I want to learn if my application has admin rights, is there a solution for this you may know ?
开发者_如何学运维related question:
How to launch an application with admin rights?
Just attempt to do something that requires administrative privileges:
uses
WinSvc;
function IsAdmin(Host : string = '') : Boolean;
var
H: SC_HANDLE;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then
Result := True
else begin
H := OpenSCManager(PChar(Host), nil, SC_MANAGER_ALL_ACCESS);
Result := H <> 0;
if Result then
CloseServiceHandle(H);
end;
end;
You call the WinAPI function GetTokenInformation, passing TokenElevation. There's a C++ example here which should be easy to convert.
Note that being the administrator and being elevated are different.
As to knowing wether or not your program has admin rights, I have no code for that, but This might be an idea. Note that I just wrote it and is untested.
But the idea is if you are able to create a file at the program files
folder then you probably have admin rights.
function IsRunningWithAdminPrivs: Boolean;
begin
var
List: TStringList;
begin
List := TStringList.Create;
try
try
List.Text := 'Sample';
// Use SHGetFolder path to retreive the program files folder
// here is hardcoded for the sake of the example
List.SaveToFile('C:\program files\test.txt');
Result := True;
except
Result := False;
end;
finally
List.Free;
DeleteFile('C:\program files\test.txt');
end;
end;
精彩评论