求教关于象用“SHOWMODAL”调用子窗体一样来调用其他可执行文件(200分)

  • 主题发起人 主题发起人 jiabingli
  • 开始时间 开始时间
J

jiabingli

Unregistered / Unconfirmed
GUEST, unregistred user!
我现有一个用VC++开发的POS程序,现需要在DELPHI程序中调用它(pos.exe),在调用时需<br>将它放在我的程序前面(就象SHOWMODAL一个窗体一样),直到POS程序运行完才能执行我<br>的程序,现试过用ShellExecute(handle,'open','c:/pos/pos.exe','','',SW_SHOWNORMAL);<br>和WinExec()好象都不灵,不知如何写这一段程序?<br>
 
用 createprocess 和 waitforsingleobject 配合,就可以达到你的要求
 
在调用了POS程序后把自己的程序隐藏了,当POS程序运行结束后再激活自己的程序。
 
用xeen方法看看,delphi的函数调用,去打开程序,应该<br>很难实现你的要求,因为这是两个程序。彼此独立,除非<br>你的pos 有stayontop;
 
如何启动外部程序并等待它结束,你还能告诉我吗?<br><br>其实,这是一个"古老"的话题,在WIN95时代就被讨论过了。不过,既然这么多人不知道,我感觉还是有必要再讨论一下。<br><br>一、为什么要启动外部程序<br><br>也许,你想让你的程序完成全部的功能。不过,无论从物力还是人力上,你都应养成资源共享的习惯。更好的考虑是,充分利用已有的程序,而让你的程序专注于某一方面的功能。比如说,浏览器负责打开网页,让人们浏览,当遇到下载的任务时,可以交给更专业的下载软件去做。你也可能在你的程序里留下了你的主页和邮箱地址,你希望有人点击它们时就分别启动浏览器和电子邮件。在某些情况下,你需要外部程序处理后,再进行下一步的工作,这时就会遇到启动外部程序并等待它结束的问题。<br><br>二、预备知识<br><br>启动外部程序我们可以使用函数Winexec、ShellExecute和ShellExecuteEx。我推荐大家使用函数ShellExecute,因为它既灵活,又简单。看看下面的例子,用法就清楚了:<br><br>*: 启动一个程序<br><br>ShellExecute(Handle,'open',PChar('c:/test/app.exe'),<br><br>nil,nil,SW_SHOW);<br><br>* 启动记事本 (因为记事本在系统路径下,所以不必写完整的路径名了):<br><br>ShellExecute(Handle, 'open', PChar('notepad'),<br><br>nil, nil, SW_SHOW);<br><br>* 启动记事本并加载一个纯文本文件:<br><br>ShellExecute(Handle, 'open', PChar('notepad'),<br><br>PChar('c:/test/readme.txt', nil, SW_SHOW);<br><br>* 使用记事本打开一个纯文本文件 (请确定*.txt文件被关联到记事本):<br><br>ShellExecute(Handle, 'open', PChar('c:/test/readme.txt'),<br><br>nil, nil, SW_SHOW);<br><br>* 使用默认浏览器打开网址:<br><br>ShellExecute(Handle, 'open', PChar('http://www.festra.com/'),<br><br>nil, nil, SW_SHOW);<br><br>* 打印一个文件:<br><br>&nbsp; &nbsp;ShellExecute(Handle, 'print', PChar('c:/test/readme.txt'),<br><br>nil, nil, SW_SHOW);<br><br>* 用Windows Explorer打开一个文件夹:<br><br>&nbsp; ShellExecute(Handle, 'explore', PChar('c:/windows)',<br><br>nil, nil, SW_SHOW);<br><br>* 运行一个DOS命令并立即返回:<br><br>&nbsp; ShellExecute(Handle, 'open', PChar('command.com'), <br><br>PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);<br><br>* 运行一个DOS命令并保持DOS窗口打开 ("stay in DOS"):<br><br>&nbsp; ShellExecute(Handle, 'open', PChar('command.com'), <br><br>PChar('/k dir'), nil, SW_SHOW);<br><br>&nbsp;<br><br>启动一个外部程序并不难吧?不过,我们如何知道它是否运行结束了呢?我们的程序又怎样等待它结束呢?<br><br>三、启动外部程序并等待它结束的函数<br><br>我们可以通过进程句柄(process handle)来查看进程(程序)是否结束。为了得到进程句柄,有两个Win32 API函数可以利用:ShellExecuteEx 或者CreateProces。解决这个问题最简单的方法是,使用ShellExecuteEx启动一个外部程序,然后使用WaitForSingleObject管理这个外部程序的进程句柄。我们可以这样定义一个函数:<br><br>……<br><br>{ ExecAppWait:功能:运行外部程序并等待它结束。。<br><br>运行外部程序APPNAME,参数PARAMS;<br><br>&nbsp; Returns:如果外部程序出错返回 FASLE <br><br>}<br><br>function ExecAppWait(AppName, Params: string): Boolean ;<br><br>…… &nbsp; &nbsp;<br><br>function ExecAppWait(AppName, Params: string): Boolean;<br><br>var<br><br>&nbsp; // Structure containing and receiving info about application to start<br><br>&nbsp; ShellExInfo: TShellExecuteInfo;<br><br>begin<br><br>&nbsp; FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);<br><br>&nbsp; with ShellExInfo do begin<br><br>&nbsp; &nbsp; cbSize := SizeOf(ShellExInfo);<br><br>&nbsp; &nbsp; fMask := see_Mask_NoCloseProcess;<br><br>&nbsp; &nbsp; Wnd := Application.Handle;<br><br>&nbsp; &nbsp; lpFile := PChar(AppName);<br><br>&nbsp; &nbsp; lpParameters := PChar(Params);<br><br>&nbsp; &nbsp; nShow := sw_ShowNormal;<br><br>&nbsp; end;<br><br>&nbsp; Result := ShellExecuteEx(@ShellExInfo);<br><br>&nbsp; if Result then<br><br>&nbsp; &nbsp; while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do<br><br>&nbsp; &nbsp; begin<br><br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br><br>&nbsp; &nbsp; &nbsp; if Application.Terminated then Break;<br><br>&nbsp; &nbsp; end;<br><br>end;<br><br>……<br><br>不难理解吧?<br><br>建立一个Unit ExecWait,把上面的代码输进去。<br><br>四、例子<br><br>如图建立Form,源程序如下:<br><br><br><br><br>unit DemoUnit;<br><br>&nbsp;<br><br>interface<br><br>uses<br><br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls,<br><br>&nbsp; Forms, Dialogs, StdCtrls, &nbsp;SHELLAPI; <br><br>type<br><br>&nbsp; TForm1 = class(TForm)<br><br>&nbsp; &nbsp; Edit1: TEdit;<br><br>&nbsp; &nbsp; Edit2: TEdit;<br><br>&nbsp; &nbsp; Label1: TLabel;<br><br>&nbsp; &nbsp; Label2: TLabel;<br><br>&nbsp; &nbsp; BtnExec: TButton;<br><br>&nbsp; &nbsp; CheckBoxWait: TCheckBox;<br><br>&nbsp; &nbsp; Label3: TLabel;<br><br>&nbsp; &nbsp; Label4: TLabel;<br><br>&nbsp; &nbsp; Edit3: TEdit;<br><br>&nbsp; &nbsp; Edit4: TEdit;<br><br>&nbsp; &nbsp; Label5: TLabel;<br><br>&nbsp; &nbsp; procedure BtnExecClick(Sender: TObject);<br><br>&nbsp; private<br><br>&nbsp; &nbsp; { Private declarations }<br><br>&nbsp; public<br><br>&nbsp; &nbsp; { Public declarations }<br><br>&nbsp; end;<br><br>&nbsp;<br><br>var<br><br>&nbsp; Form1: TForm1;<br><br>&nbsp;<br><br>implementation<br><br>&nbsp;<br><br>uses<br><br>&nbsp; execwait;<br><br>&nbsp;<br><br>{$R *.DFM}<br><br>&nbsp;<br><br>procedure TForm1.BtnExecClick(Sender: TObject);<br><br>var<br><br>&nbsp; Success: Boolean;<br><br>&nbsp; InstanceID: THandle;<br><br>begin<br><br>&nbsp; { 最小化窗口,提醒发生的变化 }<br><br>&nbsp; Application.Minimize;<br><br>&nbsp;<br><br>&nbsp; Success := False;<br><br>&nbsp; try<br><br>&nbsp; &nbsp; if CheckBoxWait.Checked then<br><br>&nbsp; &nbsp; &nbsp; Success := ExecAppWait(Edit1.Text, Edit2.Text)<br><br>&nbsp; &nbsp; else begin<br><br>&nbsp; &nbsp; &nbsp; InstanceID := ShellExecute(Handle, 'open', PChar(Edit1.Text),<br><br>&nbsp; &nbsp; &nbsp; &nbsp; PChar(Edit2.Text), nil, SW_SHOW);<br><br>&nbsp; &nbsp; &nbsp; Success := InstanceID &gt;= 32; // 小于32可就错了<br><br>&nbsp; &nbsp; end;<br><br>&nbsp; finally<br><br>&nbsp; &nbsp; // 可别忘了恢复我们的程序的窗口!<br><br>&nbsp; &nbsp; Application.Restore;<br><br>&nbsp; &nbsp; if not Success then<br><br>&nbsp; &nbsp; &nbsp; ShowMessage('Application 1 failed: ' + Edit1.Text + ' ' + Edit2.Text);<br><br>&nbsp; end;<br><br>&nbsp; try<br><br>&nbsp; &nbsp; if CheckBoxWait.Checked then<br><br>&nbsp; &nbsp; &nbsp; Success := ExecAppWait(Edit3.Text, Edit4.Text)<br><br>&nbsp; &nbsp; else begin<br><br>&nbsp; &nbsp; &nbsp; InstanceID := ShellExecute(Handle, 'open', PChar(Edit3.Text),<br><br>&nbsp; &nbsp; &nbsp; &nbsp; PChar(Edit4.Text), nil, SW_SHOW);<br><br>&nbsp; &nbsp; &nbsp; Success := InstanceID &gt;= 32; //小于32可就错了<br><br>&nbsp; &nbsp;end;<br><br>&nbsp; finally<br><br>&nbsp; &nbsp; //恢复我们的程序的窗口<br><br>&nbsp; &nbsp; Application.Restore;<br><br>&nbsp; &nbsp; if not Success then<br><br>&nbsp; &nbsp; &nbsp; ShowMessage('Application 2 failed: ' + Edit3.Text + ' ' + Edit4.Text);<br><br>&nbsp; end;<br><br>end;<br><br>&nbsp;<br><br>end.<br><br>&nbsp;<br><br>OK,没有问题吧?你赶快试试吧,把它应用到你的程序里。#<br><br>
 
&nbsp; &nbsp;好帖!收藏!
 
主进程会挂机。如果长时间挂起会显示没有影应
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部