본문 바로가기

Computer Science/Languages

[MFC] force shutdown process

728x90

#include <TlHelp32.h>

 

BOOL Cex_ProcessKDlg::KillProcess(CString strName)

{

strName.MakeUpper();

HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );

if ( (int)hSnapshot != -1 )

{

PROCESSENTRY32 pe32 ;

pe32.dwSize=sizeof(PROCESSENTRY32);

BOOL bContinue ;

CString strProcessName;

if ( Process32First ( hSnapshot, &pe32 ) )

{

do

{

strProcessName = pe32.szExeFile; //strProcessName이 프로세스 이름;

strProcessName = strProcessName.MakeUpper();

if( ( strProcessName.Find(strName,0) != -1 ) )

{                                        

HANDLE hProcess = OpenProcess( PROCESS_TERMINATE , FALSE, pe32.th32ProcessID );

if( hProcess )

{

DWORD dwExitCode;

GetExitCodeProcess( hProcess, &dwExitCode);

TerminateProcess( hProcess, dwExitCode);

CloseHandle(hProcess);

CloseHandle( hSnapshot );

return TRUE;

}

return FALSE;

}

bContinue = Process32Next ( hSnapshot, &pe32 );

} while ( bContinue );

}

CloseHandle( hSnapshot );

}

 

return FALSE;

}


[스크랩]