Forráskód Böngészése

Minor cleanup + fix the example.

Martijn Laan 1 éve
szülő
commit
3dc589000e

+ 3 - 4
ISHelp/isxfunc.xml

@@ -2890,18 +2890,17 @@ end;</pre></example>
         <example><pre>var
   ResultCode: Integer;
   Output: TExecOutput;
-
 begin
   try
     // Get the system configuration
-    Result := ExecAndCaptureOutput(ExpandConst('{cmd}', 'systeminfo', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, Output);
+    Result := ExecAndCaptureOutput(ExpandConstant('{cmd}'), '/k systeminfo', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, Output);
   except
-    Result := False
+    Result := False;
     Log(GetExceptionMessage);
   end;
 
   if Result then begin
-    // Do something with Output.StdOut
+    // Do something with the Output.StdOut array of string
   end;
 end;</pre></example>
         <seealso><p><link topic="isxfunc_Exec">Exec</link><br />

+ 19 - 33
Projects/Src/CmnFunc2.pas

@@ -58,18 +58,16 @@ type
     FCaptureErrList: TStringList;
     FCaptureError: Boolean;
     procedure CloseAndClearHandle(var Handle: THandle);
-    procedure LogErrorFmt(const S: String; const Args: array of const);
+    procedure HandleAndLogErrorFmt(const S: String; const Args: array of const);
     procedure DoRead(var PipeRead: THandle; var Buffer: AnsiString; const LastRead: Boolean);
-    function GetCaptureOutList: TStringList;
-    function GetCaptureErrList: TStringList;
   public
     constructor Create(const ALogProc: TLogProc; const ALogProcData: NativeInt; AMode: TOutputMode = omLog);
     destructor Destroy; override;
     procedure UpdateStartupInfo(var StartupInfo: TStartupInfo);
     procedure NotifyCreateProcessDone;
     procedure Read(const LastRead: Boolean);
-    property CaptureOutList: TStringList read GetCaptureOutList;
-    property CaptureErrList: TStringList read GetCaptureErrList;
+    property CaptureOutList: TStringList read FCaptureOutList;
+    property CaptureErrList: TStringList read FCaptureErrList;
     property CaptureError: Boolean read FCaptureError;
   end;
 
@@ -1691,7 +1689,7 @@ begin
   end;
 end;
 
-procedure TCreateProcessOutputReader.LogErrorFmt(const S: String; const Args: array of const);
+procedure TCreateProcessOutputReader.HandleAndLogErrorFmt(const S: String; const Args: array of const);
 begin
   FLogProc('OutputReader: ' + Format(S, Args), True, False, FLogProcData);
 
@@ -1699,22 +1697,6 @@ begin
     FCaptureError := True;
 end;
 
-function TCreateProcessOutputReader.GetCaptureOutList: TStringList;
-begin
-  if not Assigned(FCaptureOutList) then
-    raise Exception.Create('FCaptureOutList not set');
-
-  Result := FCaptureOutList;
-end;
-
-function TCreateProcessOutputReader.GetCaptureErrList: TStringList;
-begin
-  if not Assigned(FCaptureErrList) then
-    raise Exception.Create('FCaptureErrList not set');
-
-  Result := FCaptureErrList;
-end;
-
 procedure TCreateProcessOutputReader.UpdateStartupInfo(var StartupInfo: TStartupInfo);
 begin
   StartupInfo.dwFlags := StartupInfo.dwFlags or STARTF_USESTDHANDLES;
@@ -1757,15 +1739,16 @@ procedure TCreateProcessOutputReader.DoRead(var PipeRead: THandle;
     Result := 0;
   end;
 
-  procedure LogLine(const S: AnsiString);
+  procedure LogLine(const FromPipe: THandle; const S: AnsiString);
   begin
+    var UTF8S := UTF8ToString(S);
     if FMode = omLog then begin
-      FLogProc(UTF8ToString(S), False, FNextLineIsFirstLine, FLogProcData);
+      FLogProc(UTF8S, False, FNextLineIsFirstLine, FLogProcData);
       FNextLineIsFirstLine := False;
-    end else if PipeRead = FStdOutPipeRead then
-      FCaptureOutList.Add(UTF8ToString(S))
+    end else if FromPipe = FStdOutPipeRead then
+      FCaptureOutList.Add(UTF8S)
     else
-      FCaptureErrList.Add(UTF8ToString(S));
+      FCaptureErrList.Add(UTF8S);
   end;
 
 begin
@@ -1775,7 +1758,7 @@ begin
     if not FOKToRead then begin
       var LastError := GetLastError;
       if LastError <> ERROR_BROKEN_PIPE then
-        LogErrorFmt('PeekNamedPipe failed (%d).', [LastError]);
+        HandleAndLogErrorFmt('PeekNamedPipe failed (%d).', [LastError]);
     end else if TotalBytesAvail > 0 then begin
       { Don't read more than our read limit }
       if TotalBytesAvail > FMaxTotalBytesToRead - FTotalBytesRead then
@@ -1787,7 +1770,7 @@ begin
       FOKToRead := ReadFile(PipeRead, Buffer[TotalBytesHave+1],
         TotalBytesAvail, BytesRead, nil);
       if not FOKToRead then begin
-        LogErrorFmt('ReadFile failed (%d).', [GetLastError]);
+        HandleAndLogErrorFmt('ReadFile failed (%d).', [GetLastError]);
         { Restore back to original size }
         SetLength(Buffer, TotalBytesHave);
       end else begin
@@ -1799,7 +1782,7 @@ begin
           var P := FindNewLine(Buffer, LastRead);
           if P = 0 then
             Break;
-          LogLine(Copy(Buffer, 1, P-1));
+          LogLine(PipeRead, Copy(Buffer, 1, P-1));
           Inc(FTotalLinesRead);
           if (Buffer[P] = #13) and (P < Length(Buffer)) and (Buffer[P+1] = #10) then
             Inc(P);
@@ -1814,14 +1797,17 @@ begin
           if FMode = omLog then
             Buffer := ''
           else begin
+            { Bit of a hack: the Buffer parameter points to either FReadOutBuffer or FReadErrBuffer.
+              We want both cleared and must do this now because won't get here again. So just access
+              both directly. }
             FReadOutBuffer := '';
             FReadErrBuffer := '';
           end;
 
           if FTotalBytesRead >= FMaxTotalBytesToRead then
-            LogErrorFmt('Maximum output length (%d) reached, ignoring remainder.', [FMaxTotalBytesToRead])
+            HandleAndLogErrorFmt('Maximum output length (%d) reached, ignoring remainder.', [FMaxTotalBytesToRead])
           else
-            LogErrorFmt('Maximum output lines (%d) reached, ignoring remainder.', [FMaxTotalLinesToRead]);
+            HandleAndLogErrorFmt('Maximum output lines (%d) reached, ignoring remainder.', [FMaxTotalLinesToRead]);
         end;
       end;
     end;
@@ -1838,7 +1824,7 @@ begin
   end;
 
   if LastRead and (Buffer <> '') then
-    LogLine(Buffer);
+    LogLine(PipeRead, Buffer);
 end;
 
 end.

+ 3 - 3
Projects/Src/Compile.pas

@@ -7379,8 +7379,6 @@ procedure TSetupCompiler.SignCommand(const AName, ACommand, AParams, AExeFilenam
     LastSignCommandStartTick := GetTickCount;
 
     var StartupInfo: TStartupInfo;
-    var ProcessInfo: TProcessInformation;
-    var LastError, ExitCode: DWORD;
     FillChar(StartupInfo, SizeOf(StartupInfo), 0);
     StartupInfo.cb := SizeOf(StartupInfo);
     StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
@@ -7392,9 +7390,10 @@ procedure TSetupCompiler.SignCommand(const AName, ACommand, AParams, AExeFilenam
       var dwCreationFlags: DWORD := CREATE_DEFAULT_ERROR_MODE or CREATE_NO_WINDOW;
       OutputReader.UpdateStartupInfo(StartupInfo);
 
+      var ProcessInfo: TProcessInformation;
       if not CreateProcess(nil, PChar(AFormattedCommand), nil, nil, InheritHandles,
          dwCreationFlags, nil, PChar(CompilerDir), StartupInfo, ProcessInfo) then begin
-        LastError := GetLastError;
+        var LastError := GetLastError;
         AbortCompileFmt(SCompilerSignToolCreateProcessFailed, [LastError,
           Win32ErrorString(LastError)]);
       end;
@@ -7417,6 +7416,7 @@ procedure TSetupCompiler.SignCommand(const AName, ACommand, AParams, AExeFilenam
           end;
         end;
         OutputReader.Read(True);
+        var ExitCode: DWORD;
         if not GetExitCodeProcess(ProcessInfo.hProcess, ExitCode) then
           AbortCompile('Sign: GetExitCodeProcess failed');
         if ExitCode <> 0 then

+ 2 - 3
Projects/Src/ScriptFunc_R.pas

@@ -823,19 +823,18 @@ end;
 
 procedure ExecAndCaptureFinalize(StackData: Pointer; const OutputReader: TCreateProcessOutputReader);
 begin
-  var I: Integer;
   { StdOut - 0 }
   var Item := NewTPSVariantRecordIFC(StackData, 0);
   var List := OutputReader.CaptureOutList;
   PSDynArraySetLength(Pointer(Item.Dta^), Item.aType, List.Count);
-  for I := 0 to List.Count - 1 do
+  for var I := 0 to List.Count - 1 do
     VNSetString(PSGetArrayField(Item, I), List[I]);
 
   { StdErr - 1 }
   Item := NewTPSVariantRecordIFC(StackData, 1);
   List := OutputReader.CaptureErrList;
   PSDynArraySetLength(Pointer(Item.Dta^), Item.aType, List.Count);
-  for I := 0 to List.Count - 1 do
+  for var I := 0 to List.Count - 1 do
     VNSetString(PSGetArrayField(Item, I), List[I]);
 
   { Error - 2 }