使用给定的过程ID获取过程描述

发布于 2025-01-30 11:44:54 字数 1454 浏览 2 评论 0原文

我有一个程序,可以用Toolhelp API列举所有进程。使用我的Sysinternals Process Explorer,我也可以看到所有过程的描述。此描述来自可执行文件吗?我如何获得它的名字?

这是我目前列举流程的代码:

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <vector>
#include <system_error>
#include <memory>

using namespace std;

vector<PROCESSENTRY32W> getAllProcesses();

int main()
{
    for( PROCESSENTRY32W &pe : getAllProcesses() )
        wcout << pe.szExeFile << endl;
}

using XHANDLE = unique_ptr<void, decltype([]( HANDLE h ) { h && h != INVALID_HANDLE_VALUE && CloseHandle( h ); })>;

vector<PROCESSENTRY32W> getAllProcesses()
{
    auto throwSysErr = []() { throw system_error( (int)GetLastError(), system_category(), "error enumerating processes" ); };
    vector<PROCESSENTRY32W> processes;
    XHANDLE xhSnapshot( CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) );
    if( xhSnapshot.get() == INVALID_HANDLE_VALUE )
        throwSysErr();;
    PROCESSENTRY32W pe;
    pe.dwSize = sizeof pe;
    if( !Process32FirstW( xhSnapshot.get(), &pe ) )
        throwSysErr();
    for( ; ; )
    {
        processes.emplace_back( pe );
        pe.dwSize = sizeof pe;
        if( !Process32NextW( xhSnapshot.get(), &pe ) )
            if( GetLastError() == ERROR_NO_MORE_FILES )
                break;
            else
                throwSysErr();
    }
    return processes;
}

I've got a program that enumerates all processes with the Toolhelp API. With my Sysinternals Process Explorer I also can see a description of all processes. Is this description coming from the executable ? How do I get its name ?

That's my current code to enumerate the processes:

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <vector>
#include <system_error>
#include <memory>

using namespace std;

vector<PROCESSENTRY32W> getAllProcesses();

int main()
{
    for( PROCESSENTRY32W &pe : getAllProcesses() )
        wcout << pe.szExeFile << endl;
}

using XHANDLE = unique_ptr<void, decltype([]( HANDLE h ) { h && h != INVALID_HANDLE_VALUE && CloseHandle( h ); })>;

vector<PROCESSENTRY32W> getAllProcesses()
{
    auto throwSysErr = []() { throw system_error( (int)GetLastError(), system_category(), "error enumerating processes" ); };
    vector<PROCESSENTRY32W> processes;
    XHANDLE xhSnapshot( CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) );
    if( xhSnapshot.get() == INVALID_HANDLE_VALUE )
        throwSysErr();;
    PROCESSENTRY32W pe;
    pe.dwSize = sizeof pe;
    if( !Process32FirstW( xhSnapshot.get(), &pe ) )
        throwSysErr();
    for( ; ; )
    {
        processes.emplace_back( pe );
        pe.dwSize = sizeof pe;
        if( !Process32NextW( xhSnapshot.get(), &pe ) )
            if( GetLastError() == ERROR_NO_MORE_FILES )
                break;
            else
                throwSysErr();
    }
    return processes;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-02-06 11:44:54

@RemyLebeau的方式与代码实现的方式,该方法由 verqueryvaluea 文档样本。 AS

如果指定的过程是系统空闲过程(0x00000000),则
功能失败,最后一个错误代码为 error_invalid_parameter 。如果
指定的过程是系统过程或客户端之一
服务器运行时子系统(CSRSS)进程,此功能失败,并且
最后一个错误代码是 error_access_denied
限制阻止用户级代码打开它们。

int main()
{
    TCHAR szFile[MAX_PATH] = {};
    DWORD dwSize = MAX_PATH;

    for (PROCESSENTRY32W& pe : getAllProcesses())
    {
        wcout << pe.szExeFile << endl;

        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
            FALSE, pe.th32ProcessID);
        if (hProcess == NULL)
        {
            //ErrorExit(TEXT("OpenProcess"));
        }
        else
        {
            memset(szFile, 0, MAX_PATH);
            dwSize = MAX_PATH;
            QueryFullProcessImageName(hProcess,0, szFile,&dwSize);
            DWORD s = GetFileVersionInfoSize(szFile,NULL);
            if (s != 0)
            {
                LPVOID lpData = HeapAlloc(GetProcessHeap(), 0, s);
                GetFileVersionInfo(szFile,0,s, lpData);

                HRESULT hr;
                UINT cbTranslate;
                struct LANGANDCODEPAGE {
                    WORD wLanguage;
                    WORD wCodePage;
                } *lpTranslate;

                // Read the list of languages and code pages.

                VerQueryValue(lpData,
                    TEXT("\\VarFileInfo\\Translation"),
                    (LPVOID*)&lpTranslate,
                    &cbTranslate);

                // Read the file description for each language and code page.
                LPVOID lpBuffer;
                UINT dwBytes;
                for (int i = 0; i < (cbTranslate / sizeof(struct LANGANDCODEPAGE)); i++)
                {
                    TCHAR SubBlock[255] = {};
                    hr = StringCchPrintf(SubBlock, 50,
                        TEXT("\\StringFileInfo\\%04x%04x\\FileDescription"),
                        lpTranslate[i].wLanguage,
                        lpTranslate[i].wCodePage);
                    if (FAILED(hr))
                    {
                        // TODO: write error handler.
                    }

                    // Retrieve file description for language and code page "i". 
                    VerQueryValue(lpData,
                        SubBlock,
                        &lpBuffer,
                        &dwBytes);

                    wcout << (TCHAR*)(lpBuffer) << endl;
                }
                HeapFree(GetProcessHeap(), 0, lpData);
            }
            //GetProcessImageFileName(hProcess, szFile, dwSize);
        }

    }
}

@RemyLebeau 's way with code implement which is adapted from VerQueryValueA document sample. And as OpenProcess states,

If the specified process is the System Idle Process (0x00000000), the
function fails and the last error code is ERROR_INVALID_PARAMETER. If
the specified process is the System process or one of the Client
Server Run-Time Subsystem (CSRSS) processes, this function fails and
the last error code is ERROR_ACCESS_DENIED because their access
restrictions prevent user-level code from opening them.

int main()
{
    TCHAR szFile[MAX_PATH] = {};
    DWORD dwSize = MAX_PATH;

    for (PROCESSENTRY32W& pe : getAllProcesses())
    {
        wcout << pe.szExeFile << endl;

        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
            FALSE, pe.th32ProcessID);
        if (hProcess == NULL)
        {
            //ErrorExit(TEXT("OpenProcess"));
        }
        else
        {
            memset(szFile, 0, MAX_PATH);
            dwSize = MAX_PATH;
            QueryFullProcessImageName(hProcess,0, szFile,&dwSize);
            DWORD s = GetFileVersionInfoSize(szFile,NULL);
            if (s != 0)
            {
                LPVOID lpData = HeapAlloc(GetProcessHeap(), 0, s);
                GetFileVersionInfo(szFile,0,s, lpData);

                HRESULT hr;
                UINT cbTranslate;
                struct LANGANDCODEPAGE {
                    WORD wLanguage;
                    WORD wCodePage;
                } *lpTranslate;

                // Read the list of languages and code pages.

                VerQueryValue(lpData,
                    TEXT("\\VarFileInfo\\Translation"),
                    (LPVOID*)&lpTranslate,
                    &cbTranslate);

                // Read the file description for each language and code page.
                LPVOID lpBuffer;
                UINT dwBytes;
                for (int i = 0; i < (cbTranslate / sizeof(struct LANGANDCODEPAGE)); i++)
                {
                    TCHAR SubBlock[255] = {};
                    hr = StringCchPrintf(SubBlock, 50,
                        TEXT("\\StringFileInfo\\%04x%04x\\FileDescription"),
                        lpTranslate[i].wLanguage,
                        lpTranslate[i].wCodePage);
                    if (FAILED(hr))
                    {
                        // TODO: write error handler.
                    }

                    // Retrieve file description for language and code page "i". 
                    VerQueryValue(lpData,
                        SubBlock,
                        &lpBuffer,
                        &dwBytes);

                    wcout << (TCHAR*)(lpBuffer) << endl;
                }
                HeapFree(GetProcessHeap(), 0, lpData);
            }
            //GetProcessImageFileName(hProcess, szFile, dwSize);
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文