如何在服务程序中判断某一个程序是否运行,并对其进行关闭和重新启动 (50分)

S

sccphi

Unregistered / Unconfirmed
GUEST, unregistred user!
我创建了一个Service Application的一个工程,让它在
WIN2000启动便执行此程序,其中有一个功能为判断另一个
程序(非Service程序)是否已运行,如果已运行进行关闭
再重新启动此程序(非Service程序),但是在Service
Application 中始终不能判断出另一个程序是否在运行
一致认为没有运行。下面为我的代码(这些代码我放在一个TIMER中来控制),
var H:THandle;
P:DWORD;
Begin
H:=FindWindow(nil,'Project1'); //Project1为我的另外一个程序
问题一、//????在此处无论Project1是否运行,H的值它都 为0,这是为什么?
if H<>0 then
begin
try GetWindowThreadProcessId(H,@P); Except End;
if P<>0 then
Begin
try
try TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF); Except End;
WinExec(pchar('g:/test/project1.exe'),0);
问题二、????//好象在Service Application通过WinExec打开的文件都不能看见窗体,只能在任务中看见以运行了此程序,为什么??
Except End;
End;
end;
End;

还望高手多指教!谢谢!

 
不知是否和权限有关,试试下面的:

来自:jsxjd, 时间:2002-11-22 12:19:00, ID:1450931
---------------------------------------------------------
获得进程列表,并终止 Excel 进程
procedure TForm1.Button1Click(Sender: TObject);
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
Ret : BOOL;
ProcessID : integer;
s:string;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
Ret:=Process32First(FSnapshotHandle,FProcessEntry32);
Memo1.clear;
while Ret do
begin
Memo1.lines.add(FProcessEntry32.szExeFile);
s:=ExtractFileName(FProcessEntry32.szExeFile);
if s='EXCEL.EXE' then
begin
ProcessID:=FProcessEntry32.th32ProcessID;
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);
s:='';
end;
Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
 
试试用ShellExecute
 
现在能够判断出另一个程序是否已运行
但不能重新启动,ShellExecute、WinExec都不行????
 
http://delphibbs.com/delphibbs/dispq.asp?lid=1468562
 
//==============================================================================
//强制终止某应用程序运行********************************************************
//==============================================================================
procedure AppForceExit(const AppName: string);
var lppe: TProcessEntry32;
ssHandle: THandle;
AppFound: Boolean;
Wnd: HWND;
begin
ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
AppFound := Process32First(sshandle, lppe);
while AppFound do
begin
//其中lppe.szExefile就是程序名**********************************************
if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then
begin
Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
TerminateProcess(Wnd, 0);
end;
AppFound := Process32Next(ssHandle, lppe);
end;
end;

//==============================================================================
//在进程中查找某应用程序是否正在运行********************************************
//==============================================================================
function ProcessFound(const AppName: string): Boolean;
var lppe: TProcessEntry32;
ssHandle: THandle;
AppFound: Boolean;
begin
Result := false;
ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
AppFound := Process32First(sshandle, lppe);
while AppFound do
begin
//其中lppe.szExefile就是程序名**********************************************
if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then Result := true;
AppFound := Process32Next(ssHandle, lppe);
end;
end;
 
你程序的窗体标题是否是project1,如果不是,肯定是找不到的,h当然为0
 
to:sccphi
你是否可以告诉我:现在能够判断出另一个程序是否已运行的方法是什么?
 
顶部