본문 바로가기
IT-개발,DB

[개발/MFC] 실행중인 프로세스 강제 종료시키기

by SB리치퍼슨 2011. 4. 15.

윈도우즈에서 실행중인 프로세스를 강제 종료시키자.
내가 사용하는 방법도 있지만 아래 소스가 잘 정리되어 있어서 그대로 스크랩했다.

출처 : http://koronaii.tistory.com/241

BOOL CGomControl::ProcessKill(CString strProcessName)                                                                      
{                                                                                                                                                          
   HANDLE hProcessSnap = NULL;                                                                                                         
   BOOL bRet = FALSE;                                                                                                                         
   PROCESSENTRY32 pe32 = {0};                                                                                                         
                                                                                                                                                          
   hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);                                             
   if (hProcessSnap == (HANDLE)-1)                                                                                                      
       return false;                                                                                                                                 
   pe32.dwSize = sizeof(PROCESSENTRY32);                                                                                         
                                                                                                                                                          
   //프로세스가 메모리상에 있으면 첫번째 프로세스를 얻는다                                                                       
   if (Process32First(hProcessSnap, &pe32))                                                                                           
   {                                                                                                                                                     
       BOOL bCurrent = FALSE;                                                                                                              
       MODULEENTRY32 me32 = {0};                                                                                                      
                                                                                                                                                          
       do {                                                                                                                                            
           bCurrent = GetProcessModule(pe32.th32ProcessID,strProcessName);                                           
           if(bCurrent) {                                                                                                                           
               HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);          
               if(hProcess) {                                                                                                                       
                   if(TerminateProcess(hProcess, 0)) {                                                                                   
                       unsigned long nCode; //프로세스 종료 상태                                                                      
                       GetExitCodeProcess(hProcess, &nCode);                                                                        
                   }                                                                                                                                     
                   CloseHandle(hProcess);                                                                                                    
               }                                                                                                                                         
           }                                                                                                                                             
       }                                                                                                                                                 
       //다음 프로세스의 정보를 구하여 있으면 루프를 돈다.                                                                          
       while (Process32Next(hProcessSnap, &pe32));                                                                                
   }                                                                                                                                                     
                                                                                                                                                          
   CloseHandle (hProcessSnap);                                                                                                            
   return true;                                                                                                                                      
}                                                                                                                                                           
  
                                                                                                                                                        
BOOL CGomControl::GetProcessModule(DWORD dwPID, CString sProcessName)                                       
{                                                                                                                                                          
   HANDLE hModuleSnap = NULL;                                                                                                          
   MODULEENTRY32 me32 = {0};                                                                                                          
                                                                                                                                                          
   hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);                                         
   if(hModuleSnap == (HANDLE)-1)                                                                                                        
       return (FALSE);                                                                                                                            
   me32.dwSize = sizeof(MODULEENTRY32);                                                                                          
                                                                                                                                                          
   //해당 프로세스의 모듈리스트를 루프로 돌려서 프로세스이름과 동일하면                                                    
   //true를 리턴한다.                                                                                                                             
   if(Module32First(hModuleSnap, &me32)) {                                                                                          
       do {                                                                                                                                            
           if(me32.szModule == sProcessName)                                                                                         
           {                                                                                                                                             
               CloseHandle (hModuleSnap);                                                                                                 
               return true;                                                                                                                          
           }                                                                                                                                             
       }                                                                                                                                                 
                                                                                                                                                          
       while(Module32Next(hModuleSnap, &me32));                                                                                  
   }                                                                                                                                                     
                                                                                                                                                          
   CloseHandle (hModuleSnap);                                                                                                             
   return false;                                                                                                                                     
}                                                                                                                                                          
                                                                                                                                                          
                                                                                                                                                          
                                                                                                                                                          
bool CGomControl::GomClose(){                                                                                                          
   return ProcessKill(_T("GOM.exe"));//실제 죽이는 구문                                                                           
                                                                                                                                                          

반응형

댓글