-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.Windows.pas
More file actions
366 lines (313 loc) · 10.3 KB
/
Copy pathMaxLogic.Windows.pas
File metadata and controls
366 lines (313 loc) · 10.3 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
unit maxLogic.Windows;
interface
uses
winApi.Windows, System.Classes, System.SysUtils, System.generics.collections;
type
TSystemUser = record
Node,
Username,
SID: string;
rawRow: string; // the sv row as a whole
end;
function RetrieveWindowsUsers: TArray<TSystemUser>;
{ Run a DOS program and retrieve its output dynamically while it is running. }
function GetDosOutput(CommandLine: string; aWorkDir: string = 'C:\'): string;
type
TStrProc = reference to procedure(const line: string);
// Capture console output in [Realtime] and return its char right away
procedure CaptureConsoleOutput(const aExeFileName, AParameters: string;
OnLineReady: TStrProc; const aWorkingDir: string = '');
function GetFullProcessImageName(APid: dword): string;
/// <summary>
/// checks if the application is already running
/// if aCheckFullPath is true you need to specify the fill file name including the path
/// if aCheckFullPath is false, then only the filename without the path will betested
/// </summary>
function ProcessIsRunning(const aExeName: string; aCheckFullPath: boolean): boolean;
// the appExeName can be either just the filename or a full file name, that is path + filename
function CheckCountProcess(const AppExeName: string; ProcessIds: TList<THandle> = nil): integer;
function QueryFullProcessImageName(
hProcess: THandle;
dwFlags: dword;
lpExeName: PChar;
out lpdwSize: dword
): bool;
stdcall external kernel32 Name 'QueryFullProcessImageNameW';
function RunAsAdmin(hwnd: hwnd; FileName: string; Parameters: string): boolean;
function IsDelphiRunning: boolean;
function ForceForegroundWindow(hwnd: THandle): boolean;
implementation
uses
System.IOUtils, System.StrUtils, maxLogic.IOUtils, AutoFree,
ShellApi, TlHelp32, jclSysInfo;
function RetrieveWindowsUsers: TArray<TSystemUser>;
var
Cmd: string;
Row, l: TStringList;
NodeIndex, NameIndex, SidIndex, X: integer;
u: TSystemUser;
Items: TList<TSystemUser>;
begin
Result := [];
gc(l, TStringList.Create);
gc(Row, TStringList.Create);
Cmd := 'wmic.exe useraccount get name,sid /format:csv';
l.Text := GetDosOutput(Cmd, GetInstallDir);
Row.StrictDelimiter := True;
Row.delimiter := ',';
Row.CaseSensitive := False;
// there are some empty lines there... get rid of them...
for X := l.Count - 1 downto 0 do
if Trim(l[X]) = '' then
l.delete(X);
if l.Count < 2 then
exit;
Row.delimitedText := l[0];
NodeIndex := Row.IndexOf('Node');
NameIndex := Row.IndexOf('Name');
SidIndex := Row.IndexOf('SID');
gc(Items, TList<TSystemUser>.Create);
for X := 1 to l.Count - 1 do
begin
Row.delimitedText := l[X];
u := default(TSystemUser);
if NameIndex <> -1 then
u.Username := Row[NameIndex];
if NodeIndex <> -1 then
u.Node := Row[NodeIndex];
if SidIndex <> -1 then
u.SID := Row[SidIndex];
u.rawRow := l[X];
Items.Add(u);
end;
Result := Items.ToArray;
end;
{ Run a DOS program and retrieve its output dynamically while it is running. }
function GetDosOutput(CommandLine: string; aWorkDir: string = 'C:\'): string;
var
SecurityAttributes: TSecurityAttributes;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: boolean;
pCommandLine: array[0..256] of AnsiChar;
BytesRead: Cardinal;
sCmd, WorkDir: string;
isOk: boolean;
begin
Result := '';
with SecurityAttributes do
begin
nLength := SizeOf(SecurityAttributes);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SecurityAttributes, 0);
try
StartupInfo := default(TStartupInfo);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
// don't redirect stdin
StartupInfo.hStdOutput := StdOutPipeWrite;
StartupInfo.hStdError := StdOutPipeWrite;
WorkDir := aWorkDir;
sCmd := 'cmd.exe /C ' + CommandLine;
UniqueString(sCmd);
isOk := CreateProcess(nil, PChar(sCmd), nil, nil,
True, 0, nil, PChar(WorkDir), StartupInfo, ProcessInfo);
CloseHandle(StdOutPipeWrite);
if isOk then
try
repeat
// To read from the pipe, a process uses the read isOK in a call to the ReadFile function.
// ReadFile returns when one of the following is true:
// a write operation completes on the write end of the pipe,
// the number of bytes requested has been read,
// or an error occurs.
// When a process uses WriteFile to write to an anonymous pipe, the write operation is not completed until all bytes are
// written. If the pipe buffer is full before all bytes are written, WriteFile does not return until another process
// or thread uses ReadFile to make more buffer space available.
WasOK := winApi.Windows.ReadFile(StdOutPipeRead, pCommandLine, 255, BytesRead, nil);
if BytesRead > 0 then
begin
pCommandLine[BytesRead] := #0;
Result := Result + string(pCommandLine);
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
finally
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
Result := Trim(Result);
end;
// Capture console output in [Realtime] and return its char right away
procedure CaptureConsoleOutput(const aExeFileName, AParameters: string;
OnLineReady: TStrProc; const aWorkingDir: string = '');
const
CReadBuffer = 2400;
var
sCmd: string;
SecurityAttributes: TSecurityAttributes;
hRead: THandle;
hWrite: THandle;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
pBuffer: array[0..CReadBuffer + 1] of AnsiChar;
dRead: dword;
dRunning: dword;
WorkingDir: string;
begin
SecurityAttributes.nLength := SizeOf(TSecurityAttributes);
SecurityAttributes.bInheritHandle := True;
SecurityAttributes.lpSecurityDescriptor := nil;
if CreatePipe(hRead, hWrite, @SecurityAttributes, 0) then
begin
StartupInfo := default(TStartupInfo);
StartupInfo.cb := SizeOf(TStartupInfo);
StartupInfo.hStdInput := hRead;
StartupInfo.hStdOutput := hWrite;
StartupInfo.hStdError := hWrite;
StartupInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
sCmd := aExeFileName + ' ' + AParameters;
UniqueString(sCmd);
WorkingDir := aWorkingDir;
if WorkingDir = '' then
WorkingDir := GetInstallDir;
if CreateProcess(nil, PChar(sCmd), @SecurityAttributes,
@SecurityAttributes, True,
NORMAL_PRIORITY_CLASS,
nil, PChar(WorkingDir), StartupInfo, ProcessInfo) then
begin
repeat
dRunning := WaitForSingleObject(ProcessInfo.hProcess, 100);
// Application.ProcessMessages();
repeat
dRead := 0;
ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);
pBuffer[dRead] := #0;
// OemToAnsi(pBuffer, pBuffer);
OnLineReady(string(pBuffer));
until (dRead < CReadBuffer);
until (dRunning <> WAIT_TIMEOUT);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
CloseHandle(hRead);
CloseHandle(hWrite);
end;
end;
function GetFullProcessImageName(APid: dword): string;
var
LBuffer: PChar;
LSize: dword;
LProcess: THandle;
begin
Result := '';
LProcess := OpenProcess(PROCESS_QUERY_INFORMATION, False, APid);
if LProcess <> INVALID_HANDLE_VALUE then
begin
try
LSize := max_path;
LBuffer := GetMemory(LSize * SizeOf(char));
try
if QueryFullProcessImageName(LProcess, 0, LBuffer, LSize) then
begin
Result := LBuffer;
end;
finally
FreeMemory(LBuffer);
end;
finally
CloseHandle(LProcess);
end;
end;
end;
function ProcessIsRunning(const aExeName: string; aCheckFullPath: boolean): boolean;
var
LSnapshot: THandle;
LProcess: TProcessEntry32;
LExe: string;
begin
LSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
try
LProcess.dwSize := SizeOf(LProcess);
if Process32First(LSnapshot, LProcess) then
begin
LExe := ExtractFileName(aExeName);
repeat
if SameText(LProcess.szExeFile, LExe)
and
((not aCheckFullPath)
or SameText(GetFullProcessImageName(LProcess.th32ProcessID), aExeName)) then
exit(True);
until not Process32Next(LSnapshot, LProcess);
end;
Result := False;
finally
CloseHandle(LSnapshot)
end;
end;
function RunAsAdmin(hwnd: hwnd; FileName: string; Parameters: string): boolean;
{
source: https://stackoverflow.com/questions/923350/delphi-prompt-for-uac-elevation-when-needed
See Step 3: Redesign for UAC Compatibility (UAC)
http://msdn.microsoft.com/en-us/library/bb756922.aspx
This code is released into the public domain. No attribution required.
}
var
sei: TShellExecuteInfo;
begin
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.wnd := hwnd;
sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
sei.lpVerb := PChar('runas');
sei.lpFile := PChar(FileName); // PAnsiChar;
if Parameters <> '' then
sei.lpParameters := PChar(Parameters); // PAnsiChar;
sei.nShow := SW_SHOWNORMAL; // Integer;
Result := ShellExecuteEx(@sei);
end;
function IsDelphiRunning: boolean;
begin
Result := FindWindow('TAppBuilder', nil) > 0;
end;
function ForceForegroundWindow(hwnd: THandle): boolean;
begin
if IsIconic(hwnd) then
ShowWindow(hwnd, SW_RESTORE);
SetActiveWindow(hwnd);
// BringWindowToTop(hWnd);
SetForegroundWindow(hwnd);
Result := (GetForegroundWindow = hwnd);
end; { ForceForegroundWindow }
function CheckCountProcess(const AppExeName: string; ProcessIds: TList<THandle> = nil): integer;
var
FHandle: THandle;
l: TStringList;
UseFullPath: boolean;
fn: string;
X: integer;
begin
Result := 0;
UseFullPath := pos('\', AppExeName) > 0;
fn := AnsiLowercase(AppExeName);
l := TStringList.Create;
jclSysInfo.RunningProcessesList(l, UseFullPath);
for X := 0 to l.Count - 1 do
if fn = AnsiLowercase(l[X]) then
begin
Result := Result + 1;
if assigned(ProcessIds) then
ProcessIds.Add(THandle(l.Objects[X]))
end;
l.Free;
end;
end.