i'm working on a program in c++ where i'm trying to use the WriteProcessMemory() function in windows. for that i need a function that gets the target process id. i'm able to do that using the following function:
#pragma once #include <Windows.h> #include <TlHelp32.h> #include <iostream> //get process id from executable name using tlhelp32snapshot DWORD GetProcID(wchar_t *exeName){ PROCESSENTRY32 procEntry = {0}; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (!hSnapshot) { return 0; } procEntry.dwSize = sizeof(procEntry); if (!Process32First(hSnapshot, &procEntry)) { return 0; } do { if (!wcscmp(procEntry.szExeFile, exeName)) { CloseHandle(hSnapshot); return procEntry.th32ProcessID; } } while (Process32Next(hSnapshot, &procEntry)); CloseHandle(hSnapshot); return 0; } //main function int main() { using namespace std; cout << "some output" << endl; return 0; } i'm able to compile using visual studio if i set the character set to unicode but when i try using g++ i get a conversion error:
g++ -std=c++17 write.cpp write.cpp:1:9: warning: #pragma once in main file #pragma once ^ write.cpp: In function 'DWORD GetProcID(wchar_t*)': write.cpp:21:43: error: cannot convert 'CHAR* {aka char*}' to 'const wchar_t*' for argument '1' to 'int wcscmp(const wchar_t*, const wchar_t*)' if (!wcscmp(procEntry.szExeFile, exeName)) { ^ write.cpp: In function 'MODULEENTRY32 GetModule(DWORD, wchar_t*)': write.cpp:40:46: error: cannot convert 'char*' to 'const wchar_t*' for argument '1' to 'int wcscmp(const wchar_t*, const wchar_t*)' if (!wcscmp(modEntry.szModule, moduleName)) { ^ i'm able to compile with cl using the arguments:
cl /EHsc /D UNICODE write.cpp here /D UNICODE is the same as going in visual studio > rmb on project > properties and seting Character Set to Use Unicode Character Set.
is there an option to force unicode in g++ like in cl?
-DUNICODEparameter. Or you can#define UNICODEdirectly in your code. (and I would remove the#pragma once, it's not a header)#define UNICODEprior and it didn't work, so i took it out, but i tried-DUNICODEand it actually worked. thanks a lot.Wstuff:PROCESSENTRY32Wand so on. Also this function mya leak resources if returns at first return statement.-municodeswitch (assuming you're using mingw-w64), as well as defining UNICODE it also links the right version of main and so on