有时候经常需要检查某个程序是否正在运行,在windows mobile 5.0系统开发中我经常用到以下几种方法:
第一种:
HANDLE hMutex=::CreateMutexW(NULL,true,L"程序名");
DWORD dwError=GetLastError();
if(dwError==ERROR_ALREADY_EXISTS)
{
::AfxMessageBox(L"The program is running!");
return ;
}
::ReleaseMutex(hMutex);
第二种:
HWND hWmp=::FindWindowW(L"Dialog",L"程序窗口名");
if(hWmp)
{
::AfxMessageBox(L"The program is running!");
return FALSE;
}
第三种:
PROCESSENTRY32 lppe;
memset(&lppe,0,sizeof(PROCESSENTRY32));
HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
lppe.dwSize=sizeof(PROCESSENTRY32);
::Process32First(handle,&lppe);
do
{
HANDLE hh=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,lppe.th32ProcessID);
CString temp(lppe.szExeFile);
if(temp.Find(L"程序名")>=0)
{
::AfxMessageBox(L"The program is running!");
::TerminateProcess(hh,0xffffffff);
::CloseHandle(hh);
break;
}
::CloseHandle(handle);
}while(Process32Next(handle,&lppe));
::CloseHandle(handle);
以上三种方法,只能检查其它程序,而不能检查自身是否正在运行,因为windows mobile 系统的运行机制和PC机上的windows XP是不太一样的.