DWORD getModuleAddress(DWORD processID, const char* moduleName) { DWORDPTR…
- 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
- 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_tcsicmpfunction is from the family of_tcsfunctions which are meant to be compatible with either narrow or wide characters, depending on whether the_UNICODEmacro is defined.In your code, it looks like there might be a mixup between narrow and wide character types. Let's assume that
_UNICODEis defined;_tcsicmpwould then refer to_wcsicmp, which expects wide strings (wchar_t *). However, your code is creating astd::string(which uses narrow characters) and then converting the characters fromchartowchar_tin 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* moduleNametoconst TCHAR* moduleNameto 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
TCHARarray 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 explicitcharorwchar_ttypes and their corresponding functions (stricmpor_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
moduleNamematches the encoding expected by_tcsicmp(narrow or wide), as determined by whether_UNICODEis defined. If you're sure that you want to use narrow characters, you would replace_tcsicmpwith_stricmpand changeTCHAR*toconst 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_UNICODEis defined. This can be done usingMultiByteToWideCharfor conversion before the comparison if necessary. - Changed
- 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
- Assistant
The code you are using has a macro definition that switches between
wcscmpand_stricmpdepending on whetherUNICODEis defined. The issue is that this won't work correctly ifmoduleNameis a narrow string (i.e.,const char*) andUNICODEis defined becausewcscmpexpects 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
_tcsicmpand 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*tostd::wstringbefore the comparison in the wide character case. Notice that the comparison is done inside the#ifdef UNICODEblock, which ensures that the correct string types and comparison functions are used depending on whetherUNICODEis defined.For the narrow string comparison, I switched to using
strcmp, which is the standard narrow string comparison function. This is because the fieldModuleEntry32.szModulewill contain narrow strings ifUNICODEis not defined.Please note that the
std::wstring moduleNameWideStringconversion assumes thatmoduleNameuses 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 UNICODEsection altogether and just use the narrow string functions. - User
can i just fix it another way
- 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 likewcscmp.You can simplify the
_tcsicmpmacro to always use_stricmpand then you don't have to worry about whetherUNICODEis 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
_tcsicmpmacro entirely and replaced it with_stricmp, which is designed for comparing null-terminated strings ofchartype. - 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 ModuleEntry32to ensure it's zero-initialized to be safe.
By sticking to narrow strings, the
MODULEENTRY32structure will naturally use theszModulefield as a narrow string (char array) whenUNICODEis 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
UNICODEis defined, as I demonstrated in the previous response. - I removed the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.