procedure Tfrm_rtu_nwkmain.checkpro; var ippe:tprocessentry32; sshandle:thandle; found:boolean; ttt : boolean; begin sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0); found:=process32first(sshandle,ippe); while found do begin if ansicomparetext(extractfilename(ippe.szExeFile),'Myexe.exe')=0 then begin showmessage('fount'); break; end; found:=process32next(sshandle,ippe); sleep(1); end;end;
if Findwindow(nil,'应用程序标题')<>0 then showmessage('程序已运行了');
程序运行的时候执行 CreateMutex(nil,true,sMutexName); if GetLastError=ERROR_ALREADY_EXISTS then 该程序已经运行
使用完整路径做比较(搬顶楼的代码): uses tlhelp32,psapi; -------------------------- var ippe:tprocessentry32; sshandle:thandle; found:boolean; ttt : boolean; pProcess:THandle; hMod:HMODULE; buf:array[0..MAX_PATH] of char; cbNeeded:DWORD; len:Integer; begin sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0); ippe.dwSize:=sizeof(ippe); found:=process32first(sshandle,ippe); while found do begin pProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,FALSE,ippe.th32ProcessID); if pProcess>0 then begin if( EnumProcessModules( pProcess, @hMod, sizeof( hMod ), cbNeeded ) ) then begin ZeroMemory(@buf,MAX_PATH+1); len:=GetModuleFileNameEx(pProcess, hMod,buf,sizeof (buf)); buf[len]:=#0; //if ansicomparetext(extractfilename(ippe.szExeFile),'Myexe.exe')=0 then if ansicomparetext(buf,Application.ExeName)=0 then begin showmessage('fount'); break; end; end; end; found:=process32next(sshandle,ippe); //sleep(1); end;end;
var
ippe:tprocessentry32;
sshandle:thandle;
found:boolean;
ttt : boolean;
begin
sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0);
found:=process32first(sshandle,ippe);
while found do
begin
if ansicomparetext(extractfilename(ippe.szExeFile),'Myexe.exe')=0 then
begin
showmessage('fount');
break;
end;
found:=process32next(sshandle,ippe); sleep(1);
end;end;
if Findwindow(nil,'应用程序标题')<>0 then
showmessage('程序已运行了');
CreateMutex(nil,true,sMutexName);
if GetLastError=ERROR_ALREADY_EXISTS then
该程序已经运行
uses
tlhelp32,psapi;
--------------------------
var
ippe:tprocessentry32;
sshandle:thandle;
found:boolean;
ttt : boolean;
pProcess:THandle;
hMod:HMODULE;
buf:array[0..MAX_PATH] of char;
cbNeeded:DWORD;
len:Integer;
begin
sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0);
ippe.dwSize:=sizeof(ippe);
found:=process32first(sshandle,ippe);
while found do
begin
pProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or
PROCESS_VM_READ,FALSE,ippe.th32ProcessID);
if pProcess>0 then
begin
if( EnumProcessModules( pProcess, @hMod, sizeof( hMod ),
cbNeeded ) ) then
begin
ZeroMemory(@buf,MAX_PATH+1);
len:=GetModuleFileNameEx(pProcess, hMod,buf,sizeof (buf));
buf[len]:=#0;
//if ansicomparetext(extractfilename(ippe.szExeFile),'Myexe.exe')=0 then
if ansicomparetext(buf,Application.ExeName)=0 then
begin
showmessage('fount');
break;
end;
end;
end;
found:=process32next(sshandle,ippe); //sleep(1);
end;end;