从进程ID获取子进程名称

时间:2017-07-13 14:11:45

标签: c++ windows winapi

问题

是否可以从正在运行的进程子项中获取该名称? 有人可以给我一个例子吗?

我想创建一个显示所有正在运行的进程和子进程的程序。

我在Windows 10电台上这样做。 我用c ++语言写作。

到目前为止得到的是:

    // Runningprocess.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <psapi.h>
    // To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
    // and compile with -DPSAPI_VERSION=1

    void PrintProcessNameAndID( DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

        // Get a handle to the process.

        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );

        // Get the process name.

        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;

            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }

        // Print the process name and identifier.

        _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

        // Release the handle to the process.

        CloseHandle( hProcess );
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
            // Get the list of process identifiers.

            DWORD aProcesses[1024], cbNeeded, cProcesses;
            unsigned int i;

            if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            {
                return 1;
            }


            // Calculate how many process identifiers were returned.

            cProcesses = cbNeeded / sizeof(DWORD);

            // Print the name and process identifier for each process.

            for ( i = 0; i < cProcesses; i++ )
            {
                if( aProcesses[i] != 0 )
                {
                    PrintProcessNameAndID( aProcesses[i] );
                    //here needs to come a print funtion for the child processes of "aProcesses[i]"
                }
            }

        return 0;
    }

0 个答案:

没有答案
相关问题