在DELPHI中如何调用外部exe(100分)

  • 主题发起人 主题发起人 杨俊
  • 开始时间 开始时间

杨俊

Unregistered / Unconfirmed
GUEST, unregistred user!
请问在DELPHI中如何调用外部的EXE文件?
 
早上好<br>先引用一个shellapi单元,<br>然后用ShellExecute
 
ShellExecute,CreateProcess都可以引用外部程序。
 
WinExec,ShellExecute,CreateProcess<br>最好用CreateProcess<br>
 
uses shellapi<br>ShellExecute(Application.Handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Open',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'C:/Windows/Rundll32.exe',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Shell32.dll,SHFormatDrive',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'C:/Windows',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SW_SHOWNORMAL);<br>老问题,结束吧.
 
来晚了.....<br>不过我记得以前在Delphi4中想启动Word,但是却发现CreateProcess不行,不<br>得已只好使用WinExec,就可以了。
 
WinExec 可以.
 
在BCB中应该怎么办呢?<br>我用Exec...系列,这样能够创建一个子进程,但是相应的父进程却<br>被kill了.<br>
 
用CreateProcess执行<br>BOOL CreateProcess(<br><br>&nbsp; &nbsp; LPCTSTR lpApplicationName, // pointer to name of executable module <br>&nbsp; &nbsp; LPTSTR lpCommandLine, // pointer to command line string<br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes <br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes <br>&nbsp; &nbsp; BOOL bInheritHandles, // handle inheritance flag <br>&nbsp; &nbsp; DWORD dwCreationFlags, // creation flags <br>&nbsp; &nbsp; LPVOID lpEnvironment, // pointer to new environment block <br>&nbsp; &nbsp; LPCTSTR lpCurrentDirectory, // pointer to current directory name <br>&nbsp; &nbsp; LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO <br>&nbsp; &nbsp; LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION &nbsp;<br>&nbsp; &nbsp;);<br><br>lpProcessInformation-&gt;hProcess为该进程的句柄<br>具体可查delphi或VC的帮助<br>&nbsp;<br>如<br>createprocess(NULL,'zq010.exe',NULL,NULL,False,0,NULL,NULL,info,pinfo)<br>句柄:pinfo.hprocess
 
在C:/Program Files/Borland/Delphi4/Demos/Doc/Filmanex/有delphi例子<br>说明用法ExecuteFile,注意引用单元shellapi.<br>
 
老掉牙的问题,请检索一下好不好.
 
Dick<br>来晚了.....<br>不过我记得以前在Delphi4中想启动Word,但是却发现CreateProcess不行,??????????不<br>得已只好使用WinExec,就可以了。<br>?????????????????????<br><br>&nbsp;<br>&nbsp;<br>
 
调用Word最好的方法是通过Com接口而不是Api。
 
我认为最好还是用CreateProcess,<br>因为CreateProcess提供了更多的对进程控制<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>st:TStartUpInfo;<br>pp:TProcessInformation;<br>ppp:Thandle;<br>begin<br>FillChar(st,sizeof(st),#0);<br>with st do<br>begin<br>&nbsp; cb:=sizeof(st);<br>&nbsp; dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;<br>&nbsp; lptitle:=nil;<br>&nbsp; wShowWindow:=SW_Show;<br>end;<br>CreateProcess(PChar('c:/program files/microsoft office/office/winword.exe'),<br>&nbsp; &nbsp; &nbsp; nil,nil,nil,true,DETACHED_PROCESS,nil,nil,st,pp);<br>//关闭进程<br>{ &nbsp;ppp:=OpenProcess(PROCESS_ALL_ACCESS, FALSE,pp.dwProcessId );<br>&nbsp; TerminateProcess(ppp,0);}<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//setwindowlong(handle , GWL_STYLE, getwindowlong(handle, GWL_STYLE) and (not WS_CAPTION or WS_BORDER) or WS_Popup);<br>end;<br><br>end.
 
创建进程<br>CreateProcess(PChar('c:/program files/microsoft office/office/winword.exe'),<br>&nbsp; &nbsp; &nbsp; nil,nil,nil,true,DETACHED_PROCESS,nil,nil,st,pp);<br><br>关闭进程<br>&nbsp; ppp:=OpenProcess(PROCESS_ALL_ACCESS, FALSE,pp.dwProcessId );<br>&nbsp; TerminateProcess(ppp,0);}<br>
 
winexc 是 win3.1 启动一个进程.createprocess 是 win 32 启动一个进程.<br>而 shellexecute 是启动 exe 的 api .都被说了.但还有 executefile(可执行文<br>件,执行参数,'',显示状态); 这个 api 是 shellexecute 的扩展.
 
用:WinExec('*.exe', WM_SHOW);
 
我没说的了,结束吧
 
后退
顶部