簡體   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