//头文件
#include<Psapi.h>
#pragma comment(lib,"psapi.lib")
//-----------------FindProcess--------------------
//找到自己文件名
char szName[]="XXXX.exe";
DWORD aProcesses[1024],cbNeeded,dwProcesses;
char szProcessName[MAX_PATH];
unsigned int iID;
//枚举系统进程ID列表
EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded);
dwProcesses=cbNeeded/sizeof(DWORD);
for (iID = 0 ; iID < dwProcesses ; ++iID)
{
//取得进程的句柄
HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,aProcesses[iID]);
if(hProcess)
{
HMODULE hMod;
DWORD dwNeeded;
if(EnumProcessModules(hProcess,&hMod,sizeof(hMod),&dwNeeded))
{
memset(szProcessName,0,sizeof(szProcessName));
GetModuleBaseName(hProcess,hMod,szProcessName,sizeof(szProcessName));
if(0 == strcmp(szProcessName,szName))//匹配进程名
{
return ERROR_FILE_ALREADY_OPEN;
}
}
}
}