|
@@ -1,13 +1,13 @@
|
|
{ ***************************************************************************
|
|
{ ***************************************************************************
|
|
|
|
|
|
- Copyright (c) 2016-2018 Kike Pérez
|
|
|
|
|
|
+ Copyright (c) 2016-2021 Kike Pérez
|
|
|
|
|
|
Unit : Quick.Process
|
|
Unit : Quick.Process
|
|
Description : Process functions
|
|
Description : Process functions
|
|
Author : Kike Pérez
|
|
Author : Kike Pérez
|
|
Version : 1.5
|
|
Version : 1.5
|
|
Created : 14/07/2017
|
|
Created : 14/07/2017
|
|
- Modified : 15/12/2020
|
|
|
|
|
|
+ Modified : 08/07/2021
|
|
|
|
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
|
|
|
|
@@ -34,8 +34,10 @@ unit Quick.Process;
|
|
interface
|
|
interface
|
|
|
|
|
|
uses
|
|
uses
|
|
|
|
+ {$IFDEF MSWINDOWS}
|
|
Windows,
|
|
Windows,
|
|
- Classes,
|
|
|
|
|
|
+ ShellAPI,
|
|
|
|
+ Quick.Console,
|
|
{$IFNDEF CONSOLE}
|
|
{$IFNDEF CONSOLE}
|
|
Controls,
|
|
Controls,
|
|
{$IFNDEF FPC}
|
|
{$IFNDEF FPC}
|
|
@@ -43,23 +45,40 @@ uses
|
|
Winapi.Messages,
|
|
Winapi.Messages,
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
- DateUtils,
|
|
|
|
{$IFNDEF FPC}
|
|
{$IFNDEF FPC}
|
|
- TlHelp32,
|
|
|
|
- psapi,
|
|
|
|
|
|
+ TlHelp32,
|
|
|
|
+ psapi,
|
|
|
|
+ {$ELSE}
|
|
|
|
+ JwaTlHelp32,
|
|
|
|
+ Process,
|
|
|
|
+ {$ENDIF}
|
|
{$ELSE}
|
|
{$ELSE}
|
|
- JwaTlHelp32,
|
|
|
|
- Process,
|
|
|
|
|
|
+ Posix.Base,
|
|
|
|
+ Posix.Fcntl,
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+ Classes,
|
|
|
|
+ DateUtils,
|
|
SysUtils,
|
|
SysUtils,
|
|
- ShellAPI,
|
|
|
|
Quick.Commons;
|
|
Quick.Commons;
|
|
|
|
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ type
|
|
|
|
+ TStreamHandle = pointer;
|
|
|
|
+ function popen(const command: PAnsiChar; const _type: PAnsiChar): TStreamHandle; cdecl; external libc name _PU + 'popen';
|
|
|
|
+ function pclose(filehandle: TStreamHandle): int32; cdecl; external libc name _PU + 'pclose';
|
|
|
|
+ function fgets(buffer: pointer; size: int32; Stream: TStreamHAndle): pointer; cdecl; external libc name _PU + 'fgets';
|
|
|
|
+ {$ENDIF}
|
|
|
|
+
|
|
|
|
|
|
//stop a running process
|
|
//stop a running process
|
|
|
|
+ {$IFDEF MSWINDOWS}
|
|
function KillProcess(const aFileName : string) : Integer; overload;
|
|
function KillProcess(const aFileName : string) : Integer; overload;
|
|
|
|
+ {$ELSE}
|
|
|
|
+ function KillProcess(const aProcessName : string) : Integer; overload;
|
|
|
|
+ {$ENDIF}
|
|
function KillProcess(aProcessId : Cardinal) : Boolean; overload;
|
|
function KillProcess(aProcessId : Cardinal) : Boolean; overload;
|
|
//run process as Admin privilegies
|
|
//run process as Admin privilegies
|
|
|
|
+ {$IFDEF MSWINDOWS}
|
|
function RunAsAdmin(hWnd: HWND; const aFilename, aParameters: string): Boolean;
|
|
function RunAsAdmin(hWnd: HWND; const aFilename, aParameters: string): Boolean;
|
|
//impersonate logon
|
|
//impersonate logon
|
|
function Impersonate(const aDomain, aUser, aPassword : string): Boolean;
|
|
function Impersonate(const aDomain, aUser, aPassword : string): Boolean;
|
|
@@ -79,6 +98,10 @@ uses
|
|
//executes an aplication and wait for terminate
|
|
//executes an aplication and wait for terminate
|
|
function ExecuteAndWait(const aFilename, aCommandLine: string): Boolean;
|
|
function ExecuteAndWait(const aFilename, aCommandLine: string): Boolean;
|
|
function ShellExecuteAndWait(const aOperation, aFileName, aParameter, aDirectory : string; aShowMode : Word; aWaitForTerminate: Boolean) : LongInt;
|
|
function ShellExecuteAndWait(const aOperation, aFileName, aParameter, aDirectory : string; aShowMode : Word; aWaitForTerminate: Boolean) : LongInt;
|
|
|
|
+ {$ENDIF}
|
|
|
|
+ //runs a command and gets console output
|
|
|
|
+ function RunCommand(const aFilename, aParameters : string) : TStringList;
|
|
|
|
+ {$IFDEF MSWINDOWS}
|
|
{$IFNDEF FPC}
|
|
{$IFNDEF FPC}
|
|
//execute an application and return handle
|
|
//execute an application and return handle
|
|
function ShellExecuteReturnHandle(const aOperation, aFileName, aParameters, aWorkingDir : string; aShowMode: Integer) : THandle;
|
|
function ShellExecuteReturnHandle(const aOperation, aFileName, aParameters, aWorkingDir : string; aShowMode: Integer) : THandle;
|
|
@@ -93,10 +116,12 @@ uses
|
|
//capture a window handle and show it into a wincontrol
|
|
//capture a window handle and show it into a wincontrol
|
|
procedure CaptureWindowIntoControl(aWindowHandle: THandle; aContainer: TWinControl);
|
|
procedure CaptureWindowIntoControl(aWindowHandle: THandle; aContainer: TWinControl);
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+ {$ENDIF}
|
|
|
|
|
|
|
|
|
|
implementation
|
|
implementation
|
|
|
|
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
const
|
|
const
|
|
DNLEN = 15;
|
|
DNLEN = 15;
|
|
UNLEN = 256;
|
|
UNLEN = 256;
|
|
@@ -202,7 +227,9 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+{$ENDIF}
|
|
|
|
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
function KillProcess(const aFileName: string): Integer;
|
|
function KillProcess(const aFileName: string): Integer;
|
|
const
|
|
const
|
|
PROCESS_TERMINATE = $0001;
|
|
PROCESS_TERMINATE = $0001;
|
|
@@ -230,8 +257,22 @@ begin
|
|
end;
|
|
end;
|
|
CloseHandle(FSnapshotHandle);
|
|
CloseHandle(FSnapshotHandle);
|
|
end;
|
|
end;
|
|
|
|
+{$ELSE}
|
|
|
|
+function KillProcess(const aProcessName: string): Integer;
|
|
|
|
+var
|
|
|
|
+ sl : TStringList;
|
|
|
|
+begin
|
|
|
|
+ sl := RunCommand('pkill',aProcessName);
|
|
|
|
+ try
|
|
|
|
+ Result := 1;
|
|
|
|
+ finally
|
|
|
|
+ sl.Free;
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
+{$ENDIF}
|
|
|
|
|
|
function KillProcess(aProcessId : Cardinal) : Boolean;
|
|
function KillProcess(aProcessId : Cardinal) : Boolean;
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
var
|
|
var
|
|
hProcess : THandle;
|
|
hProcess : THandle;
|
|
begin
|
|
begin
|
|
@@ -244,7 +285,20 @@ begin
|
|
CloseHandle(hProcess);
|
|
CloseHandle(hProcess);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
+{$ELSE}
|
|
|
|
+var
|
|
|
|
+ sl : TStringList;
|
|
|
|
+begin
|
|
|
|
+ sl := RunCommand('kill',aProcessId.ToString);
|
|
|
|
+ try
|
|
|
|
+ Result := True;
|
|
|
|
+ finally
|
|
|
|
+ sl.Free;
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
+{$ENDIF}
|
|
|
|
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
function RunAsAdmin(hWnd: HWND; const aFilename, aParameters: string): Boolean;
|
|
function RunAsAdmin(hWnd: HWND; const aFilename, aParameters: string): Boolean;
|
|
var
|
|
var
|
|
shinfo: TShellExecuteInfo;
|
|
shinfo: TShellExecuteInfo;
|
|
@@ -435,7 +489,38 @@ begin
|
|
SetLastError(dwExitCode);
|
|
SetLastError(dwExitCode);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
+{$ENDIF}
|
|
|
|
+
|
|
|
|
+function RunCommand(const aFilename, aParameters : string) : TStringList;
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
|
|
+begin
|
|
|
|
+ Result := TStringList.Create;
|
|
|
|
+ RunConsoleCommand(aFilename,aParameters,nil,Result);
|
|
|
|
+end;
|
|
|
|
+{$ELSE}
|
|
|
|
+var
|
|
|
|
+ Handle: TStreamHandle;
|
|
|
|
+ Data: array[0..511] of uint8;
|
|
|
|
+ command : PAnsiChar;
|
|
|
|
+begin
|
|
|
|
+ Result := TStringList.Create;
|
|
|
|
+ try
|
|
|
|
+ if aParameters.IsEmpty then command := PAnsiChar(AnsiString(aFilename))
|
|
|
|
+ else command := PAnsiChar(AnsiString(aFilename + ' ' + aParameters));
|
|
|
|
+ Handle := popen(command, 'r');
|
|
|
|
+ try
|
|
|
|
+ while fgets(@Data[0], Sizeof(Data), Handle) <> nil do Result.Add(Utf8ToString(@Data[0]));
|
|
|
|
+ finally
|
|
|
|
+ pclose(Handle);
|
|
|
|
+ end;
|
|
|
|
+ except
|
|
|
|
+ on E: Exception do Exception.CreateFmt('RunCommand: %s',[e.Message]);
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
|
|
|
|
+{$ENDIF}
|
|
|
|
+
|
|
|
|
+{$IFDEF MSWINDOWS}
|
|
function ShellExecuteAndWait(const aOperation, aFileName, aParameter, aDirectory: string; aShowMode : Word; aWaitForTerminate: Boolean) : LongInt;
|
|
function ShellExecuteAndWait(const aOperation, aFileName, aParameter, aDirectory: string; aShowMode : Word; aWaitForTerminate: Boolean) : LongInt;
|
|
var
|
|
var
|
|
done: Boolean;
|
|
done: Boolean;
|
|
@@ -562,5 +647,6 @@ begin
|
|
SetForegroundWindow(aWindowHandle);
|
|
SetForegroundWindow(aWindowHandle);
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+{$ENDIF}
|
|
|
|
|
|
end.
|
|
end.
|