繁体   English   中英

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

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

我正在尝试使用进程名称获取进程的 procid。 错误是关于 procEntry.szExeFile。 但是,我收到以下错误:

[{
    "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
}]

有没有其他方法可以获取进程 ID? 我尝试重新安装 c++ 库。 我也尝试过转换它,但这也不起作用。 这是我正在使用的代码:

#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;


}

您正在使用基于TCHAR的宏,并且您正在编译您的项目并定义了UNICODE ,因此这些宏 map 到基于wchar_t的 API(即PROCESSENTRY32 -> PROCESSENTRY32WProcess32First -> Process32FirstW )。 因此, PROCESSENTRY32::szExeFile字段是一个wchar_t[]数组。 但是, strcmp()需要char*字符串,因此会出现编译器错误。

由于您的 function 接受const char*作为输入,因此您根本不应该使用TCHAR API。 请改用基于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;
}

否则,将代码更改为使用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;
}

如果可以提供帮助,请远离TCHAR及其相关宏。 它们是 Win9x/ME 时代的遗留物,当时人们将代码从 ANSI 迁移到 UNICODE。 它们在现代编码中确实没有一席之地,因为现在一切都是 Unicode。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM