window遍歷任務管理器的進程 查找指定進程是否在運行

int _tmain(int argc, _TCHAR* argv[])
{
 std::string strName = "QQ.exe";
 std::string strFind;
 HANDLE hProcessSnap = NULL;
 hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
 if( !hProcessSnap  ) return 0;
 PROCESSENTRY32 pe32={0};
 pe32.dwSize = sizeof( PROCESSENTRY32 );
 if(Process32First(hProcessSnap,&pe32))
 {
  char temp[256] = {0};
  do
  {
   int num = WideCharToMultiByte( CP_ACP, 0, pe32.szExeFile, wcslen( pe32.szExeFile ), 0, 0, NULL, NULL );
   if( num == 0 ) continue;
   char *szmultibyte = new char[num + 1];
   memset( szmultibyte, 0, num+1 );
   WideCharToMultiByte( CP_ACP, 0, pe32.szExeFile, wcslen( pe32.szExeFile ), szmultibyte, num, NULL, NULL );
   szmultibyte[num] = '\0';
   if( strcmp( strName.c_str(), szmultibyte ) == 0 )
   {
    strFind = szmultibyte;
    break;
   }else
   {
    std::string strTemp = szmultibyte;
    if( strTemp.find( strName.c_str() ) != strTemp.npos )
    {
     strFind = szmultibyte;
     break;
    }
   }
   delete szmultibyte;
  }
  while(Process32Next(hProcessSnap,&pe32));
 }

 if( strFind.size() ) printf( "find: %s\n", strFind.c_str() );
 getchar();
 return 0;
}

 

感覺這記憶力是越來越差了。 之前搞過,忘記了。這次寫的時候又糾結了。 寫到日誌裏來當筆記了。

發佈了136 篇原創文章 · 獲贊 5 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章