Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure TFuncoesDiversas.ExecutaComandoDOS(Comando: string; Stb: TStringBuilder);
- const
- READ_BUFFER_SIZE = 2400;
- var
- Security : TSecurityAttributes;
- readableEndOfPipe,
- writeableEndOfPipe : THandle;
- start : TStartUpInfo;
- ProcessInfo : TProcessInformation;
- Buffer : PAnsiChar;
- BytesRead : DWORD;
- AppRunning : DWORD;
- begin
- try
- Security.nLength := SizeOf(TSecurityAttributes);
- Security.bInheritHandle := True;
- Security.lpSecurityDescriptor := nil;
- if CreatePipe(readableEndOfPipe, writeableEndOfPipe, @Security, 0) then
- begin
- Buffer := AllocMem(READ_BUFFER_SIZE + 1);
- FillChar(Start, Sizeof(Start), #0);
- start.cb := SizeOf(start);
- start.dwFlags := start.dwFlags or STARTF_USESTDHANDLES;
- start.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
- start.hStdOutput := writeableEndOfPipe;
- start.hStdError := writeableEndOfPipe;
- start.dwFlags := start.dwFlags + STARTF_USESHOWWINDOW;
- start.wShowWindow := SW_HIDE;
- ProcessInfo := Default(TProcessInformation);
- UniqueString(Comando);
- if CreateProcess(nil, PChar(Comando), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, start, ProcessInfo) then
- begin
- repeat
- Apprunning := WaitForSingleObject(ProcessInfo.hProcess, 100);
- Application.ProcessMessages;
- until (Apprunning <> WAIT_TIMEOUT);
- repeat
- BytesRead := 0;
- ReadFile(readableEndOfPipe, Buffer[0], READ_BUFFER_SIZE, BytesRead, nil);
- Buffer[BytesRead] := #0;
- OemToAnsi(Buffer, Buffer);
- Stb.AppendLine(String(Buffer));
- until (BytesRead < READ_BUFFER_SIZE);
- end;
- FreeMem(Buffer);
- CloseHandle(ProcessInfo.hProcess);
- CloseHandle(ProcessInfo.hThread);
- CloseHandle(readableEndOfPipe);
- CloseHandle(writeableEndOfPipe);
- end;
- except
- on E: Exception do
- begin
- Application.MessageBox(PChar('Ocorreu um erro ao executar a função "TFuncoesDiversas.ExecutaComandoDOS".' +
- #13 + #13 + 'Erro: ' + E.Message), PChar(Application.Title), MB_OK + MB_ICONSTOP);
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement