c++pointerscharmemory-access

Can't convert char[9] to char*


I'm new to game hacking, and started by following a tutorial which gave me the source code. Once I understood what it does, I tried to compile, and 3 identical errors came up:

Can't convert char[9] to char* (Error n. C2664)

The errors are referred to the variables ProcessName and ModuleName.

Even if I know good fundamentals of C++, I've always had difficulties with pointers.

The functions interested in the errors are these 2:

bool AttachProcess(char *ProcessName) {
    HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    PROCESSENTRY32 procEntry;
    procEntry.dwSize = sizeof(procEntry);
    
    const WCHAR* procNameChar;
    int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
    procNameChar = new WCHAR[nChars];
    MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
    
    do
        if (!wcscmp(procEntry.szExeFile, procNameChar))
        {
            this->dwPID = procEntry.th32ProcessID;
            CloseHandle(hPID);
            this->hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, this->dwPID);
            return true;
        }
    while (Process32Next(hPID, &procEntry));
    
    CloseHandle(hPID);
    return false;
}
    
MODULEENTRY32 GetModule(char* ModuleName)
{
    HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
    MODULEENTRY32 mEntry;
    mEntry.dwSize = sizeof(mEntry);
    
    const WCHAR* modNameChar;
    int nChars = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
    modNameChar = new WCHAR[nChars];
    MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, (LPWSTR)modNameChar, nChars);
    
    do
        if (!wcscmp(mEntry.szModule, modNameChar))
        {
            CloseHandle(hModule);
            return mEntry;
        }
    while (Module32Next(hModule, &mEntry));
    
    CloseHandle(hModule);
    mEntry.modBaseAddr = 0x0;
    return mEntry;
}

I recall those functions here:

MemoryManager()
{
    this->hProcess = NULL;
    this->dwPID = NULL;
    try {
        if (!AttachProcess("csgo.exe")) throw 1;
        this->ClientDLL = GetModule("client.dll");
        this->EngineDLL = GetModule("engine.dll");
        //and so on

Solution

  • The error occurred because you attempted to pass a string literal (a const char[]) to a function that expects a pointer to non-const char array (char *).

    Because you never modify the contents of ProcessName you should change

    bool AttachProcess(char *ProcessName)
    

    to

    //                 VVVVV
    bool AttachProcess(const char *ProcessName)