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
|
(*:
DsiWin32.pas
Collection of Win32 wrappers and helper functions.
Free for personal and commercial use. No rights reserved.
Maintainer : gabr
Contributors : ales, aoven, gabr, Lee_Nover, _MeSSiah_, Miha-R, Odisej, xtreme,
Brdaws, Gre-Gor
Creation date : 2002-10-09
Last modification : 2005-01-27
Version : 1.12a
*)
procedure DSiCloseHandleAndNull(var handle: THandle);
begin
if handle <> 0 then begin
CloseHandle(handle);
handle := 0;
end;
end; { DSiCloseHandleAndNull }
function DSiExecuteAndCapture(const app: string; output: TStrings; const workDir: string): LongInt;
const
StartBufSize = 1024;
var
bAvail : longword;
bRead : longword;
buffer : pointer;
exitCode : longword;
iBufSize : cardinal;
last : string;
newStdIn : THandle;
newStdOut : THandle;
pBuf : PChar;
PI : TProcessInformation;
pSentinel : PChar;
read_StdOut: THandle;
SA : TSecurityAttributes;
SD : TSecurityDescriptor;
SI : STARTUPINFO;
str : string;
useWorkDir : string;
write_StdIn: THandle;
begin
Result := -1;
buffer := nil;
write_StdIn := 0;
read_StdOut := 0;
newStdOut := 0;
newStdIn := 0;
ZeroMemory(@PI, SizeOf(PI));
ZeroMemory(@SA, SizeOf(SA));
try
{ Za Windows NT inicializiraj security descriptor. }
// if DSiIsWinNT then begin
if (Win32Platform = VER_PLATFORM_WIN32_NT) then begin
InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SD, true, nil, false);
SA.lpSecurityDescriptor := @SD;
end
else
SA.lpSecurityDescriptor := nil;
SA.nLength := SizeOf(TSecurityAttributes);
SA.bInheritHandle := true; // dovoli dedovanje handlov
{ Ustvari nov stdin pipe. }
if not CreatePipe(newStdIn, write_StdIn, @SA, 0) then
Exit;
{ Ustvari nov stout pipe. }
if not CreatePipe(read_StdOut, newStdOut, @SA, 0) then
Exit;
{ Nastavi parametre novega procesa. }
ZeroMemory(@SI, SizeOf(TStartupInfo));
SI.cb := SizeOf(TStartupInfo);
SI.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
SI.wShowWindow := SW_HIDE;
SI.hStdOutput := newStdOut;
SI.hStdError := newStdOut;
SI.hStdInput := newStdIn;
if workDir = '' then
GetDir(0, useWorkDir)
else
useWorkDir := workDir;
{ Startaj proces. }
if not CreateProcess(nil, PChar(app), nil, nil, true,
NORMAL_PRIORITY_CLASS, nil, PChar(useWorkDir), SI, PI)
then
Exit;
//Result := PI.hProcess;
last := ''; // buffer za shranjevanje nedokoncanih vrstic (brez zakljucnega CRLF)
iBufSize := StartBufSize;
buffer := AllocMem(iBufSize);
try
GetExitCodeProcess(PI.hProcess, exitCode);
while exitCode = STILL_ACTIVE do begin
{ Poglej, ce je na stdout pipi kaj podatkov. }
PeekNamedPipe(read_StdOut, buffer, iBufSize, @bRead, @bAvail, nil);
if (bRead > 0) then begin
if (iBufSize < bAvail) then begin // ce je buffer premajhen, ga povecaj
iBufSize := bAvail;
ReallocMem(buffer, iBufSize);
end;
ZeroMemory(buffer, iBufSize); // pocisti buffer
ReadFile(read_StdOut, buffer^, iBufSize, bRead, nil); // preberi stdout pipe
str := last; // shrani prejsnjo nedokoncano vrstico
pBuf := buffer;
pSentinel := pBuf;
Inc(pSentinel, bRead);
while (pBuf < pSentinel) do begin
case pBuf^ of
#0: Inc(pBuf);
#10:
begin // konec vrstice
Inc(pBuf);
Output.Add(Str);
str := '';
end; //#10
#13:
begin
Inc(pBuf);
if (pBuf < pSentinel) and (pBuf^ = #10) then
Inc(pBuf); // preskoci, ker je to konec vrstice
Output.Add(Str);
str := '';
end; //#13
else begin
str := str + pBuf^; // shrani prebrani znak
Inc(pBuf);
end; //else
end; //case
end;
last := str; // shrani morebitno nedokon?ano vrstico
end;
Sleep(1); // pavza
GetExitCodeProcess(PI.hProcess, exitCode);
end;
finally FreeMem(buffer, iBufSize); end;
result := exitCode ;
if (Last <> '') then
Output.Add(Last); // poslji zadnjo vrstico tudi, ce ni bila dokoncana
finally
{ Pocisti za sabo. }
DSiCloseHandleAndNull(PI.hThread);
DSiCloseHandleAndNull(PI.hProcess);
DSiCloseHandleAndNull(NewStdIn);
DSiCloseHandleAndNull(NewStdOut);
DSiCloseHandleAndNull(Read_StdOut);
DSiCloseHandleAndNull(Write_StdIn);
end;
end; { DSiExecuteAndCapture } |
Partager