5

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?

4
  • 3
    Yes, g++ has -DUNICODE parameter. Or you can #define UNICODE directly in your code. (and I would remove the #pragma once, it's not a header) Commented May 19, 2019 at 18:38
  • hey, thanks a lot. i used #define UNICODE prior and it didn't work, so i took it out, but i tried -DUNICODE and it actually worked. thanks a lot. Commented May 19, 2019 at 18:45
  • 2
    You should use W stuff: PROCESSENTRY32W and so on. Also this function mya leak resources if returns at first return statement. Commented May 19, 2019 at 18:53
  • 3
    Use -municode switch (assuming you're using mingw-w64), as well as defining UNICODE it also links the right version of main and so on Commented May 20, 2019 at 1:57

2 Answers 2

3

cl (Microsoft C/C++ Compiler) and g++ (Gnu C++ Compiler) have a very close arguments syntax on certain parameter. The delta is more of the usual difference Dos / Shell (slash vs dash).

The equivalent of /DMY_IDENTIFIER (cl) is on g++:

-DMY_IDENTIFER 

Which means in your case: -DUNICODE

The complete compilation command line would have to look like:

g++ -DUNICODE -std=c++17 write.cpp 
Sign up to request clarification or add additional context in comments.

Comments

3

The correct answer here is to use the compiler switch -municode which defines everything you need, and links to the correct CRT files as to provide you with the proper definition of _wmain.

This is not available on ye olde MinGW, so you'll need a MinGW-w64 toolchain for that. Chances are you are already using that anyway. If that is not the case, you can download the installer from here.

1 Comment

You opended my eyes on something I really missed up related to compilers. Now, I will always look if a -moption exists for what I'm doing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.