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
|
program CreateProcess2;
{$IFDEF VPASCAL}
{&PMTYPE VIO}
{&USE32+}
{$ELSE}
{$APPTYPE CONSOLE}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{$ENDIF}
uses
Windows, Messages, SysUtils;
// http://laurent-dardenne.developpez.com/Sources/RedirectionConsole.zip
function CreateDOSProcessRedirected(const aCL, aIF, aOF: string): boolean;
// CL=CommandLine, IF=InputFile, OF=OutputFile
const
a = GENERIC_READ or GENERIC_WRITE;
b = FILE_SHARE_READ or FILE_SHARE_WRITE;
c = OPEN_ALWAYS;
d = CREATE_ALWAYS;
e = FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH;
f = INVALID_HANDLE_VALUE;
g = STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
h = HIGH_PRIORITY_CLASS;
i = SW_HIDE;
j = INFINITE;
var
SI : TStartupInfo;
PI : TProcessInformation;
SA : TSecurityAttributes;
hAP, hAT, hIF, hOF : THandle; // AP=ApplicationProcess, AT=ApplicationThread
pCL, pIF, pOF : array[0..MAX_PATH] of char;
begin
if not FileExists(aIF) then
begin
Result := False;
Exit;
end else
Result := True;
////////////////////////////////////////////////////////////////////////////////
StrPCopy(pCL, aCL);
StrPCopy(pIF, aIF);
StrPCopy(pOF, aOF);
////////////////////////////////////////////////////////////////////////////////
FillChar(SA, SizeOf(SA), #0);
with SA do begin
nLength := SizeOf(SA);
lpSecurityDescriptor := nil;
bInheritHandle := True;
end;
////////////////////////////////////////////////////////////////////////////////
hIF := CreateFile(pIF, a, b, @SA, c, e, 0);
hOF := CreateFile(pOF, a, b, @SA, d, e, 0);
////////////////////////////////////////////////////////////////////////////////
Result := Result and (hIF <> f) and (hOF <> f);
////////////////////////////////////////////////////////////////////////////////
FillChar(SI, SizeOf(SI), #0);
with SI do begin
cb := SizeOf(SI);
dwFlags := g;
wShowWindow := i;
hStdOutput := hOF;
hStdInput := hIF;
end;
////////////////////////////////////////////////////////////////////////////////
Result := Result and
CreateProcess(nil, pCL, nil, nil, True, h, nil, nil, SI, PI);
////////////////////////////////////////////////////////////////////////////////
if Result then
begin
WaitforSingleObject(PI.hProcess, j);
hAP := PI.hProcess;
hAT := PI.hThread;
end;
////////////////////////////////////////////////////////////////////////////////
if hOF <> f then CloseHandle(hOF);
if hIF <> f then CloseHandle(hIF);
if hAT <> f then CloseHandle(hAT);
if hAP <> f then CloseHandle(hAP);
end;
const
s = 'Fonction termin'#130'e avec succ'#138's.';
begin
{ Le fichier minuscule.txt sera traité par l'application Upper et le résultat
enregistré dans majuscule.txt. }
if CreateDOSProcessRedirected('Upper ','minuscule.txt', 'majuscule.txt') then
WriteLn(s);
{ Le fichier ancien.bas sera traité par l'application FBeauty et le résultat
enregistré dans nouveau.bas. }
if CreateDOSProcessRedirected('FBeauty ','ancien.bas', 'nouveau.bas') then
WriteLn(s);
WriteLn('Appuyez sur la touche Entr'#130'e');
ReadLn;
end. |
Partager