如标题:
在delphi程序怎样调用外部.exe(需要带参数调用)程序,及怎样通过delphi程序判断外部程序是否在运行,和怎样结束外部程序的运行??
在delphi程序怎样调用外部.exe(需要带参数调用)程序,及怎样通过delphi程序判断外部程序是否在运行,和怎样结束外部程序的运行??
解决方案 »
- Oracle 数据库插入数据出错
- fastreport 哪个版本支持中文最好 并且稳定 ?
- 如何实现象QQ那样在一般WINDOWS窗口界面上写信息提交后和远程服务器进行数据交互?
- 我的activex没有规律的出错啊,不知道为什么,烦!
- 求助一个类似于QQ的好友列表的控件!!!
- 如何判断某一目录下是否存在某一文件???在线等待!!!
- ????有谁知道Delphi、VC、Java、VB等的开发工具是什么吗????
- 用OLE操作word的问题
- 如何在程序中动态建立lookupField?
- 关于主键的问题
- 用Tmemo控件显示memory字段时,为什么有时会显示成问号?
- 有编写外挂经验的朋友请进~~~100等着你
Winexec('C:\AAA.EXE 555', SW_SHOW);其中555为参数。
可以通过判断相应的进程是否存在;调用API,
,和怎样结束外部程序的运行??
可以结束进程;调用API
具体的凼数记不清了,查HELP的WINDOWS SDK,
begin
hwnd:=findwindow('TForm1','Form1');
if hwnd<>0 then
sendMessage(hwnd,WM_CLOSE,0,0);
end;
//调用外部程序 格式如: diff.exe a.txt b.txt >>c.txt
function TDiff.AnsiDiff(File1,File2:string;stream:TmemoryStream):string;
const
DiffStr='/c diff %s %s >>%s';
var
sTmp:string;
begin
sTmp:=formatDateTime('mmddhhmmss',now)+inttostr(getTickcount)+inttostr(random(1000))+'.txt';
ShellExecute (0,nil,'cmd.exe',pchar(Format(DiffStr,[File1,File2,sTmp])),nil,0);
if stream<>nil then
stream.LoadFromFile(sTmp);
result:=sTmp;
end;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapShotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(AFileName))
or (UpperCase(FProcessEntry32.szExeFile ) = UpperCase(AFileName))) then
TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0);
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
end;
//AFileName为要终止的程序全名
下面这个函数用于启动一个程序,我的代码拷出来的,懒得改了,自己看吧
FHProcessService是进程句柄,THandle类型
function TfrmMain.StartService(bStart: Boolean): Boolean;
var
WorkDir : string;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
begin
Result := False; if bStart then
begin
if not OpenIpService then
begin
WorkDir := ExtractFileDir(Application.ExeName);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_SHOWDEFAULT;
if not CreateProcess(nil,
PChar(GetExePath + szServerServiceFileName), { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
True, { handle inheritance flag }
// CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
PChar(WorkDir), { pointer to current directory name, PChar}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF } then
begin
MemoError.Lines.Add('服务创建失败');
Exit;
end;
MemoError.Lines.Add('服务创建成功');
FHProcessService := ProcessInfo.hProcess;
end;
if FCheckServerThread <> nil then
begin
if not FCheckServerThread.Terminated then
FCheckServerThread.Terminate;
FCheckServerThread.Free; end; FCheckServerThread := TCheckServerThread.Create(False);
MemoError.Lines.Add('开始监视服务器运行情况');
FCheckServerThread.Resume; Result := True;
end
else
begin
if FHProcessService <> 0 then
begin
FControlEvent.SetEvent;
if WaitForSingleObject(FHProcessService, 10000) <> WAIT_OBJECT_0 then
begin
Result := False;
end
else
Result := True;
CloseHandle(FHProcessService);
FHProcessService := 0;
end;
end;
end;FCheckServerThread 是自己写的监视进程线程,代码如下procedure TCheckServerThread.Execute;
begin
inherited;
if frmMain.FHProcessService <> 0 then
begin if WaitForSingleObject(frmMain.FHProcessService, INFINITE) <> WAIT_OBJECT_0
then
PostMessage(frmMain.Handle, WM_WAITERROR, 0, 0)
else
PostMessage(frmMain.Handle, WM_PROCESSOVER, 0, 0);
end;
end;
这种方法好处在于,不消耗系统资源,内核对象在被等待时期,不占用任何资源(对象本身除外) ,比循环扫描进程列表要好很多
The TerminateProcess function terminates the specified process and all of its threads.
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);Parameters
hProcess
[in] Handle to the process to terminate.
The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.uExitCode
[in] Exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.但个人不建议使用,该方法强行中止,有可能引起资源不释放,建议使用fancylee(小恐龙) 的方法发送消息,如果没有消息循环,可以用事件通知(程序的是自己写的),否则,就只有强行中止了