Jump to content
Fórum Script Brasil
  • 0

Pegar Retorno de Comando DOS!


Gustavo_Sid

Question

Olá galera.

Estou fazendo um sistema de backup do firebird pelo Gbak, e gostaria de pegar o retorno do Gbak. Pois pelo WinExec ou ShellExecute não consigo pegar o retorno. Tenteu também pelo CreateProcess mais tmabém não funciona.

Usei o seguinte código:

procedure TForm1.CaptureConsoleOutput(DosApp: string; var AMemo: TMemo);

const

ReadBuffer = 1048576; // 1 MB Buffer

var

Security : TSecurityAttributes;

ReadPipe,WritePipe : THandle;

start : TStartUpInfo;

ProcessInfo : TProcessInformation;

Buffer : Pchar;

TotalBytesRead,

BytesRead : DWORD;

Apprunning,n,

BytesLeftThisMessage,

TotalBytesAvail : integer;

begin

with Security do

begin

nlength := SizeOf(TSecurityAttributes);

binherithandle := true;

lpsecuritydescriptor := nil;

end;

if CreatePipe (ReadPipe, WritePipe, @Security, 0) then

begin

// Redirect In- and Output through STARTUPINFO structure

Buffer := AllocMem(ReadBuffer + 1);

FillChar(Start,Sizeof(Start),#0);

start.cb := SizeOf(start);

start.hStdOutput := WritePipe;

start.hStdInput := ReadPipe;

start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;

start.wShowWindow := SW_HIDE;

// Create a Console Child Process with redirected input and output

if CreateProcess(nil ,PChar(DosApp),

@Security,@Security,

true ,CREATE_NO_WINDOW or NORMAL_PRIORITY_CLASS,

nil ,nil,

start ,ProcessInfo) then

begin

n:=0;

TotalBytesRead:=0;

repeat

Inc(n,1);

Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);

Application.ProcessMessages;

if not PeekNamedPipe(ReadPipe ,@Buffer[TotalBytesRead],

ReadBuffer ,@BytesRead,

@TotalBytesAvail,@BytesLeftThisMessage) then break

else if BytesRead > 0 then

ReadFile(ReadPipe,Buffer[TotalBytesRead],BytesRead,BytesRead,nil);

TotalBytesRead:=TotalBytesRead+BytesRead;

until (Apprunning <> WAIT_TIMEOUT) or (n > 150);

ShowMessage('Terminou');

Buffer[TotalBytesRead]:= #0;

OemToChar(Buffer,Buffer);

AMemo.Text := AMemo.Text + (StrPas(Buffer));

end;

FreeMem(Buffer);

CloseHandle(ProcessInfo.hProcess);

CloseHandle(ProcessInfo.hThread);

CloseHandle(ReadPipe);

CloseHandle(WritePipe);

end;

end;

O backup é feito mais não leva nada para o Memo, alguém pode me ajudar por favor ??

Obrigado.

Edited by Gustavo_Sid
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0
Estou fazendo um sistema de backup do firebird pelo Gbak, e gostaria de pegar o retorno do Gbak. Pois pelo WinExec ou ShellExecute não consigo pegar o retorno
O retorno seria assim ?

Database "C:\TESTE\Dados\Dados_med.fdb"

Database header page information:

Flags 0

Checksum 12345

Generation 9776587

Page size 4096

ODS version 10.1

Oldest transaction 9776474

Oldest active 9776475

Oldest snapshot 9776475

Next transaction 9776477

Bumped transaction 1

Sequence number 0

Next attachment ID 0

Implementation ID 16

Shadow count 0

Page buffers 0

Next header page 0

Database dialect 3

Creation date Aug 14, 2007 12:20:22

Attributes force write

Variable header data:

*END*

http://www.devmedia.com.br/post-4877-Utili...up-restore.html

pelo WinExec basta fazer isso:

procedure TForm1.Button1Click(Sender: TObject);

begin

WinExec('cmd /c gbak –user SYSDBA –pas masterkey 172.16.20.14:c:\dados.fdb c:\backup.fbk >c:\StatusBanco.txt', 0);

end;

OBS: Estou usando o exemplo do link, mas basta voce alterar para o seu uso

veja tambem que o retorno do Dos vai ficar em um arquivo ( c:\StatusBanco.txt ) que voce poderá ler pelo delphi e jogar em um Memo ( exemplo acima )

abraço

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...