Advertisement
Guest User

Untitled

a guest
Aug 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.26 KB | None | 0 0
  1. procedure TFuncoesDiversas.ExecutaComandoDOS(Comando: string; Stb: TStringBuilder);
  2. const
  3.   READ_BUFFER_SIZE = 2400;
  4. var
  5.   Security           : TSecurityAttributes;
  6.   readableEndOfPipe,
  7.   writeableEndOfPipe : THandle;
  8.   start              : TStartUpInfo;
  9.   ProcessInfo        : TProcessInformation;
  10.   Buffer             : PAnsiChar;
  11.   BytesRead          : DWORD;
  12.   AppRunning         : DWORD;
  13. begin
  14.   try
  15.     Security.nLength              := SizeOf(TSecurityAttributes);
  16.     Security.bInheritHandle       := True;
  17.     Security.lpSecurityDescriptor := nil;
  18.  
  19.     if CreatePipe(readableEndOfPipe, writeableEndOfPipe, @Security, 0) then
  20.     begin
  21.       Buffer := AllocMem(READ_BUFFER_SIZE + 1);
  22.  
  23.       FillChar(Start, Sizeof(Start), #0);
  24.  
  25.       start.cb          := SizeOf(start);
  26.       start.dwFlags     := start.dwFlags or STARTF_USESTDHANDLES;
  27.       start.hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
  28.       start.hStdOutput  := writeableEndOfPipe;
  29.       start.hStdError   := writeableEndOfPipe;
  30.       start.dwFlags     := start.dwFlags + STARTF_USESHOWWINDOW;
  31.       start.wShowWindow := SW_HIDE;
  32.       ProcessInfo       := Default(TProcessInformation);
  33.  
  34.       UniqueString(Comando);
  35.  
  36.       if CreateProcess(nil, PChar(Comando), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, start, ProcessInfo) then
  37.       begin
  38.         repeat
  39.           Apprunning := WaitForSingleObject(ProcessInfo.hProcess, 100);
  40.  
  41.           Application.ProcessMessages;
  42.         until (Apprunning <> WAIT_TIMEOUT);
  43.  
  44.         repeat
  45.           BytesRead := 0;
  46.           ReadFile(readableEndOfPipe, Buffer[0], READ_BUFFER_SIZE, BytesRead, nil);
  47.           Buffer[BytesRead] := #0;
  48.  
  49.           OemToAnsi(Buffer, Buffer);
  50.  
  51.           Stb.AppendLine(String(Buffer));
  52.         until (BytesRead < READ_BUFFER_SIZE);
  53.       end;
  54.  
  55.       FreeMem(Buffer);
  56.       CloseHandle(ProcessInfo.hProcess);
  57.       CloseHandle(ProcessInfo.hThread);
  58.       CloseHandle(readableEndOfPipe);
  59.       CloseHandle(writeableEndOfPipe);
  60.     end;
  61.   except
  62.     on E: Exception do
  63.     begin
  64.       Application.MessageBox(PChar('Ocorreu um erro ao executar a função "TFuncoesDiversas.ExecutaComandoDOS".' +
  65.       #13 + #13 + 'Erro: ' + E.Message), PChar(Application.Title), MB_OK + MB_ICONSTOP);
  66.     end;
  67.   end;
  68. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement