I asked a question recently about using unicode and the problems that arose here:
argument of type "WCHAR *" is incompatible with parameter of type "LPCSTR" in c++
In solving one problem, I encountered another one that has taken me now a literal rabbit hole of differences between ansi and unicode. I have learnt a lot but I still cannot seem to solve this problem after almost a week of trying.
I looked at How do you properly use WideCharToMultiByte as I think this is what I need to convert the find_pid from unicode wchar back to char otherwise I get erros but to no avail.
All help little or small is appreciated as this is driving me mad.
Here is what I am trying to solve:
DWORD find_pid(const wchar_t* procname) {
// Dynamically resolve some functions
HMODULE kernel32 = GetModuleHandleA("Kernel32.dll");
using CreateToolhelp32SnapshotPrototype = HANDLE(WINAPI *)(DWORD, DWORD);
CreateToolhelp32SnapshotPrototype CreateToolhelp32Snapshot = (CreateToolhelp32SnapshotPrototype)GetProcAddress(kernel32, "CreateToolhelp32Snapshot");
using Process32FirstPrototype = BOOL(WINAPI *)(HANDLE, LPPROCESSENTRY32);
Process32FirstPrototype Process32First = (Process32FirstPrototype)GetProcAddress(kernel32, "Process32First");
using Process32NextPrototype = BOOL(WINAPI *)(HANDLE, LPPROCESSENTRY32);
Process32NextPrototype Process32Next = (Process32NextPrototype)GetProcAddress(kernel32, "Process32Next");
// Init some important local variables
HANDLE hProcSnap;
PROCESSENTRY32 pe32;
DWORD pid = 0;
pe32.dwSize = sizeof(PROCESSENTRY32);
// Find the PID now by enumerating a snapshot of all the running processes
hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hProcSnap)
return 0;
if (!Process32First(hProcSnap, &pe32)) {
CloseHandle(hProcSnap);
return 0;
}
while (Process32Next(hProcSnap, &pe32)) {
if (lstrcmp(procname, pe32.szExeFile) == 0) {
pid = pe32.th32ProcessID;
break;
}
}
// Cleanup
CloseHandle(hProcSnap);
return pid;
}
//changing to std::wstring does not work, already tried
std::string parentProcess = "C:\\hello.exe"
DWORD pid = find_pid(parentProcess.c_str());