C++实例 获取进程所在文件夹
#include "windows.h" #include "tlhelp32.h" #include "iostream" #include "string.h" /* C++实例 获取进程所在文件夹 blogs to http://www.pythonschool.com/ QQ群:218030155 */ using namespace std; void GetProcessInfo(HANDLE hProcess, HANDLE hModule, PROCESSENTRY32* pinfo, MODULEENTRY32* minfo) { BOOL report; //是否遍历结束标志 char shortpath[MAX_PATH] = ""; //保存路径变量 report = Process32First(hProcess, pinfo); //获取第一个进程信息 while(report) { hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pinfo->th32ProcessID); //对进程ID构进模块信息 Module32First(hModule, minfo); //获取每一个模块信息 GetShortPathName(minfo->szExePath, shortpath, 256); //将查找到的路径赋值到变量shortpath if( strcmp(pinfo->szExeFile, "QQ.exe") == 0) cout << pinfo->szExeFile << " " << shortpath << endl; report = Process32Next(hProcess, pinfo); //继续查找下一个进程 } } int main(int argc, char* argv[]) { HANDLE hModule; //模块句柄 HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //初始化进程快照 PROCESSENTRY32* pinfo = new PROCESSENTRY32; //进程信息描述 pinfo->dwSize = sizeof(PROCESSENTRY32); //给定空间大小 MODULEENTRY32* minfo = new MODULEENTRY32; //模块信息描述 minfo->dwSize = sizeof(MODULEENTRY32); //给定空间大小 GetProcessInfo(hProcess, hModule, pinfo, minfo); //主体处理 CloseHandle(hProcess); //关闭进程句柄 CloseHandle(hModule); //关闭模块句柄 return 0; } /* int main(int argc, char* argv[]) { HANDLE hModule; //模块句柄 BOOL report; //是否遍历结束标志 char shortpath[MAX_PATH] = ""; //保存路径变量 HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //初始化进程快照 PROCESSENTRY32* pinfo = new PROCESSENTRY32; //进程信息描述 pinfo->dwSize = sizeof(PROCESSENTRY32); //给定空间大小 MODULEENTRY32* minfo = new MODULEENTRY32; //模块信息描述 minfo->dwSize = sizeof(MODULEENTRY32); //给定空间大小 report = Process32First(hProcess, pinfo); //获取第一个进程信息 while(report) { hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pinfo->th32ProcessID); //对进程ID构进模块信息 Module32First(hModule, minfo); //获取每一个模块信息 GetShortPathName(minfo->szExePath, shortpath, 256); //将查找到的路径赋值到变量shortpath if( strcmp(pinfo->szExeFile, "QQ.exe") == 0) cout << pinfo->szExeFile << " " << shortpath << endl; report = Process32Next(hProcess, pinfo); //继续查找下一个进程 } CloseHandle(hProcess); //关闭进程句柄 CloseHandle(hModule); //关闭模块句柄 return 0; } */
学习笔记转摘于:
丝酷网 http://www.pythonschool.com/