프로그래밍/c++2013. 4. 26. 11:50

 

int findProcID( CString strProcName )

{

int nProcID = 0;

strProcName .MakeUpper(); // 문자열 비교를 하기전 강제로 모두 대문자로 바꿔줌.

HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );

if ( (int)hSnapshot != -1 )
{

PROCESSENTRY32 pe32 ;
pe32.dwSize=sizeof(PROCESSENTRY32);
BOOL bContinue ;
CString tempProcessName;

if ( Process32First ( hSnapshot, &pe32 ) )
{

//프로세스 목록 검색 시작
do
{

tempProcessName = pe32.szExeFile; //프로세스 목록 중 비교할 프로세스 이름;
tempProcessName.MakeUpper();
if( ( tempProcessName.Find(strProcName , 0) != -1 ) )
{

HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, 0, pe32.th32ProcessID ); //프로세스 핸들 얻기
if( hProcess )
{

       nProcID = pe32.th32ProcessID;

// 응용해서 프로세스 핸들도 리턴이 가능하다.


}

}
bContinue = Process32Next ( hSnapshot, &pe32 );

} while ( bContinue );

}
CloseHandle( hSnapshot );

}

return nProcID ;

}



Posted by GaePein