Fórum Como eu uso o gbak com WinExec ou ShellExecute no delphi? #369123
17/03/2009
0
Junnsouzza
Curtir tópico
+ 0Posts
17/03/2009
Woinch
WinExec(PChar(´aplicativo.exe -parametros´, SW_SHOWNORMAL));
Já para o ShellExecute:
ShellExecute(Handle, ´open´, PChar(´Aplicacao.exe´), PChar(´-parametros´), ´´, SW_SHOWNORMAL);
*Lembrando que a constante SW_SHOWNORMAL pode ser trocada por outras, por exemplo SW_HIDE que irá executar o programa de forma oculta.
Espero ter ajudado.
Gostei + 0
18/03/2009
Webjoel
Basta você passar um comando para o seu cmd, como você faria na mão, segue um exemplo de uma cópia de arquivo:
WinExec(PChar(´cmd.exe /c copy C:\etiquetas.txt c:\joel\etiquetas.txt´),SW_SHOWNORMAL);
Gostei + 0
18/03/2009
Junnsouzza
Gostei + 0
18/03/2009
Woinch
Gostei + 0
18/03/2009
Junnsouzza
Será que isso funciona??
Gostei + 0
18/03/2009
Woinch
ExtractFilePath(Application.FileName);
Ela irá retornar a pasta onde está a aplicação.
Gostei + 0
18/03/2009
Junnsouzza
Gostei + 0
18/03/2009
Junnsouzza
Quando eu usava o IBBackupService e IBRestoreService (Usava, não uso mais), eu tinha as seguintes linhas de código dentro do
with IBBackupService do:
Verbose := true; ServiceStart; While not Eof do mmBackup.Lines.Add(GetNextLine); // mmBackup é um memo
Isso fazia com que os detalhes do serviço de backup fossem sendo impressos nas linhas do Memo. Esses mesmos detalhes aparecem no console quando eu executo o gbak. Pois bem, tem como fazer essas linhas serem impressas no Memo novamente, já que eu não uso mais os IBServices??
Gostei + 0
18/03/2009
Seven
GetEnvironmentVariable(´PROGRAMFILES´) pega a pasta arquivos de programas (independente de onde estiver.
execexterno(GetEnvironmentVariable(´PROGRAMFILES´)+´\firebird\firebird_2_0\bin\isql.exe´,´ -extract -quiet -output ´+GetEnvironmentVariable(´TEMP´)+´\bancotemp.tmp ´+endbancoant+´ -user sysdba -password masterkey´,SW_HIDE);
até
function ExecExterno(const FileName, Params: string; const WindowState: Word): boolean;
var
SUInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: string;
begin
{ Coloca o nome do arquivo entre aspas. Isto é necessário devido aos espaços contidos em nomes longos }
CmdLine := ´´´ + Filename+´´ ´+ Params ;
FillChar(SUInfo, SizeOf(SUInfo), #0);
with SUInfo do
begin
cb := SizeOf(SUInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WindowState;
end;
Result := CreateProcess(nil, PChar(CmdLine), nil, nil, false,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
PChar(ExtractFilePath(Filename)), SUInfo, ProcInfo);
{ Aguarda até ser finalizado }
if Result then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
{ Libera os Handles }
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
Gostei + 0
18/03/2009
Woinch
Desculpe, escrevi errado. O correto é:
ExtractFilePath(Application.ExeName)
Gostei + 0
19/03/2009
Junnsouzza
Gostei + 0
19/03/2009
Igorcb
pelo shellexecute
Gostei + 0
19/03/2009
Junnsouzza
eu chamo a função desse jeito : CaptureConsoleOutput(´C:\Arquivos de programas\Firebird\Firebird_1_5\bin\gbak.exe´ + ´ -b -v -user SYSDBA -pas masterkey ´ + ArqBackupOrigem + ´ ´ + ArqBackupDestino, mmBackup);
procedure TForm1.CaptureConsoleOutput(DosApp : string;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 // Increase counter to prevent an endless loop if the process is dead Inc(n,1); // wait for end of child process Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100); Application.ProcessMessages; // it is important to read from time to time the output information // so that the pipe is not blocked by an overflow. New information // can be written from the console app to the pipe only if there is // enough buffer space. 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); 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;
Gostei + 0
13/09/2016
Eduardo Silva
eu chamo a função desse jeito : CaptureConsoleOutput(´C:\\Arquivos de programas\\Firebird\\Firebird_1_5\\bin\\gbak.exe´ + ´ -b -v -user SYSDBA -pas masterkey ´ + ArqBackupOrigem + ´ ´ + ArqBackupDestino, mmBackup);
[code:1:0e9c62d07e]
procedure TForm1.CaptureConsoleOutput(DosApp : string;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
// Increase counter to prevent an endless loop if the process is dead
Inc(n,1);
// wait for end of child process
Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
Application.ProcessMessages;
// it is important to read from time to time the output information
// so that the pipe is not blocked by an overflow. New information
// can be written from the console app to the pipe only if there is
// enough buffer space.
if not PeekNamedPipe(ReadPipe ,@Buffer[TotalBytesRead&93;,
ReadBuffer ,@BytesRead,
@TotalBytesAvail,@BytesLeftThisMessage) then break
else if BytesRead > 0 then
ReadFile(ReadPipe,Buffer[TotalBytesRead&93;,BytesRead,BytesRead,nil);
TotalBytesRead:=TotalBytesRead+BytesRead;
until (Apprunning <> WAIT_TIMEOUT) or (n > 150);
Buffer&91;TotalBytesRead&93;:= 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;[/code:1:0e9c62d07e]
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)