读书人

在windows VC++上得到父进程ID的方法

发布时间: 2012-10-31 14:37:32 作者: rapoo

在windows VC++下得到父进程ID的方法,求····
请教,我在从linux移植code到windows上时,有得到子进程和父进程id的函数:getpid(),getppid(),在VC++上子进程的id可以直接用getpid()得到,但是父进程没有对应getppid()的函数,我该如何实现呢,急等~~望各位大侠帮忙······

[解决办法]

C/C++ code
#include <windows.h>#include <tchar.h>#include <stdio.h>#include <psapi.h>void PrintModules( DWORD processID ){    HMODULE hMods[1024];    HANDLE hProcess;    DWORD cbNeeded;    unsigned int i;    // Print the process identifier.    printf( "\nProcess ID: %u\n", processID );    // Get a list of all the modules in this process.    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |                            PROCESS_VM_READ,                            FALSE, processID );    if (NULL == hProcess)        return;    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))    {        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )        {            TCHAR szModName[MAX_PATH];            // Get the full path to the module's file.            if ( GetModuleFileNameEx(hProcess, hMods[i], szModName,                                     sizeof(szModName)/sizeof(TCHAR)))            {                // Print the module name and handle value.                _tprintf(TEXT("\t%s (0x%08X)\n"),                         szModName, hMods[i]);            }        }    }    CloseHandle( hProcess );}void main( ){    // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;    unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )        return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name of the modules for each process.    for ( i = 0; i < cProcesses; i++ )        PrintModules( aProcesses[i] );}
[解决办法]
windows下子进程跟父进程之间的关系本事就是没什么关联的,不像Linux,windows也没有直接一个API可以得到父进程的ID,可以参考 http://www.cnblogs.com/login007/archive/2011/09/02/2163468.html
[解决办法]
前段时间刚好用到

C/C++ code
        DWORD GetParentProcessID(DWORD dwProcessId)    {        LONG                        status;        DWORD                        dwParentPID = (DWORD)-1;        HANDLE                        hProcess;        PROCESS_BASIC_INFORMATION    pbi;        PROCNTQSIP NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(              GetModuleHandle(L"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;            }
[解决办法]
探讨
3K~~我也用你这种方法试了,但是我得到的父进程的ID好像不是我需要的Id,比如我产生的可执行文件为mytool,如果我在编译器VC++中运行mytool,则它的父进程id显示的是编译器的id,如果是直接双击运行mytool,则父进程id显示是explorer.exe的id?
但是在linux下,我用getpid()和getppid()得到的id和它的父进程id分别是mytool和/tools/cad/cds/ies/8.2/IUS82/tools/systemc/bin/gdb64 -q -fullname mytool的id,所以linux和windows下得到的感觉不一样?能帮我解决一下吗? 搞不懂啊~~~~

读书人网 >C语言

热点推荐