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?
Unicode is a universal character set, ie. a standard that defines, in one place, all the characters needed for writing the majority of living languages in use on computers. It aims to be, and to a large extent already is, a superset of all other character sets that have been encoded.
Unicode supports more than a million code points, which are written with a "U" followed by a plus sign and the number in hex; for example, the word "Hello" is written U+0048 U+0065 U+006C U+006C U+006F (see hex chart).
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With