简体   繁体   English

'const char*' 和 'WCHAR*'

[英]'const char*' and 'WCHAR*'

It's my first time making an external gamehacking application, and I'm having tourble with something.这是我第一次制作外部游戏黑客应用程序,我遇到了一些问题。

My issue: Cannot convert 'const char*' to 'WCHAR*' in argument passing.我的问题:无法在参数传递中将 'const char*' 转换为 'WCHAR*'。

I already tried to change the character set to multibyte, but its still showning an error.我已经尝试将字符集更改为多字节,但它仍然显示错误。

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

HANDLE hProc = NULL;
DWORD pID;

bool attachProc(char* procName)
{
    PROCESSENTRY32 procEntry32;
    procEntry32.dwSize = sizeof(PROCESSENTRY32);
    
    auto hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcSnap == INVALID_HANDLE_VALUE) 
    {
        std::cout << "Snapshot failed :((" << std::endl;
        return false;
    }

    while (Process32Next(hProcSnap, &procEntry32))
    {
        std::cout << procEntry32.szExeFile << std::endl;

        if (!strcmp(procName, procEntry32.szExeFile)) //ERROR RIGHT THERE
        {
            std::cout << "Process found: " << procEntry32.szExeFile << " Process id: " << procEntry32.th32ProcessID << std::endl;
            hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procEntry32.th32ProcessID);
            pID = procEntry32.th32ProcessID;

            if (hProc == NULL)
            {
                std::cout << "Process handling failed." << std::endl;
            }

            CloseHandle(hProcSnap);
            return true;
        }
    }
    std::cout << "Couldn't find " << procName << " in the process snapshot." << std::endl;
    CloseHandle(hProcSnap);
    return false;
}

template <class dataType>
void wpm(dataType valToWrite, DWORD addressToWrite) {
    WriteProcessMemory(hProc, (PVOID)addressToWrite, &valToWrite, sizeof(dataType), 0);
}

template <class dataType>
void rpm(DWORD addressToRead)
{
    dataType rpmBuffer;

    ReadProcessMemory(hProc, (PVOID)addressToRead, rpmBuffer, sizeof(dataType), 0);

    return rpmBuffer;
}

int main()
{

    DWORD memoryAddress;
    attachProc((char*)"hl2.exe");
}

Tysm, i hope u guys can help me out with this. Tysm,我希望你们能帮助我解决这个问题。

You should be getting an error.应该得到一个错误。 First, const char* could be compatible with const WCHAR* and not WCHAR* .首先, const char*可以与const WCHAR*而不是WCHAR*兼容。

Another thing, usually, in Windows, WCHAR characters stand for utf-16 characters, and ascii characters are not the same as utf-16 .另一件事,通常在 Windows 中, WCHAR 字符代表utf-16字符,而ascii字符与utf-16不同。 Windows has a built-in function converting from utf-8 or ascii to utf-16 , as follows: Windows 有一个内置的 function 从utf-8ascii转换为utf-16 ,如下:

    const auto result = MultiByteToWideChar(
        CP_ACP,
        MB_ERR_INVALID_CHARS,
        yourStringHere,
        yourStringSizeHere
        yourTargetStringHere,
        yourTargetStringSizeHere
    );

And, the C-standard library has an equivalent implementation -而且,C 标准库有一个等效的实现 -

std::mbstowcs(wchar_t* dst, const char* src, std::size_t len);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM