for delphi I want to learn if my application has admin rights, is there a solution for this you may know ?
related question:
http://stackoverflow.com/questions/3300023/how-to-launch-an-application-with-admin-rights
for delphi I want to learn if my application has admin rights, is there a solution for this you may know ?
related question:
http://stackoverflow.com/questions/3300023/how-to-launch-an-application-with-admin-rights
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;
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.
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, GENERIC_READ or GENERIC_WRITE or GENERIC_EXECUTE);
Result := H <> 0;
if Result then
CloseServiceHandle(H);
end;
end;