如何利用Delphi 6 调用其他可执行文件(.exe)???(100分)

C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
如何利用Delphi 6 调用其他可执行文件(.exe)???
最近编程需要调用其他可执行文件,请问各位高手该如何解决?
最好能给原来的可执行文件限制在我设定的窗口内,并且可以在源程序的窗口上添加一些修饰图案,不改变源程序的输入、输出文件格式。
 

晋少

Unregistered / Unconfirmed
GUEST, unregistred user!
WinExec或用WINAPI调用。
 
C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
能告诉我如何使用吗,或者哪儿可以找到资料?谢谢!
 
L

lxw5214

Unregistered / Unconfirmed
GUEST, unregistred user!
shellexecute函数也行,说明查帮助
 
D

Dong_HC

Unregistered / Unconfirmed
GUEST, unregistred user!
ShellExecute函数原型及参数含义如下:
ShellExecute(
HWND hwnd, //父窗口句柄
LPCSTR lpOperation, //操作类型
LPCSTR lpFile, //要进行操作的文件或路径
LPCSTR lpParameters, //当lpOperation为“explore”时指定要传递的参数,通常设为NULL
LPCSTR lpDirectory, //指定默认目录,通常设为NULL
INT nShowCmd //文件打开的方式,以通常方式还是最大化或最小化显示
)
例子如下:
//调用计算器
ShellExecute(NULL,"open","calc.exe",NULL,NULL,SW_SHOWNORMAL);


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

winexec('d:/***.exe',1);
 
C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
Dong_HC,您好!
怎么用你的例子,编译不过去?
 
C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
Winexec(pchar('C:/D区/delphi 20080108/test/极品时刻表/JPSKB.exe'),sw_shownormal);
该软件是windows软件;
Winexec(pchar('C:/D区/ppp/iii/iii.exe'),sw_shownormal);
该软件是dos软件;
现在我遇到的问题是windows软件可以正常运行,而dos软件却不能正常运行,请问是怎么回事?
 
P

PING-Delphi

Unregistered / Unconfirmed
GUEST, unregistred user!
to calf100:
Dong_HC,您好!
怎么用你的例子,编译不过去?
如果使用ShellExecute,参数应该这样:
ShellExecute(nil,"open","calc.exe",nil,nil,SW_SHOWNORMAL);
 
F

fengfan

Unregistered / Unconfirmed
GUEST, unregistred user!
dos软件要使用cmd。command.com
 
T

tylerzhang

Unregistered / Unconfirmed
GUEST, unregistred user!
ShellExecute(Handle, 'open', PChar(ExtractFilePath(Application.ExeName)+'xx.exe'), nil, nil, SW_SHOWNORMAL);
 
U

ufo

Unregistered / Unconfirmed
GUEST, unregistred user!
不要忘了uses shellapi 单元
 
F

fanboynet

Unregistered / Unconfirmed
GUEST, unregistred user!
要限制在你的窗口内,用窗口绑架技术.
运行的话winexec基本上都能对付.
 
C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
调用外部应用程序工作已经完成,可是怎么能正常关闭外部应用程序呢?
看到很多资料介绍用FindWindow和SendMessage函数进行操作,可是我却不能完成。
FindWindow不能正确获得句柄。
 
S

sunzhanwei

Unregistered / Unconfirmed
GUEST, unregistred user!
建议使用CreateProcess函数来创建,
unit RunAppUnit;
//运行和关闭外部程序单元
//作者:孙占伟
//使用时先引用此单元,然后就可以使用了.
//关闭已经启动的程序的时候使用TerminateApp()
//例子:
//var
//s:TRunApp;
//begin

// s:=TRunApp.Create;
// 使用命令行
// s.CommandLines='/s /m';
// s.RunApp(应用程序路径);
//end;

//关闭程序
//begin

// TerminateApp(s.GetProcessID,5000);//5000指的是关闭程序超时等待的时间
//end;

interface
uses
Windows,Messages;
type
TRunApp=class
private
AppStartInfo:TStartupInfo;
ProcessInfo: TProcessInformation;
CmdLines:string;//命令行
public
constructor Create();
function RunApp(AppName:string):Boolean;//运行的程序路径
function GetProcessID():DWORD;//获取运行的程序的进程ID,用来关闭程序使用
published
property CommandLines:string read CmdLines write CmdLines;
end;
//使用此函数来关闭打开的程序
function TerminateApp(dwPID:DWORD;dwTimeOut:DWORD):DWORD;stdcall;
implementation
{ TRunApp }
constructor TRunApp.Create;
begin
inherited Create;
FillChar(AppStartInfo, Sizeof(AppStartInfo), #0);
AppStartInfo.cb:=SizeOf(AppStartInfo);
end;

function TerminateAppEnum(TAppHwnd: HWND;
LParam: LPARAM): Boolean;
var
dwID:DWORD;
begin
GetWindowThreadProcessId(TAppHwnd,dwID);
if (dwID=DWORD(LParam)) then
begin
PostMessage(TAppHwnd,WM_CLOSE,0,0);
end;
Result :=True;
end;

function TerminateApp(dwPID:DWORD;dwTimeOut:DWORD):DWORD;
var
hProc:THandle;
dwRet:BOOL;
begin
Result :=0;
hProc:=OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE,False,dwPID);
if (hProc=0) then
begin
Exit;
end;
EnumWindows(@TerminateAppEnum,dwPID);
if (WaitForSingleObject(hProc, dwTimeout)<>WAIT_OBJECT_0) then
begin
dwRet:=TerminateProcess(hProc,0);
if dwRet=True then
Result:=2
else
Result:=0;
end
else
begin
Result:=1;
end;
CloseHandle(hProc);
end;

function TRunApp.GetProcessID: DWORD;
begin
Result:=ProcessInfo.dwProcessId;
end;

function TRunApp.RunApp(AppName: string): Boolean;
begin
Result := CreateProcess(nil,
PChar(AppName+' '+self.CmdLines),
nil, nil,
False,
NORMAL_PRIORITY_CLASS,
nil, nil,
AppStartInfo,
ProcessInfo
);
end;
end.
 
C

calf100

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 
顶部