-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathockwindowsapi.pas
104 lines (88 loc) · 2.59 KB
/
ockwindowsapi.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
unit OckWindowsAPI;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LazFileUtils, osLog,
DSiWin32,
ShellAPI,
OckSystemAPI
;
type
{ TWindowsAPI }
TWindowsAPI = class(TSystemAPI)
private
function RunAsAdmin(const Handle: DWord; const Path: string; Params: string): Boolean;
procedure RunApplicationElevated(const PathToExe:string);
public
Handle: DWord;
function IsAdmin:boolean;override;
procedure RunApplication(const PathToExe:string);override;
constructor Create(const Handle:DWord);
destructor Destroy;override;
end;
var
SystemAPI : TSystemAPI;
implementation
{ TWindowsAPI }
function TWindowsAPI.IsAdmin: boolean;
begin
result := DSiIsAdmin;
end;
function TWindowsAPI.RunAsAdmin(const Handle: DWord; const Path: string;
Params: string): Boolean;
var
ShellExecuteInfoA: TShellExecuteInfoA;
begin
FillChar(ShellExecuteInfoA, SizeOf(ShellExecuteInfoA), 0);
with ShellExecuteInfoA do
begin
cbSize := SizeOf(ShellExecuteInfoA);
Wnd := Handle;
fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
lpVerb := 'runas';
lpFile := PAnsiChar(Path);
lpParameters := PAnsiChar(Params);
nShow := 1;
end;
Result := ShellExecuteExA(@ShellExecuteInfoA);
end;
procedure TWindowsAPI.RunApplication(const PathToExe: string);
var
//Shell,
//ShellOptions,
//ShellCommand,
//ShellOutput: String;
PathToExe: String;
begin
try
//Result := '';
PathToExe := TrimFilename(AppLocation + 'images_to_depot\images_to_depot.exe');
LogDatei.log('Saving images on depot...',LLInfo);
{set shell and options}
//Shell := 'powershell.exe';//PathToExe;//'powershell.exe';
//ShellOptions := '/c'; //-Verb runAs 'Start-Process PowerShell -Verb RunAs | '
//ShellCommand := 'Start-Process ' + QuotedStr(PathToExe) + ' -Verb RunAs';
//if RunCommand(Shell, ['Start-Process', QuotedStr(PathToExe), '-Verb RunAs'], ShellOutput) then
if RunAsAdmin(Handle, PathToExe,'') then
begin
//ShellCommand := '';
//Result := ShellOutput;
LogDatei.log('Images saved on depot', LLInfo);
//LogDatei.log('Shell command: ' + ShellCommand, LLDebug);
//ShowMessage(ShellOutput);
end
else
begin
//Result := '';
LogDatei.log('Error while trying to run as admin ' + PathToExe, LLError);
end;
except
//Result := '';
LogDatei.log('Exception during SaveImagesOnDepot', LLDebug);
end;
end;
initialization
SystemAPI := TWindowsAPI.Create;
finalization
FreeAndNil(SystemAPI);
end.