C++ 获取父进程
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
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 |
// Win32Project2.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <wtypes.h> #include<tlhelp32.h> #include <psapi.h> #pragma comment(lib,"psapi.lib") using namespace std; #define ProcessBasicInformation 0 typedef struct { DWORD ExitStatus; DWORD PebBaseAddress; DWORD AffinityMask; DWORD BasePriority; ULONG UniqueProcessId; ULONG InheritedFromUniqueProcessId; } PROCESS_BASIC_INFORMATION; typedef LONG(__stdcall *PROCNTQSIP)(HANDLE, UINT, PVOID, ULONG, PULONG); // DWORD GetParentProcessID(DWORD dwProcessId); int _tmain(int argc, _TCHAR* argv[]) { // 输入文件名 HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 procentry;//=sizeof(PROCESSENTRY32); BOOL bFlag = Process32First(hSnapShot, &procentry); DWORD parentprocessid; while (bFlag) { if (_stricmp(procentry.szExeFile, "QQExternal.exe") == 0) //你的程序 { DWORD processid = procentry.th32ProcessID; //找到 cout << procentry.th32ProcessID << endl; cout << GetParentProcessID(procentry.th32ProcessID) << endl; parentprocessid = GetParentProcessID(procentry.th32ProcessID); } bFlag = Process32Next(hSnapShot, &procentry); } cout << parentprocessid << endl; HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, parentprocessid); char processName[MAX_PATH] = { 0 }; GetModuleBaseName(processHandle, 0, processName, MAX_PATH); char processFullPath[MAX_PATH] = { 0 }; GetModuleFileNameEx(processHandle, 0, processFullPath, MAX_PATH); cout << processName << endl; cout << processFullPath << endl; //cout << Environment::CommandLine<< endl; system("pause"); return 0; } DWORD GetParentProcessID(DWORD dwProcessId) { LONG status; DWORD dwParentPID = (DWORD)-1; HANDLE hProcess; PROCESS_BASIC_INFORMATION pbi; PROCNTQSIP NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress( GetModuleHandle("ntdll"), "NtQueryInformationProcess"); if (NULL == NtQueryInformationProcess) { return (DWORD)-1; } // Get process handle hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId); if (!hProcess) { return (DWORD)-1; } // Retrieve information status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, (PVOID)&pbi, sizeof(PROCESS_BASIC_INFORMATION), NULL ); // Copy parent Id on success if (!status) { dwParentPID = pbi.InheritedFromUniqueProcessId; } CloseHandle(hProcess); return dwParentPID; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1583