multimap实例 -- 添加、遍历数据

 

1 #pragma warning(disable:4786)
2 #include <map>
3 using namespace std ;
4 
5 typedef HANDLE (WINAPI *_OPENTHREAD)(DWORD, BOOL, DWORD);//动态调用Dll

 

1 multimap<DWORD, HANDLE> m_ProcessThread;//定义multimap

 

 1 //挂起进程
 2 void CXTDlg::OnProcessSuspend() 
 3 {
 4     //获取当前选中进程(ListCtrl)
 5     POSITION pos = m_ctrlProcessList.GetFirstSelectedItemPosition();
 6     int nIndex = m_ctrlProcessList.GetNextSelectedItem(pos);
 7 
 8     //读取进程ID
 9     DWORD dwProcessID = 0;
10     char szTmp[MAX_PATH] = {0};
11     m_ctrlProcessList.GetItemText(nIndex, 1, szTmp, MAX_PATH);
12     dwProcessID = strtoul(szTmp, NULL, 10);
13 
14     //获取指定进程的线程快照
15     HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwProcessID); 
16     if (hThreadSnap == INVALID_HANDLE_VALUE) 
17     {
18         return; 
19     }
20     
21     //遍历线程
22     THREADENTRY32 te32 = {0};
23     te32.dwSize = sizeof(THREADENTRY32); 
24     if (Thread32First(hThreadSnap, &te32)) 
25     { 
26         do 
27         { 
28             if (te32.th32OwnerProcessID == dwProcessID)
29             {
30                 //挂起线程
31                 HMODULE hDll = LoadLibrary("Kernel32.dll");
32                 _OPENTHREAD OpenThread = (_OPENTHREAD)GetProcAddress(hDll, "OpenThread");
33                 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
34                 SuspendThread(hThread);
35 
36                 //存储线程句柄
37                 m_ProcessThread.insert(multimap<DWORD, HANDLE>::value_type(dwProcessID, hThread));
38             }
39         } 
40         while (Thread32Next(hThreadSnap, &te32)); 
41     } 
42     else 
43     {
44         return;;
45     }
46     
47     //释放快照资源
48     CloseHandle (hThreadSnap); 
49 }
50 
51 
52 //恢复进程
53 void CXTDlg::OnProcessResume() 
54 {
55     //获取当前选中进程
56     POSITION pos = m_ctrlProcessList.GetFirstSelectedItemPosition();
57     int nIndex = m_ctrlProcessList.GetNextSelectedItem(pos);
58 
59     //读取进程ID
60     DWORD dwProcessID = 0;
61     char szTmp[MAX_PATH] = {0};
62     m_ctrlProcessList.GetItemText(nIndex, 1, szTmp, MAX_PATH);
63     dwProcessID = strtoul(szTmp, NULL, 10);
64 
65     //遍历multimap,获取线程句柄
66     typedef multimap<DWORD, HANDLE>::iterator multiMapItor;
67     pair<multiMapItor,multiMapItor> doubleIter = m_ProcessThread.equal_range(dwProcessID);
68     while(doubleIter.first != doubleIter.second)
69     {
70         if (doubleIter.first->second)
71         {
72             //恢复线程
73             ResumeThread(doubleIter.first->second);
74             //释放线程资源
75             CloseHandle(doubleIter.first->second);
76             doubleIter.first->second = NULL;
77         }
78         ++doubleIter.first;
79     }    
80 }

 

posted @ 2014-08-02 22:06  luzhiyuan  阅读(2335)  评论(0编辑  收藏  举报