프로그래밍 초보 탈출

Libraries/Delphi Library

[System] Execute Library 2

째즈토끼 2022. 6. 17. 18:00
unit LibExecuteEx;

interface

uses
  Windows, SysUtils, ShellApi, WinBase, WinNT, WTSApi32, StrUtils, UserEnv,
  LibLog, LibProcessToken;

function EXEC_CreateProcess(
			ApplicationName : PChar;
			Parameter : PChar = nil;
			Directory : PChar = nil;
			AsUserToken: THandle = 0;
			WaitForInputIdle : Boolean = FALSE;
			WaitForTermination : Boolean = FALSE;
			WaitingThread : THandle = 0;
                        EnvironmentBlock: Pointer = nil;
                        InheritHandles: BOOL = FALSE;
                        CreationFlags: DWORD = 0;
                        Desktop: PChar = nil;
			Show : Integer = SW_SHOW) : BOOL;

function EXEC_CreateProcessAsConsoleUser(
			ApplicationName : PChar;
			Parameter : PChar = nil;
			Directory : PChar = nil;
			WaitForInputIdle : Boolean = FALSE;
			WaitForTermination : Boolean = FALSE;
			WaitingThread : THandle = 0;
                        InheritHandles: BOOL = FALSE;
                        CreationFlags: DWORD = 0;
                        Desktop: PChar = nil;
			Show : Integer = SW_SHOW) : BOOL;

 

 

[System] Process Token Library

unit LibProcessToken; interface uses Windows, SysUtils, WinNT, WinBase, Sddl, WTSApi32, TlHelp32, LibSystemConst, LibProcessInfo; function GetProcessHandle(Filename: String): THandle; // CloseHandle..

jazz16.tistory.com

function EXEC_CreateProcess(
			ApplicationName : PChar;
			Parameter : PChar = nil;
			Directory : PChar = nil;
			AsUserToken: THandle = 0;
 			WaitForInputIdle : Boolean = FALSE;
			WaitForTermination : Boolean = FALSE;
			WaitingThread : THandle = 0;
                        EnvironmentBlock: Pointer = nil;
                        InheritHandles: BOOL = FALSE;
                        CreationFlags: DWORD = 0;
                        Desktop: PChar = nil;
			Show : Integer = SW_SHOW) : BOOL;
const
  WinSta0_Default_Desktop : PChar = 'winsta0\default';
var
  ARGU 			: String;
  SI			: TStartupInfo;
  PI			: TProcessInformation;
begin
  ZeroMemory(@SI, SizeOf(SI));
  ZeroMemory(@PI, SizeOf(PI));

  SI.cb := SizeOf(SI);
  if Assigned(Desktop)
     then SI.lpDesktop := Desktop
     else SI.lpDesktop := WinSta0_Default_Desktop;

  SI.dwFlags := SI.dwFlags or STARTF_USESHOWWINDOW;
  SI.wShowWindow	:= Show;

  if Assigned(EnvironmentBlock) then CreationFlags := CreationFlags or CREATE_UNICODE_ENVIRONMENT;

  ARGU := '"' + ApplicationName + '"';
  if Parameter <> nil then ARGU := Trim(ARGU + ' ' + Parameter);

  if AsUserToken = 0
     then RESULT := CreateProcess(	nil,
                                        PChar(ARGU),
                                        nil,
                                        nil,
                                        InheritHandles,
                                        CreationFlags,
                                        EnvironmentBlock,
                                        Directory,
                                        SI,
                                        PI)
     else RESULT := CreateProcessAsUser(AsUserToken,
                                        nil,
                                        PChar(ARGU),
                                        nil,
                                        nil,
                                        InheritHandles,
                                        CreationFlags,
                                        EnvironmentBlock,
                                        Directory,
                                        SI,
                                        PI);
  if RESULT and WaitForInputIdle then Windows.WaitForInputIdle(PI.hProcess, INFINITE);
  if RESULT and WaitForTermination then begin
     if WaitingThread = 0 then begin
	WaitForSingleObject(PI.hProcess, INFINITE);
	end
     else begin
	while (WaitForSingleObject(WaitingThread, 0) = WAIT_TIMEOUT) and
	      (WaitForSingleObject(PI.hProcess, 1000) = WAIT_TIMEOUT) do Sleep(100);
	end;
     end;

  if PI.hProcess <> 0 then CloseHandle(PI.hProcess);
  if PI.hThread  <> 0 then CloseHandle(PI.hThread);
end;
function EXEC_CreateProcessAsConsoleUser(
			ApplicationName : PChar;
			Parameter : PChar = nil;
			Directory : PChar = nil;
			WaitForInputIdle : Boolean = FALSE;
			WaitForTermination : Boolean = FALSE;
			WaitingThread : THandle = 0;
                        InheritHandles: BOOL = FALSE;
                        CreationFlags: DWORD = 0;
                        Desktop: PChar = nil;
			Show : Integer = SW_SHOW) : BOOL;
var
  UserToken, LinkedToken : THandle;
  EnvironmentBlock: Pointer;
begin
  RESULT := FALSE;

  UserToken := GetConsoleToken();
  if UserToken = 0 then Exit;

  try
    LinkedToken := GetLinkedToken(UserToken);
    if LinkedToken = 0 then Exit;

    try
      if not CreateEnvironmentBlock(EnvironmentBlock, UserToken, TRUE) then Exit;

      RESULT := EXEC_CreateProcess(
			ApplicationName,
			Parameter,
			Directory,
			LinkedToken,
 			WaitForInputIdle,
			WaitForTermination,
			WaitingThread,
                        EnvironmentBlock,
                        InheritHandles,
                        CreationFlags,
                        Desktop,
			Show);
      finally
      CloseHandle(LinkedToken);
      end;

    finally
    CloseHandle(UserToken);
    end;

end;

'Libraries > Delphi Library' 카테고리의 다른 글

[System] OS Version Library  (0) 2022.06.17
[Protocol] Modbus RTU Packet Library  (0) 2022.06.17
[System] Process Token Library  (0) 2022.06.17
[System] Process Information Library  (0) 2022.06.17
[System] Execute Library  (0) 2022.06.17