形式
Delphi
投稿日時
2019-06-25 03:28
公開期間
無期限
  1. // Função principal
  2. function ExecuteAndCaptureOutput(const AStdOutHandle: THandle; const ACommand, AParameters: String; out AExitCode: Cardinal): Boolean; stdcall;
  3. const
  4. READBUFFERSIZE = High(BYTE);
  5. var
  6. SCAT: TSecurityAttributes;
  7. ReadPipeHandle: THandle;
  8. WritePipeHandle: THandle;
  9. SRIN: TStartupInfo;
  10. PRIN: TProcessInformation;
  11. OEMBuffer: array [0 .. READBUFFERSIZE] of AnsiChar;
  12. CharBuffer: array [0 .. READBUFFERSIZE] of AnsiChar;
  13. BytesRead: DWORD;
  14. CommandRunning: DWORD;
  15. BytesToRead: DWORD;
  16. begin
  17. Result := False;
  18. SCAT.nLength := SizeOf(TSecurityAttributes);
  19. SCAT.bInheritHandle := true;
  20. SCAT.lpSecurityDescriptor := nil;
  21. if CreatePipe(ReadPipeHandle, WritePipeHandle, @SCAT, 0) then
  22. try
  23. ZeroMemory(@SRIN,SizeOf(TStartupInfo));
  24. SRIN.cb := SizeOf(TStartupInfo);
  25. SRIN.hStdInput := ReadPipeHandle;
  26. SRIN.hStdOutput := WritePipeHandle;
  27. SRIN.hStdError := WritePipeHandle;
  28. SRIN.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
  29. SRIN.wShowWindow := SW_HIDE;
  30. if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @SCAT, @SCAT, True, NORMAL_PRIORITY_CLASS, nil, nil, SRIN, PRIN) then
  31. try
  32. repeat
  33. CommandRunning := WaitForSingleObject(PRIN.hProcess, 100);
  34. PeekNamedPipe(ReadPipeHandle, nil, 0, nil, @BytesToRead, nil);
  35. if (BytesToRead > 0) then
  36. repeat
  37. BytesRead := 0;
  38. ReadFile(ReadPipeHandle, OEMBuffer[0], READBUFFERSIZE, BytesRead, nil);
  39. OEMBuffer[BytesRead] := #0;
  40. OemToCharA(OEMBuffer, CharBuffer);
  41. AddLine(AStdOutHandle,String(CharBuffer));
  42. until (BytesRead < READBUFFERSIZE);
  43. Application.ProcessMessages;
  44. until (CommandRunning <> WAIT_TIMEOUT);
  45. GetExitCodeProcess(PRIN.hProcess,AExitCode);
  46. Result := True;
  47. finally
  48. CloseHandle(PRIN.hProcess);
  49. CloseHandle(PRIN.hThread);
  50. end
  51. else
  52. AExitCode := GetLastError;
  53. finally
  54. CloseHandle(ReadPipeHandle);
  55. CloseHandle(WritePipeHandle);
  56. end;
  57. end;
  58. // Procedimento secundário
  59. procedure AddLine(AHandle: THandle; ALine: String);
  60. begin
  61. SendMessage(AHandle, EM_SETSEL, 0, -1);
  62. SendMessage(AHandle, EM_SETSEL, -1, -1);
  63. SendMessage(AHandle, EM_REPLACESEL, 0, LPARAM(PChar(ALine)));
  64. end;
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト