천객만래 [千客萬來] (It has an interminable succession of visitors)

내가 특정 프로그램명을 알고 있을 때 
그 특정 프로그램의 윈도우 핸들을 구할 수 있다.
그러면, 핸들에서 프로세스 ID를 구할 수 있게 된다.

그 다음 순서는 분명하다.
프로세스를 강제로 종료시키는 것이다.
그럼 아래 소스를 살펴보자.


HWND hOsk=::FindWindow(NULL, "SBRICH");
 if(hOsk) 
 {
  CWnd* p = CWnd::FromHandle(hOsk);
  CString sT;
  p->GetWindowTextA(sT);
  DWORD pid2;
  DWORD  pid = GetWindowThreadProcessId(hOsk, &pid2); // 바로서치 핸들로 프로세스 id를 구한다.

  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid2); 
 
      if(hProcess)
      {
         if(TerminateProcess(hProcess, 0))
         {
            unsigned long nCode; //프로세스 종료 상태 
            GetExitCodeProcess(hProcess, &nCode);
         }

         CloseHandle(hProcess); 
      }

  AfxMessageBox(sT);
 }

Posted by SB패밀리

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



출처 : 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"));//실제 죽이는 구문      

Posted by SB패밀리
프로세스 죽이기


Kernel32.dll 에 있는 ExitProcess()라는 함수에 대한 포인터를 얻고 이를
CreateRemoteThread()를 이용하여 프로세스를 호출하고 스스로 종료되게 하는 기능입니다.


BOOL SafeTerminateProcess(HANDLE hProcess, UNIT uExitCode)
{
DWORD dwTID, dwCode, dwErr = 0;
HANDLE hProcessDup = INVALID_HANDLE_VALUE;
HANDLE hRT = NULL;
HINSTANCE hKernel = GetModule Handle("Kernel32");

BOOL bSuccess = FALSE;
BOOL bDup = DuplicateHandle(GetCurrentProcess(),
hProcess,
GetCurrentProcess(),
&hProcessDup,
PROCESS_ALL_ACCESS,
FALSE,
0};
if (GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) && (dwCode == STILL_ACTIVE) )
{
FARPROC pfnExitProc;
pfnExitProc = GetProcAddress(hKernel, "ExitProcess");
hRT = CreateRemoteThread((bDup) ? hProcessDup : hProcess,
NULL,
0,
(LPTHREAD_START_ROUTINE) pfnExitProc,
(PVOID) uExitCode, 0, &dwTID);

if (hRT == NULL) dwErr = GetLastError();
}
else
{
dwErr = ERROR_PROCESS_ABORTED;
}

if (hRT)
{
WaitForSingleObject((bDup) ? hProcessDup : hProcess, INFINITE);
Closehandle(hRT);
bSuccess = TRUE;
}

if (bDup)
CloseHandle(hProcessDup);
if (! bSuccess)
SetLastError(dwErr);

return bSuccess;
}


 

Posted by SB패밀리

내가 특정 프로그램명을 알고 있을 때
그 특정 프로그램의 윈도우 핸들을 구할 수 있다.
그러면, 핸들에서 프로세스 ID를 구할 수 있게 된다.

그 다음 순서는 분명하다.
프로세스를 강제로 종료시키는 것이다.
그럼 아래 소스를 살펴보자.


HWND hOsk=::FindWindow(NULL, "SBRICH");
 if(hOsk)
 {
  CWnd* p = CWnd::FromHandle(hOsk);
  CString sT;
  p->GetWindowTextA(sT);
  DWORD pid2;
  DWORD  pid = GetWindowThreadProcessId(hOsk, &pid2); // 바로서치 핸들로 프로세스 id를 구한다.

  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid2);
 
      if(hProcess)
      {
         if(TerminateProcess(hProcess, 0))
         {
            unsigned long nCode; //프로세스 종료 상태
            GetExitCodeProcess(hProcess, &nCode);
         }

         CloseHandle(hProcess);
      }

  AfxMessageBox(sT);
 }
 

Posted by SB패밀리

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

출처 : 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"));//실제 죽이는 구문                                                                           
                                                                                                                                                          

Posted by SB패밀리