首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi怎么判断一个exe文件是否已经运行

2012-03-09 
delphi如何判断一个exe文件是否已经运行比如只给出一个exe文件名称main.exe,在另外的一个程序里如何判断该

delphi如何判断一个exe文件是否已经运行
比如只给出一个exe文件名称main.exe,在另外的一个程序里如何判断该main.exe是否已经执行,如果没执行,则执行,在线等带,解决马上给分

[解决办法]
var
hSnapshot: THandle;
lppe: TProcessEntry32;
Found: Boolean;
begin
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
lppe.dwSize := SizeOf(TProcessEntry32);
Found := Process32First(hSnapshot, lppe);
while Found do
begin
if ((UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase( 'main.exe ')) or (UpperCase(lppe.szExeFile )=UpperCase( 'main.exe '))) then
begin
//
showmessage( '程序正在运行 ');
end;
Found := Process32Next(hSnapshot, lppe);
end;
end;
[解决办法]
在(dpr)工程文件里编写如下代码:
var
hMutex: THandle;
begin
hMutex := CreateMutex(nil, True, 'main ');
try
if not (ERROR_ALREADY_EXISTS <> GetLastError()) then
begin
Application.MessageBox( 'main 已经在运行中! ',
PChar(Application.Title), MB_OK + MB_ICONERROR);
end;
finally
ReleaseMutex(hMutex);
end;
end.

热点排行