Sometimes you often need to check whether a program is running. In windows mobile 5.0 system development, I often use the following methods:
First:
HANDLE hMutex =: CreateMutexW (NULL, true, L "program name ");
DWORD dwError = GetLastError ();
If (dwError = ERROR_ALREADY_EXISTS)
{
: AfxMessageBox (L "The program is running! ");
Return;
}
: ReleaseMutex (hMutex );
Second:
HWND hWmp =: FindWindowW (L "Dialog", L "program window name ");
If (hWmp)
{
: AfxMessageBox (L "The program is running! ");
Return FALSE;
}
Third:
PROCESSENTRY32 lppe;
Memset (& lppe, 0, sizeof (PROCESSENTRY32 ));
HANDLE handle = createconlhelp32snapshot (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 "program name")> = 0)
{
: AfxMessageBox (L "The program is running! ");
: TerminateProcess (hh, 0 xffffffff );
: CloseHandle (hh );
Break;
}
: CloseHandle (handle );
} While (Process32Next (handle, & lppe ));
: CloseHandle (handle );
The above three methods can only check other programs, but cannot check whether they are running, because the operating mechanism of windows mobile system is not the same as that of windows XP on PC.