VC++取得系统进程列表,并得到进程全路径
VC++取得系统进程列表,并得到进程全路径
制作的简单步骤如下: 1.提升程序的权限 enableDebugPriv(); - //提升程序的权限
- void CprocesstestDlg::enableDebugPriv()
- {
- HANDLE hToken;
- LUID sedebugnameValue;
- TOKEN_PRIVILEGES tkp;
-
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- {
- return;
- }
-
- if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
- {
- CloseHandle(hToken);
- return;
- }
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Luid = sedebugnameValue;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
- {
- CloseHandle(hToken);
- return;
- }
- }
复制代码2.取得系统全部进程。 - //取得指定进程名全路径
- void CprocesstestDlg::GetProcessFilePathList()
- {
- m_vecProcess.clear();
- //vector<CString> strPathList;
- //列举进程列表
- CString strProcessName = _T("");
- DWORD m_ProcessIndex;//存放所有进程的ID
- int dProIndex = 0;
- //列出所有进程
- HANDLE Snapshot;
- Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- //获得某一时刻系统的进程、堆(heap)、模块(module)或线程的快照信息
- PROCESSENTRY32 processListStr;
- processListStr.dwSize = sizeof(PROCESSENTRY32);
- BOOL return_value;
- return_value = Process32First(Snapshot, &processListStr);
- //获得系统进程链表中第一个进程的信息
- while (return_value)
- {
- strProcessName.Format("%s", processListStr.szExeFile);
- m_list.AddString(strProcessName);
-
- int memory = processListStr.cntThreads;
- m_ProcessIndex = processListStr.th32ProcessID;//save ID into array to teminate
- return_value = Process32Next(Snapshot, &processListStr);
- //
- ProcessStruct ps;
- ps.dwIndex = m_ProcessIndex;
- ps.strName = strProcessName;
- m_vecProcess.push_back(ps);
- }
- }
复制代码3.根据进程ID取得进程全路径。 - //通过进程ID获取完整路径
- CString CprocesstestDlg::GetProcessPathByPId(DWORD dwProcessId)
- {
- CString cstrPath = _T("");
- HANDLE hProcess = NULL;
- HMODULE hModule;
- DWORD need;
- DWORD nSize = 0;
- char szPath[MAX_PATH + 1] = { 0 };
- hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
- if (hProcess)
- {
- ::EnumProcessModules(hProcess, &hModule, sizeof(hModule), &need);
- ::GetModuleFileNameExA(hProcess, hModule, (LPSTR)szPath, sizeof(szPath));
- cstrPath.Format(_T("%s"), szPath);
- }
- return cstrPath;
- }
复制代码 有兴趣的可以下载源代码。
链接:https://pan.baidu.com/s/1yDoyo0m1XiKcZFq38OUD9Q 源码下载:
|