主程序中如何调入外部程序后等待程序执行完成后再向下执行。(100分)

  • 主题发起人 主题发起人 ZhuXiaoTong
  • 开始时间 开始时间
Z

ZhuXiaoTong

Unregistered / Unconfirmed
GUEST, unregistred user!
;如我想在程序中调一个批处理进行文件拷贝,拷贝过程中命令提示符窗口模式
显示,主程序等待且不能被激活,如何实现,望提供例子代码。
 
不如干脆把拷贝做在程序里得了
 
做在程序里吧,输入文件,拷贝,查查,或者看我的一个问题,这方面好多的
 
see WaitForInputIdel & CreateProcess

The WaitForInputIdle function enables a thread to suspend its execution until
a specified process has finished its initialization and is waiting for user
input with no input pending. This can be useful for synchronizing a parent
process and a newly created child process. When a parent process creates
a child process, the CreateProcess function returns without waiting for the
child process to finish its initialization. Before trying to communicate with
the child process, the parent process can use WaitForInputIdle to determine
when the child's initialization has been completed. For example, the parent
process should use WaitForInputIdle before trying to find a window associated
with the child process.
 
这类题目正好我以前做过一个,用于封装UPX的功能。
便于每次发布程序时压缩可执行文件。
主要使用CreateProcess函数

例子如下:
procedure TForm1.Button1Click(Sender: TObject);
var
sCommandLine: string;
lpStartupInfo: TStartupInfo;
my_priority:DWORD;
lpProcessInformation: TProcessInformation;
my_file:TFileStream;
begin
if OpenDialog1.Execute then
begin
//winexec(pchar('upx -9 -k '+ExtractFileName(OpenDialog1.FileName)),SW_MAXIMIZE);
if not FileExists(ExtractFilePath(ParamStr(0)) + 'Upx.exe') then
begin
with TResourceStream.Create(hInstance, 'UPX_1', 'UPX') do
try
SaveToFile(ExtractFilePath(ParamStr(0)) + 'Upx.exe');
finally
Free;
end;
end;
form1.Visible:=false;
//my_out:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'temp.txt',fmCreate);
sCommandLine:='upx -9 -k '+ExtractFileName(OpenDialog1.FileName);
FillChar(lpStartUpInfo,sizeof(StartUpInfo),#0);
//lpStartupInfo.cb:=sizeof(my_out);
//lpStartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
//lpStartupInfo.wShowWindow:=SW_HIDE;
//lpStartupInfo.hStdOutput:=my_out.Handle;
if CheckBox1.Checked then
begin
my_priority:=HIGH_PRIORITY_CLASS;
my_file:=TFileStream.Create('temp.txt',fmCreate);
//SetStdHandle(STD_OUTPUT_HANDLE,my_file.Handle);
lpStartupInfo.cb:=sizeof(lpStartUpInfo);
lpStartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
lpStartupInfo.wShowWindow:=SW_HIDE;
lpStartupInfo.hStdOutput:=my_file.Handle;
end
else
my_priority:=0;

if CreateProcess(nil,PChar(sCommandLine),nil,nil,True,my_priority,nil,nil,lpStartUpInfo,lpProcessInformation) then
begin
WaitForSingleObject(lpProcessInformation.hProcess,INFINITE);
deletefile(ExtractFilePath(ParamStr(0)) + 'Upx.exe');
if CheckBox1.Checked then
begin
//借用sCommandLine
my_file.Free;
{SetLength(sCommandLine,64);
GetWindowsDirectory(pchar(sCommandLine),64);
strcat(pchar(sCommandLine),pchar('/Notepad.exe '+'temp.txt'));
WinExec(pchar(sCommandLine),SW_NORMAL);}
Application.CreateForm(TForm2, Form2);
form2.Memo1.Lines.LoadFromFile('temp.txt');
form2.ShowModal;
deletefile('temp.txt');
end;
form1.Visible:=true;
end;

end;
end;
 
调用单线程的程序没问题,多线程的就不行了!
 
后退
顶部