0

I am trying to get the procid of a process using the name of the process. The errors are talking about 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? I have tried reinstalling c++ libraries. 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;


}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
codenaem
  • 59
  • 8
  • Your problem is that you are passing a `WCHAR *` to `strcmp` when it is expecting a `char *`. There are Windows versions of `strcmp` that take a `TCHAR *`, which you probably should be using instead. Also your `GetProcId` function should be taking a `const TCHAR *` type. – jkb Aug 11 '22 at 18:31
  • Are you trying to get the ID of someone else's process, or of the process of your own program? The latter can be done much easier. – Seva Alekseyev Aug 11 '22 at 18:41
  • @jkb should I just use the windows version of strcmp (a version that takes tchar)and then it will work? – codenaem Aug 11 '22 at 18:43
  • @SevaAlekseyev I am getting the process ID of a program running on my computer ex: a game. – codenaem Aug 11 '22 at 18:44
  • You'll need to use `TCHAR/TEXT` throughout, as well as using the `TCHAR` versions of `strcmp`. That means also changing the parameter to the `GetProcId function to `const TCHAR *` and using `TEXT("csgo.exe")` when you call it. – jkb Aug 11 '22 at 18:48
  • You really should stay away from `TCHAR` in modern coding. – Remy Lebeau Aug 11 '22 at 18:58

1 Answers1

1

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). As such, the PROCESSENTRY32::szExeFile field is a wchar_t[] array. But, strcmp() expects char* strings instead, hence the compiler error you are getting.

Since your function takes in a const char* as input, you shouldn't be using the TCHAR APIs at all. Use the Ansi-based APIs instead, eg:

#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 Wide-based APIs instead, eg:

#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. They are a remnant from the Win9x/ME days when people were migrating code from ANSI to UNICODE. They really don't have a place in modern coding, as everything is now Unicode.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • There are no errors when compiling but the program closes right after launching. – codenaem Aug 11 '22 at 19:00
  • @aafr then you need to debug your code to find out what it is really doing at runtime. You are not making the program wait before exit if `GetProcId()` returns 0, so is `CreateToolhelp32Snapshot()` failing? Is the loop not finding a match? – Remy Lebeau Aug 11 '22 at 19:01