0

I know this has likely been addressed before, but I am struggling to solve this issue and it is driving me nuts.

I have a "target process name" static string defined (as ANSI) which I am later using with a "find_process" function later in my code - specifically the comparison function "_stricmp". While I don't define that the program should be compiled (at least I dont think I do) as UNICODE, it seems that PROCESSENTRY32 is opting for PROCESSENTRY32W by default (UNICODE). And it throws the error below for "_stricmp" as PROCESSENTRY32 is UNICODE and my static string is ANSI. (see error image below)

But when I change the "target process name" to WCHAR* and use "wcscmp" for debugging string comparison in the "find_process" function, all of a sudden PROCESSENTRY32 doesn't seem to be UNICODE anymore and it fails saying PROCESSENTRY32 is CHAR[240] and not WCHAR*. Not sure what is happening.

Any ideas how I can address this issue? I know it compiles fine with a C compiler, however trying to address the issue for the C++ compiler.

#include <Windows.h>    
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <wincrypt.h>
#include <winuser.h>
#include <string>
#include <cstring>
#include <strsafe.h> 
#pragma comment (lib, "crypt32.lib")
#pragma comment (lib, "advapi32")
#include <psapi.h>
#define DEFAULT_BUFLEN 1024
#pragma comment(lib, "user32.lib")
#include <memoryapi.h>
#include <tlhelp32.h>
#include <stdlib.h>
#include <wchar.h>
#include <exception> 
// const string definition as ANSI
#define TARGET_PROCESS_NAME "notepad.exe"

"find process" function:

DWORD find_process(const char* process) {

    PROCESSENTRY32 process_entry;
    process_entry.dwSize = sizeof(PROCESSENTRY32);
    DWORD dwProcessId;     

    //get the list of processes
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    //check processes to find TARGET_PROCESS_NAME
    if (Process32First(snapshot, &process_entry) == TRUE) {

        while (Process32Next(snapshot, &process_entry) == TRUE) {
            // this is where things fail for UNICODE / ANSI
            if (_stricmp(process_entry.szExeFile, process) == 0) { //process is the target process name declared in headers // changing strcmp to stricmp
            //if (wcscmp(process_entry.szExeFile, pTmp) == 0) { //process is the target process name declared in headers // changing strcmp to stricmp

                CloseHandle(snapshot);
               
                dwProcessId = process_entry.th32ProcessID;
                char buf1[10];
                _itoa_s(dwProcessId, buf1, 10);              

                return process_entry.th32ProcessID;
            }
        }
    }

    CloseHandle(snapshot);
    return 0;

}

Errors:

enter image description here

Theepicpowner
  • 17
  • 1
  • 6
  • 1
    Just about all WINAPI functions comes in two variants: "ASCII" and "Wide". Your program is built to use the wide version of the function, which uses the wide character `wchar_t` (or the Windows alias `WCHAR`). This is rather well-documented, and any decent book, tutorial or class should have told you about that, and how to use the generic Windows types and functions and macros to work with whatever is used. – Some programmer dude Apr 11 '22 at 09:45
  • 1
    Is this useful? https://stackoverflow.com/a/12637971/10035556 – Mayur Apr 11 '22 at 09:47
  • As for your problem, you first need to convert all your strings and characters to using the correct variant, and then find a string comparison function that knows about the current setting for characters. – Some programmer dude Apr 11 '22 at 09:47

0 Answers0