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

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



출처 : 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패밀리
프로세스가 살아있는지 검사, 프로세스 죽이기

프로세스가 살아있는지 검사
 
// uses 에 TlHelp32 추가
functionIsRunningProcess(constProcName: String) : Boolean;
var
  Process32: TProcessEntry32;
  SHandle:   THandle;
  Next:      Boolean;

begin
  Result:=False;
 
  Process32.dwSize:=SizeOf(TProcessEntry32);
  SHandle         :=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
 
  // 프로세스 리스트를 돌면서 매개변수로 받은 이름과 같은 프로세스가 있을 경우 True를 반환하고 루프종료
 ifProcess32First(SHandle, Process32) then begin
    repeat
      Next:=Process32Next(SHandle, Process32);
      if AnsiCompareText(Process32.szExeFile, Trim(ProcName))=0then begin
        Result:=True;
        break;
     end;
    until notNext;
  end;
  CloseHandle(SHandle);
end;
 
 
// 사용예.  계산기(calc.exe)가 떠있는지 확인하고 싶을 경우
procedureTForm1.Button1Click(Sender: TObject);
begin
  ifIsRunningProcess('calc.exe') thenOutputDebugString('계산기 있음')
  else                                 OutputDebugString('계산기 없음');
end;
 
 
 
 
 
 
프로세스 죽이기 (강제종료)
 
// uses 에 TlHelp32 추가
functionKillProcess(constProcName: String): Boolean;
var
  Process32: TProcessEntry32;
  SHandle:   THandle;
  Next:      Boolean;
  hProcess: THandle;
  i: Integer;
 
begin
  Result:=True;
 
  Process32.dwSize       :=SizeOf(TProcessEntry32);
  Process32.th32ProcessID:=0;
  SHandle                :=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 
  // 종료하고자 하는 프로세스가 실행중인지 확인하는 의미와 함께...
  ifProcess32First(SHandle, Process32) then begin
    repeat
      Next:=Process32Next(SHandle, Process32);
      ifAnsiCompareText(Process32.szExeFile, Trim(ProcName))=0thenbreak;
    until notNext;
  end;
  CloseHandle(SHandle);
 
  // 프로세스가 실행중이라면 Open & Terminate
  ifProcess32.th32ProcessID<>0then begin
    hProcess:=OpenProcess(PROCESS_TERMINATE, True, Process32.th32ProcessID);
    ifhProcess<>0then begin
      if notTerminateProcess(hProcess, 0) thenResult:=False;
    end
    // 프로세스 열기 실패
    elseResult:=False;
 
    CloseHandle(hProcess);
  end// if Process32.th32ProcessID<>0
  elseResult:=False;
end;
 
 
 
 
// 사용예. 계산기(calc.exe) 프로세스를 죽이고자 할 때.
procedureTForm1.Button1Click(Sender: TObject);
begin
  ifKillProcess('calc.exe') thenOutputDebugString('계산기 죽이기 실패. 계산기 프로세스가 없었거나...')
  else                            OutputDebugString('계산기 죽이기 성공.');
end;




출처 : http://blog.naver.com/erroring/150097804539
VC++에서 사용중인건데... 루틴은 동일하고 delphi로 되어있어서 퍼옵니다.

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패밀리