简体   繁体   English

如何使用 c++ 获取 windows 中某物的进程 ID

[英]How do I get the process ID of something in windows using c++

I am trying to get the procid of a process using the name of the process.我正在尝试使用进程名称获取进程的 procid。 The errors are talking about procEntry.szExeFile.错误是关于 procEntry.szExeFile。 However, I am getting the errors:但是,我收到以下错误:

[{
    "owner": "C/C++",
    "code": "167",
    "severity": 8,
    "message": "argument of type \"WCHAR *\" is incompatible with parameter of type \"const char *\"",
    "source": "C/C++",
    "startLineNumber": 17,
    "startColumn": 17,
    "endLineNumber": 17,
    "endColumn": 26
},{
    "owner": "C/C++",
    "code": "167",
    "severity": 8,
    "message": "argument of type \"WCHAR *\" is incompatible with parameter of type \"const char *\"",
    "source": "C/C++",
    "startLineNumber": 24,
    "startColumn": 21,
    "endLineNumber": 24,
    "endColumn": 30
}]

Is there another way to get the process ID?有没有其他方法可以获取进程 ID? I have tried reinstalling c++ libraries.我尝试重新安装 c++ 库。 I have also tried converting it but that didn't work either.我也尝试过转换它,但这也不起作用。 Here is the code I am using:这是我正在使用的代码:

#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <TlHelp32.h>
// Get process id from name
DWORD GetProcId(const char* procName)
{
    PROCESSENTRY32 procEntry;
    procEntry.dwSize = sizeof(procEntry);

    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (hSnap == INVALID_HANDLE_VALUE)
        return 0;

    Process32First(hSnap, &procEntry);
    if (!strcmp(procEntry.szExeFile, procName))
    {
        CloseHandle(hSnap);
        return procEntry.th32ProcessID;
    }
    while (Process32Next(hSnap, &procEntry))
    {
        if (!strcmp(procEntry.szExeFile, procName))
        {
            CloseHandle(hSnap);
            return procEntry.th32ProcessID;
        }
    }
    CloseHandle(hSnap);
    return 0;
}

int main()
{
    
    
    // Get process id from name
    DWORD procId = GetProcId("csgo.exe");
    if (!procId)
    {
        std::cout << "Could not find process" << std::endl;
        return 0;
    }
    // wait for key press
    std::cout << "Press any key to continue" << std::endl;
    std::getchar();
    return 0;


}

You are using TCHAR -based macros, and you are compiling your project with UNICODE defined, so those macros map to the wchar_t -based APIs (ie, PROCESSENTRY32 -> PROCESSENTRY32W , Process32First -> Process32FirstW , etc).您正在使用基于TCHAR的宏,并且您正在编译您的项目并定义了UNICODE ,因此这些宏 map 到基于wchar_t的 API(即PROCESSENTRY32 -> PROCESSENTRY32WProcess32First -> Process32FirstW )。 As such, the PROCESSENTRY32::szExeFile field is a wchar_t[] array.因此, PROCESSENTRY32::szExeFile字段是一个wchar_t[]数组。 But, strcmp() expects char* strings instead, hence the compiler error you are getting.但是, strcmp()需要char*字符串,因此会出现编译器错误。

Since your function takes in a const char* as input, you shouldn't be using the TCHAR APIs at all.由于您的 function 接受const char*作为输入,因此您根本不应该使用TCHAR API。 Use the A nsi-based APIs instead, eg:请改用基于A nsi 的 API,例如:

#include <iostream>
#include <string>
#include <cstring>
#include <windows.h>
#include <TlHelp32.h>

// Get process id from name
DWORD GetProcId(const char* procName)
{
    PROCESSENTRY32A procEntry;
    procEntry.dwSize = sizeof(procEntry);

    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (hSnap == INVALID_HANDLE_VALUE)
        return 0;

    if (Process32FirstA(hSnap, &procEntry))
    {
        do
        {
            if (std::strcmp(procEntry.szExeFile, procName) == 0)
            {
                CloseHandle(hSnap);
                return procEntry.th32ProcessID;
            }
        }
        while (Process32NextA(hSnap, &procEntry));
    }

    CloseHandle(hSnap);
    return 0;
}

int main()
{
    // Get process id from name
    DWORD procId = GetProcId("csgo.exe");
    if (!procId)
    {
        std::cout << "Could not find process" << std::endl;
        return 0;
    }

    // wait for key press
    std::cout << "Press any key to continue" << std::endl;
    std::getchar();

    return 0;
}

Otherwise, change the code to use wchar_t strings and W ide-based APIs instead, eg:否则,将代码更改为使用wchar_t字符串和基于W ide 的 API,例如:

#include <iostream>
#include <string>
#include <cwchar>
#include <windows.h>
#include <TlHelp32.h>

// Get process id from name
DWORD GetProcId(const wchar_t* procName)
{
    PROCESSENTRY32W procEntry;
    procEntry.dwSize = sizeof(procEntry);

    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (hSnap == INVALID_HANDLE_VALUE)
        return 0;

    if (Process32FirstW(hSnap, &procEntry))
    {
        do
        {
            if (std::wcscmp(procEntry.szExeFile, procName) == 0)
            {
                CloseHandle(hSnap);
                return procEntry.th32ProcessID;
            }
        }
        while (Process32NextW(hSnap, &procEntry));
    }

    CloseHandle(hSnap);
    return 0;
}

int main()
{
    // Get process id from name
    DWORD procId = GetProcId(L"csgo.exe");
    if (!procId)
    {
        std::cout << "Could not find process" << std::endl;
        return 0;
    }

    // wait for key press
    std::cout << "Press any key to continue" << std::endl;
    std::getchar();

    return 0;
}

Stay away from TCHAR and its related macros if you can help it.如果可以提供帮助,请远离TCHAR及其相关宏。 They are a remnant from the Win9x/ME days when people were migrating code from ANSI to UNICODE.它们是 Win9x/ME 时代的遗留物,当时人们将代码从 ANSI 迁移到 UNICODE。 They really don't have a place in modern coding, as everything is now Unicode.它们在现代编码中确实没有一席之地,因为现在一切都是 Unicode。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 如何从 DLL(windows)的文件名中获取进程 ID? - C++ How to get process ID from filename of DLL(windows)? 如何使用c ++杀死c ++进程并让其在hte杀死之前执行某些操作 - how to kill c++ process using c++ and let it do something before hte kill 如何通过使用C / C ++在Linux中提供进程ID来获取父进程ID? - How to get the Parent Process ID by giving a Process ID in Linux using C/C++? 如何获取每秒接收和发送的网络字节数对于C ++ for Windows中的进程(输入:进程ID或句柄)? - How to get Received & Sent Network Bytes per second For a process (input: process id or handle) in C++ for Windows? 如何使用C / C ++系统调用获取Linux中进程的堆内存的当前大小? - How do I get the current size of the heap memory of a process in Linux using C/C++ system calls? C ++如何获取当前的控制台主机进程 - c++ how do i get the current console conhost process C ++无法获取进程ID(窗口) - C++ Can't get process id (windows) 如何从C ++的Windows XP上的管理员进程获取有关已登录用户的语言环境信息? - How do I get locale information about logged-in user from an administrator process on Windows XP in c++? 我是否需要做一些特殊的事情才能让我的C ++程序使用gcc进行编译? - Do I need to do something special to get my C++ program to compile using gcc? 如果我有一个小型转储文件或异常结构,如何获得winqual使用的“存储桶ID”? (Windows C ++) - How do I get the “bucket id” that winqual uses if I have a minidump file or exception structure? (Windows c++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM