在编写程序时候,经常到判断当前进程是否已经执行,可用下面函数实现 int GetInstanceCount(const char *szName) { int nCounter = 0; PROCESSENTRY32 pe32; memset( &pe32, 0, sizeof(pe32) ); HANDLE hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap ) { pe32.dwSize = sizeof( PROCESSENTRY32 ); if( Process32First(hProcessSnap, &pe32) ) { do { if( !stricmp(pe32.szExeFile, szName) ) { nCounter ++; } }while( Process32Next(hProcessSnap, &pe32) ); } CloseHandle( hProcessSnap ); } return nCounter; }//end 使用示例: int nNum = GetInstanceCount("winWord.exe"); if(nNum >0 ) { printf("already runing ") }
阅读(2449) | 评论(1) | 转发(0) |