joh*_*doe 5 c++ windows unicode g++ cl
我正在 C++ 中开发一个程序,我试图在 Windows 中使用 WriteProcessMemory() 函数。为此,我需要一个获取目标进程 ID 的函数。我可以使用以下功能做到这一点:
#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;
}
Run Code Online (Sandbox Code Playgroud)
如果我将字符集设置为 unicode,我可以使用 Visual Studio 进行编译,但是当我尝试使用 g++ 时,出现转换错误:
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)) {
^
Run Code Online (Sandbox Code Playgroud)
我可以使用参数使用 cl 进行编译:
cl /EHsc /D UNICODE write.cpp
Run Code Online (Sandbox Code Playgroud)
这/D UNICODE与进入 Visual Studio > rmb on project > properties 并设置Character Set为Use Unicode Character Set.
有没有像 cl 一样在 g++ 中强制使用 unicode 的选项?