Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to "use unicode character set" in g++?

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?

like image 326
jumpindonuts Avatar asked May 19 '19 18:05

jumpindonuts


People also ask

What is character set in Unicode?

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.

What is an example of a Unicode character?

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).


2 Answers

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
like image 160
Sandburg Avatar answered Oct 22 '22 10:10

Sandburg


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.

like image 2
rubenvb Avatar answered Oct 22 '22 11:10

rubenvb