界面如下:

进程管理器-风君雪科技博客

主要代码如下:

  1 void CProcessManagerDlg::InitProcessList(void)
  2 {
  3     m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
  4     m_ListProcess.InsertColumn(0, _T("序号"));
  5     m_ListProcess.InsertColumn(1, _T("进程ID"));
  6     m_ListProcess.InsertColumn(2, _T("进程名"));
  7     m_ListProcess.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
  8     m_ListProcess.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
  9     m_ListProcess.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
 10 }
 11 
 12 void CProcessManagerDlg::InitDLLList(void)
 13 {
 14     m_ListDLL.SetExtendedStyle(m_ListDLL.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
 15     m_ListDLL.InsertColumn(0, _T("序号"));
 16     m_ListDLL.InsertColumn(1, _T("DLL名称"));
 17     m_ListDLL.InsertColumn(2, _T("DLL路径"));
 18     m_ListDLL.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
 19     m_ListDLL.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
 20     m_ListDLL.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
 21 }
 22 
 23 void CProcessManagerDlg::ShowProcessList(void)
 24 {
 25     m_ListProcess.DeleteAllItems();
 26     HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 27     if (INVALID_HANDLE_VALUE == hSnap)
 28     {
 29         AfxMessageBox(_T("CreateToolhelp32Snapshot Error!"));
 30         return;
 31     }
 32 
 33     CString str;
 34     int i = 0;
 35     PROCESSENTRY32 Pe32 = {0};
 36     Pe32.dwSize = sizeof(PROCESSENTRY32);
 37     BOOL bRet = Process32First(hSnap, &Pe32);
 38     while (bRet)
 39     {
 40         str.Format(_T("%d"), i);
 41         m_ListProcess.InsertItem(i, str);
 42         str.Format(_T("%d"), Pe32.th32ProcessID);
 43         m_ListProcess.SetItemText(i, 1, str);
 44         m_ListProcess.SetItemText(i++, 2, Pe32.szExeFile);
 45         bRet = Process32Next(hSnap, &Pe32);
 46     }
 47     CloseHandle(hSnap);
 48 }
 49 
 50 void CProcessManagerDlg::ShowDLLList(DWORD dwPid)
 51 {
 52     m_ListDLL.DeleteAllItems();
 53     MODULEENTRY32 Me32 = {0};
 54     Me32.dwSize = sizeof(MODULEENTRY32);
 55     HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPid);
 56     DWORD dwErr = GetLastError();
 57 
 58     CString str;
 59     int i = 0;
 60     BOOL bRet = Module32First(hSnap, &Me32);
 61     while (bRet)
 62     {
 63         str.Format(_T("%d"), i);
 64         m_ListDLL.InsertItem(i, str);
 65         m_ListDLL.SetItemText(i, 1, Me32.szModule);
 66         m_ListDLL.SetItemText(i++, 2, Me32.szExePath);
 67         bRet = Module32Next(hSnap, &Me32);
 68     }
 69     CloseHandle(hSnap);
 70 }
 71 
 72 void CProcessManagerDlg::OnBnClickedButtonCreate()
 73 {
 74     // TODO: 在此添加控件通知处理程序代码
 75     CCreateProcess CreateProcessDlg;
 76     CreateProcessDlg.DoModal();
 77     ShowProcessList();
 78 }
 79 
 80 void CProcessManagerDlg::OnBnClickedButtonTerminate()
 81 {
 82     // TODO: 在此添加控件通知处理程序代码
 83     POSITION pos = m_ListProcess.GetFirstSelectedItemPosition();
 84     int iSelected = -1;
 85 
 86     while (pos)
 87     {
 88         iSelected = m_ListProcess.GetNextSelectedItem(pos);
 89     }
 90 
 91     if (-1 == iSelected)
 92     {
 93         AfxMessageBox(_T("请选择要结束的进程"));
 94         return;
 95     }
 96 
 97 
 98     TCHAR szProcessID[10] = {0};
 99     m_ListProcess.GetItemText(iSelected, 1, szProcessID, 10);
100 
101     DWORD dwPid = _wtoi(szProcessID);
102     HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
103     if (NULL == hProcess)
104     {
105         return;
106     }
107 
108     TerminateProcess(hProcess, 0);
109     CloseHandle(hProcess);
110     ShowProcessList();
111 }
112 
113 void CProcessManagerDlg::OnBnClickedButtonStop()
114 {
115     // TODO: 在此添加控件通知处理程序代码
116     POSITION pos = m_ListProcess.GetFirstSelectedItemPosition();
117     int iSelected = -1;
118 
119     while (pos)
120     {
121         iSelected = m_ListProcess.GetNextSelectedItem(pos);
122     }
123 
124     if (-1 == iSelected)
125     {
126         AfxMessageBox(_T("请选择要停止的进程"));
127         return;
128     }
129 
130 
131     TCHAR szProcessID[10] = {0};
132     m_ListProcess.GetItemText(iSelected, 1, szProcessID, 10);
133 
134     DWORD dwPid = _wtoi(szProcessID);
135     HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwPid);
136     if (INVALID_HANDLE_VALUE == hSnap)
137     {
138         AfxMessageBox(_T("CreateToolhelp32Snapshot Error"));
139         return;
140     }
141 
142     THREADENTRY32 Te32 = {0};
143     Te32.dwSize = sizeof(THREADENTRY32);
144     BOOL bRet = Thread32First(hSnap, &Te32);
145 
146     while (bRet)
147     {
148         if (Te32.th32OwnerProcessID == dwPid)
149         {
150             HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, Te32.th32ThreadID);
151             SuspendThread(hThread);
152             CloseHandle(hThread);
153         }
154 
155         bRet = Thread32Next(hSnap, &Te32);
156     }
157     CloseHandle(hSnap);
158     ShowProcessList();
159 }
160 
161 void CProcessManagerDlg::OnBnClickedButtonResume()
162 {
163     // TODO: 在此添加控件通知处理程序代码
164     POSITION pos = m_ListProcess.GetFirstSelectedItemPosition();
165     int iSelected = -1;
166 
167     while (pos)
168     {
169         iSelected = m_ListProcess.GetNextSelectedItem(pos);
170     }
171 
172     if (-1 == iSelected)
173     {
174         AfxMessageBox(_T("请选择要恢复的进程"));
175         return;
176     }
177 
178 
179     TCHAR szProcessID[10] = {0};
180     m_ListProcess.GetItemText(iSelected, 1, szProcessID, 10);
181 
182     DWORD dwPid = _wtoi(szProcessID);
183     HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwPid);
184     if (INVALID_HANDLE_VALUE == hSnap)
185     {
186         AfxMessageBox(_T("CreateToolhelp32Snapshot Error"));
187         return;
188     }
189 
190     THREADENTRY32 Te32 = {0};
191     Te32.dwSize = sizeof(THREADENTRY32);
192     BOOL bRet = Thread32First(hSnap, &Te32);
193 
194     while (bRet)
195     {
196         if (Te32.th32OwnerProcessID == dwPid)
197         {
198             HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, Te32.th32ThreadID);
199             ResumeThread(hThread);
200             CloseHandle(hThread);
201         }
202 
203         bRet = Thread32Next(hSnap, &Te32);
204     }
205     CloseHandle(hSnap);
206     ShowProcessList();
207 }
208 
209 void CProcessManagerDlg::OnBnClickedButtonLook()
210 {
211     // TODO: 在此添加控件通知处理程序代码
212     POSITION pos = m_ListProcess.GetFirstSelectedItemPosition();
213     int iSelected = -1;
214 
215     while (pos)
216     {
217         iSelected = m_ListProcess.GetNextSelectedItem(pos);
218     }
219     
220     if (-1 == iSelected)
221     {
222         AfxMessageBox(_T("请选择要查看模块的进程"));
223         return;
224     }
225 
226     
227     TCHAR szProcessID[10] = {0};
228     m_ListProcess.GetItemText(iSelected, 1, szProcessID, 10);
229     //AfxMessageBox(szProcessID);
230     DebugPrivilege();
231     ShowDLLList(_wtoi(szProcessID));
232 }
233 
234 void CProcessManagerDlg::OnBnClickedButtonExit()
235 {
236     // TODO: 在此添加控件通知处理程序代码
237     OnOK();
238 }
239 
240 
241 void CProcessManagerDlg::DebugPrivilege(void)
242 {
243     HANDLE hToken = NULL;
244     BOOL bRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken);
245     if (TRUE == bRet)
246     {
247         TOKEN_PRIVILEGES tp;
248         tp.PrivilegeCount = 1;
249         LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid);
250         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
251         AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
252 
253         CloseHandle(hToken);
254     }
255 }

下载地址如下:

http://pan.baidu.com/s/1o659BR0