SDC C-Project CF Review 프로그램
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// ProcessController.cpp: implementation of the CProcessController class.
//
//////////////////////////////////////////////////////////////////////
 
 
//#include "ProcessControl.h"
#include "stdafx.h"
#include "ProcessController.h"
 
#include <tlhelp32.h>
#include <stdio.h>
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
CProcessController::CProcessController()
{
 
}
 
DWORD CProcessController::TerminateProcessByName(CString ProcessName)
{
    HANDLE         hProcessSnap = NULL; 
    DWORD          Return       = FALSE; 
    PROCESSENTRY32 pe32         = {0};
 
    ProcessName.MakeLower();
 
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
 
    if (hProcessSnap == INVALID_HANDLE_VALUE) 
        return (DWORD)INVALID_HANDLE_VALUE; 
 
    pe32.dwSize = sizeof(PROCESSENTRY32);
 
    if (Process32First(hProcessSnap, &pe32))
    { 
        DWORD Code = 0;
        DWORD         dwPriorityClass; 
       
        do 
        { 
            HANDLE hProcess; 
 
            // Get the actual priority class. 
            hProcess = OpenProcess (PROCESS_ALL_ACCESS, 
                FALSE, pe32.th32ProcessID); 
            dwPriorityClass = GetPriorityClass (hProcess); 
             
            CString Temp = pe32.szExeFile;
            Temp.MakeLower();
 
            if (Temp == ProcessName)
            {
                if (TerminateProcess(hProcess, 0))
                {
                    GetExitCodeProcess(hProcess, &Code);
                }
                else
                {
                    return GetLastError();
                }
            }
 
            CloseHandle (hProcess);
        } 
        while (Process32Next(hProcessSnap, &pe32));
        Return = TRUE; 
    } 
    else 
        Return = FALSE;    // could not walk the list of processes 
 
    // Do not forget to clean up the snapshot object. 
 
    CloseHandle (hProcessSnap);
 
    return Return;
}
 
int CProcessController::GetProcessList(PPROCESSLIST pProcessList, int MaxNum)
{
    int Index = 0;
    HANDLE         hProcessSnap = NULL; 
    DWORD          Return       = FALSE; 
    PROCESSENTRY32 pe32         = {0};
 
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
 
    if (hProcessSnap == INVALID_HANDLE_VALUE) 
        return 0; 
 
    pe32.dwSize = sizeof(PROCESSENTRY32);
 
    if (Process32First(hProcessSnap, &pe32))
    { 
        DWORD Code = 0;
        DWORD         dwPriorityClass; 
       
        do 
        { 
            HANDLE hProcess; 
 
            // Get the actual priority class. 
            hProcess = OpenProcess (PROCESS_ALL_ACCESS, 
                FALSE, pe32.th32ProcessID); 
            dwPriorityClass = GetPriorityClass (hProcess); 
             
            if (Index < MaxNum)
            {
                pProcessList[Index].ProcessName = pe32.szExeFile;
                Index++;
            }
 
            CloseHandle (hProcess);
            
        } 
        while (Process32Next(hProcessSnap, &pe32));
    } 
    else
        return 0;
           // could not walk the list of processes 
 
    // Do not forget to clean up the snapshot object. 
 
    CloseHandle (hProcessSnap);
 
    return Index;
}
 
CProcessController::~CProcessController()
{
 
}