当我从命令行运行时:
7z a 1.zip J:\test.mp4
我可以看到完成了多少%。 当我尝试使用CreateProcess和CreatePipe从Delphi运行它时,在文件打包之前我什么都没得到。然后它显示了7zip的最终输出。
我的代码如下所示:
Stream:= THandleStream.Create(hRead);
try
if not CreateProcess(nil, PChar(Cmd), nil, nil,
True, 0, nil, nil, StartupInfo,
ProcessInformation) then
RaiseLastOSError;
repeat
if not GetExitCodeProcess(ProcessInformation.hProcess, ExitCode) then
RaiseLastOSError;
while Stream.Position < Stream.Size do
begin
Stream.Read(C, 1);
if (C = #13) then
begin
Memo1.Lines.Add(S);
S := '';
Application.ProcessMessages;
end
else if C <> #10 then
begin
S := S+C;
end;
end;
until ExitCode <> Still_Active;
finally
Stream.Free;
end;
我不想只创建一个ZIP存档 - 我知道在Delphi中有更好的方法可以做到这一点。我想与控制台应用程序进行交互。许多控制台应用程序的输出可以使用我发布的代码进行处理,但是7zip失败了 - 这就是我在这里询问7zip的原因。 7zip有什么特别之处,它的输出无法正确捕获?如何从像7zip这样的应用程序中捕获输出?
答案 0 :(得分:4)
您可以查看progdigy
制作的插件进度条
function ProgressCallback(sender: Pointer; total: boolean; value: int64): HRESULT; stdcall;
begin
if total then
Mainform.ProgressBar.Max := value else
Mainform.ProgressBar.Position := value;
Result := S_OK;
end;
procedure TMainForm.ExtractClick(Sender: TObject);
begin
with CreateInArchive(CLSID_CFormatZip) do
begin
OpenFile('c:\test.zip');
SetProgressCallback(nil, ProgressCallback);
...
end;
end;
答案 1 :(得分:-1)
DWORD static CreateProcessWithComLine_Wait(const DWORD dwWait, const PWCHAR szFolderPath, const PWCHAR szFilePath, const PWCHAR szComLine)
{
DWORD result = 0;
// ----
STARTUPINFO si;
ZeroMemory( & si, sizeof(si));
// ----
PROCESS_INFORMATION pi;
ZeroMemory( & pi, sizeof(pi));
// ----
si.cb = sizeof(si);
si.wShowWindow = SW_HIDE;
// ----
DWORD dwErrorCode = 0;
// ----
int iRes = CreateProcess(szFilePath, szComLine, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS, NULL, szFolderPath, &si, &pi);
if ( iRes != 0 )
{
WaitForSingleObject(pi.hProcess, dwWait);
// ----
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
dwErrorCode = GetLastError();
result = dwErrorCode;
}
// ----
return result;
}