Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

DWORD getModuleAddress(DWORD processID, const char* moduleName) { DWORDPTR…

  1. User

    DWORD getModuleAddress(DWORD processID, const char* moduleName) { DWORD_PTR dwModuleBaseAddress = 0; DWORD result = 0;

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
    if (hSnapshot != INVALID_HANDLE_VALUE)
    {
    	MODULEENTRY32 ModuleEntry32;
    	ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
    	if (Module32First(hSnapshot, &ModuleEntry32))
    	{
    		do {
    			std::string moduleNameString(moduleName);
    			std::string moduleNameWideString(moduleNameString.begin(), moduleNameString.end());
    			if (_tcsicmp(ModuleEntry32.szModule, moduleNameWideString.c_str()) == 0)
    			{
    				dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
    				result = dwModuleBaseAddress;
    				break;
    			}
    		} while (Module32Next(hSnapshot, &ModuleEntry32));
    	}
    	CloseHandle(hSnapshot);
    }
    return result;
    

    error Severity Code Description Project File Line Suppression State Details Error C2664 'int wcscmp(const wchar_t *,const wchar_t *)': cannot convert argument 2 from 'const _Elem *' to 'const wchar_t *' with [ _Elem=char ] Usermode C:\Users\nio\Desktop\SourceCode\Usermode\functions.cpp 27

  2. Assistant

    The error you're seeing (C2664) indicates a problem with the character type used in a comparison function. In C++, there are different versions of strings and their corresponding functions, based on the character type: narrow characters (char) and wide characters (wchar_t). The _tcsicmp function is from the family of _tcs functions which are meant to be compatible with either narrow or wide characters, depending on whether the _UNICODE macro is defined.

    In your code, it looks like there might be a mixup between narrow and wide character types. Let's assume that _UNICODE is defined; _tcsicmp would then refer to _wcsicmp, which expects wide strings (wchar_t *). However, your code is creating a std::string (which uses narrow characters) and then converting the characters from char to wchar_t in a non-standard way.

    Here's a revised version of your function that should compile without error and handle both narrow and wide character sets correctly:

    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    #include <string>
    
    DWORD getModuleAddress(DWORD processID, const TCHAR* moduleName)
    {
        DWORD_PTR dwModuleBaseAddress = 0;
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
        if (hSnapshot != INVALID_HANDLE_VALUE)
        {
            MODULEENTRY32 ModuleEntry32;
            ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
            if (Module32First(hSnapshot, &ModuleEntry32))
            {
                do {
                    if (_tcsicmp(ModuleEntry32.szModule, moduleName) == 0)
                    {
                        dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
                        break;
                    }
                } while (Module32Next(hSnapshot, &ModuleEntry32));
            }
            CloseHandle(hSnapshot);
        }
        return (DWORD)dwModuleBaseAddress;
    }
    

    Changes and considerations:

    • Changed const char* moduleName to const TCHAR* moduleName to keep consistent with the character type for _tcsicmp.
    • Removed the confusing conversion from narrow string (std::string) to what appeared to be intended as a wide string.
    • Now, the calling function must pass the module name as a TCHAR array or properly defined string literal. If your application exclusively uses narrow characters or wide characters, you can drop the generic text mappings (TCHAR, _tcsicmp, etc.) and use the explicit char or wchar_t types and their corresponding functions (stricmp or _wcsicmp).
    • CAST on return changed to a simple cast: (DWORD)dwModuleBaseAddress

    Please be aware that this function only works correctly if the character encoding of moduleName matches the encoding expected by _tcsicmp (narrow or wide), as determined by whether _UNICODE is defined. If you're sure that you want to use narrow characters, you would replace _tcsicmp with _stricmp and change TCHAR* to const char*.

    If you're in a situation where you can't modify the interface (for example, if you must accept a narrow char* string), you will need to convert it to wide characters if _UNICODE is defined. This can be done using MultiByteToWideChar for conversion before the comparison if necessary.

  3. User

    #include "includes.hpp"

    #include "functions.hpp"

    #ifdef UNICODE #define _tcsicmp wcscmp #else #define _tcsicmp _stricmp #endif

    DWORD getModuleAddress(DWORD processID, const char* moduleName) { DWORD_PTR dwModuleBaseAddress = 0; DWORD result = 0;

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
    if (hSnapshot != INVALID_HANDLE_VALUE)
    {
    	MODULEENTRY32 ModuleEntry32;
    	ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
    	if (Module32First(hSnapshot, &ModuleEntry32))
    	{
    		do {
    			std::string moduleNameString(moduleName);
    			std::string moduleNameWideString(moduleNameString.begin(), moduleNameString.end());
    			if (_tcsicmp(ModuleEntry32.szModule, moduleNameWideString.c_str()) == 0)
    			{
    				dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
    				result = dwModuleBaseAddress;
    				break;
    			}
    		} while (Module32Next(hSnapshot, &ModuleEntry32));
    	}
    	CloseHandle(hSnapshot);
    }
    return result;
    

    }

    Severity Code Description Project File Line Suppression State Details Error C2664 'int wcscmp(const wchar_t *,const wchar_t *)': cannot convert argument 2 from 'const _Elem *' to 'const wchar_t *' with [ _Elem=char ] Usermode C:\Users\nio\Desktop\SourceCode\Usermode\functions.cpp 27

  4. Assistant

    The code you are using has a macro definition that switches between wcscmp and _stricmp depending on whether UNICODE is defined. The issue is that this won't work correctly if moduleName is a narrow string (i.e., const char*) and UNICODE is defined because wcscmp expects wide strings (i.e., const wchar_t*).

    The macro definition is correct, but you have to make sure that the strings being compared are of the same type. To fix the issue, you need to change the way you are calling _tcsicmp and ensure that both strings have the same character type.

    Here's what you can do:

    If your application is supposed to be Unicode-aware:

    DWORD getModuleAddress(DWORD processID, const char* moduleName)
    {
        DWORD_PTR dwModuleBaseAddress = 0;
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
        if (hSnapshot != INVALID_HANDLE_VALUE)
        {
            MODULEENTRY32 ModuleEntry32;
            ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
            if (Module32First(hSnapshot, &ModuleEntry32))
            {
                // Convert moduleName to a wide string.
                std::wstring moduleNameWideString(moduleName, moduleName + std::strlen(moduleName));
    
                do {
    #ifdef UNICODE
                    // When UNICODE is defined, use the wide string comparison.
                    if (wcscmp(ModuleEntry32.szModule, moduleNameWideString.c_str()) == 0)
    #else
                    // When UNICODE is not defined, use the narrow string comparison.
                    if (strcmp(ModuleEntry32.szModule, moduleName) == 0)
    #endif
                    {
                        dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
                        break;
                    }
                } while (Module32Next(hSnapshot, &ModuleEntry32));
            }
            CloseHandle(hSnapshot);
        }
        return static_cast<DWORD>(dwModuleBaseAddress);
    }
    

    Here, I've added a conversion from const char* to std::wstring before the comparison in the wide character case. Notice that the comparison is done inside the #ifdef UNICODE block, which ensures that the correct string types and comparison functions are used depending on whether UNICODE is defined.

    For the narrow string comparison, I switched to using strcmp, which is the standard narrow string comparison function. This is because the field ModuleEntry32.szModule will contain narrow strings if UNICODE is not defined.

    Please note that the std::wstring moduleNameWideString conversion assumes that moduleName uses ASCII or an encoding compatible with the default C locale — this may not work correctly with strings that contain extended characters or are in a different encoding.

    If your application is only going to use narrow strings and you don't need to worry about Unicode, you can forego the conversion and just use strcmp. If you do this, remove the #ifdef UNICODE section altogether and just use the narrow string functions.

  5. User

    can i just fix it another way

  6. Assistant

    Certainly! If you would like to maintain usage of narrow strings (const char*) exclusively and your application does not need to be Unicode-aware, you can adjust the program to compare narrow strings only, and ensure you are not using wide-character functions like wcscmp.

    You can simplify the _tcsicmp macro to always use _stricmp and then you don't have to worry about whether UNICODE is defined or not. This way, you are explicitly deciding to only support narrow strings.

    Here is a revised version of your code that supports narrow strings:

    #include <windows.h>
    #include <tlhelp32.h>
    #include <string.h>
    #include <string>
    
    DWORD getModuleAddress(DWORD processID, const char* moduleName)
    {
        DWORD_PTR dwModuleBaseAddress = 0;
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
        if (hSnapshot != INVALID_HANDLE_VALUE)
        {
            MODULEENTRY32 ModuleEntry32 = {};
            ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
            
            if (Module32First(hSnapshot, &ModuleEntry32))
            {
                do {
                    // Compare using the narrow string comparison function.
                    if (_stricmp(ModuleEntry32.szModule, moduleName) == 0)
                    {
                        dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
                        break;
                    }
                } while (Module32Next(hSnapshot, &ModuleEntry32));
            }
            CloseHandle(hSnapshot);
        }
        return static_cast<DWORD>(dwModuleBaseAddress);
    }
    

    Here are the changes I've made:

    • I removed the _tcsicmp macro entirely and replaced it with _stricmp, which is designed for comparing null-terminated strings of char type.
    • Got rid of the string conversion from narrow to wide, since the comparison is only being done with narrow strings.
    • Fixed the initialization of MODULEENTRY32 ModuleEntry32 to ensure it's zero-initialized to be safe.

    By sticking to narrow strings, the MODULEENTRY32 structure will naturally use the szModule field as a narrow string (char array) when UNICODE is not defined, which aligns with the character type expected by _stricmp.

    If you do need to handle both narrow and wide strings due to different configurations or internationalization requirements, you should convert the narrow input string to wide when UNICODE is defined, as I demonstrated in the previous response.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.