0
if ((WCHAR*)procName == procEntry32.szExeFile)

This is what I have, i've been told to use "strcmp" but it never seems to work. Could someone explain why? Or give me another solution?

bool attachProc(char* procName)
{
    PROCESSENTRY32 procEntry32;
    //_bstr_t procEntry32Char(procEntry32);

    // defining the size for populating it
    procEntry32.dwSize = sizeof(PROCESSENTRY32);

    //getting the list of all the processes 
    auto hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    std::cout << procEntry32.szExeFile;

    if (hProcSnap == INVALID_HANDLE_VALUE)
    {
        std::cout << "Failed To Take Snapshot Of Processes!" << std::endl;
        return false;
    }

    Process32First(hProcSnap, &procEntry32);

    while (Process32Next(hProcSnap, &procEntry32))
    {

        std::wcout << "Proc: " << procEntry32.szExeFile << std::endl;

        int correct = (procEntry32.szExeFile == (WCHAR*)procName);

        if (correct == 1)
        {
            //std::cout << "\nThe Program Was Found!\n";
        }
        else {
            //std::cout << "\nThe Program Was Not Found!\n";
        }

        std::cout << ((WCHAR*)procName == procEntry32.szExeFile);
        std::cout << "\nBoth:\n" << (WCHAR*)procName << "\n" << procEntry32.szExeFile << "\n\n";
        if ((WCHAR*)procName == procEntry32.szExeFile)
        {
            std::cout << "Found Process " << procEntry32.szExeFile << " with process ID: " << procEntry32.th32ProcessID << std::endl;

            hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procEntry32.th32ProcessID);

            pID = procEntry32.th32ProcessID;

            if (hProc == NULL)
            {
                std::cout << "Failed getting handle to process." << std::endl;
            }

            CloseHandle(hProcSnap);

            return true;
        }

    }

    std::cout << procEntry32.szExeFile;

    std::cout << "Couldn't find " << procName << " in the process snapshot!" << std::endl;
    CloseHandle(hProcSnap);

    return false;
}

Here's the rest of the function, I'm a beginner so sorry for bad practices!

  • First of all you can't convert a narrow-character string into a wide-character string by just casting. Secondly with `==` you compare two *pointers* and not the strings they might point to. I recommend you take a few steps back, [get a couple of decent C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learns the basics first (like why `==` would not work here). – Some programmer dude Jul 05 '21 at 22:21

1 Answers1

0

You appear to be using the wide-string Unicode version of the Win32 API functions (otherwise, why are you casting procName to WCHAR*?), but your procName is a narrow ANSI string instead. So, you need to either:

  1. change your code to use the narrow-string ANSI API functions, and then you can use strcmp() for comparisons.

  2. change your procName parameter to (const) wchar_t* instead of char*, and then you can use wcscmp() (or equivalent) for comparisons.

  3. same as #2, except if you can't change the type of the procName at compile-time, then convert your procName to wchar_t* at runtime using MultiByteToWideChar() (or equivalent).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770