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: