c++ - 如何在 C++ 中获取进程名称

标签 c++ windows winapi

如何在 Windows 中使用 C++ 从 PID 中获取进程名称?

最佳答案

我猜是 OpenProcess鉴于您的进程拥有必要的权限,函数应该会有所帮助。获得进程句柄后,您可以使用 GetModuleFileNameEx函数获取进程的完整路径(.exe文件的路径)。

#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "stdio.h"
#include "psapi.h"
// Important: Must include psapi.lib in additional dependencies section
// In VS2005... Project > Project Properties > Configuration Properties > Linker > Input > Additional Dependencies

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE Handle = OpenProcess(
        PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
        FALSE,
        8036 /* This is the PID, you can find one from windows task manager */
    );
    if (Handle) 
    {
        TCHAR Buffer[MAX_PATH];
        if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
        {
            // At this point, buffer contains the full path to the executable
        }
        else
        {
            // You better call GetLastError() here
        }
        CloseHandle(Handle);
    }
    return 0;
}

关于c++ - 如何在 C++ 中获取进程名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570174/

相关文章:

C# 通知表单

c - 如何通过插入的 USB 设备找到 USB 主机 Controller

c++ - 错误 C2512 : 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only

c++ - 为什么空表达式在 C/C++ 中是合法的?

C++ 电子邮件/SMTP

windows - Psexec 和 UAC 问题

c - c中的原始输入winapi,无法获取设备信息

c++ - 在 native C++ 中停靠控件

c++ - 跨网络发送数据最快的 C/C++ 技术?

c - 网络的机器代码是什么样的?