if (strcmp(moduleName, Client::GetClientFilename().c_str()) == 0) {
return true;
}
return false;
}
esse código ele funciona bem, ele me retorna o nome do executavel, porém quero usar ele em c++ puro ae eu tenho
bool Client::IsCallerAuthorized() {
// Get the return address of the current function call
void* returnAddress = _ReturnAddress();
code
// Retrieve the module handle for the caller module
HMODULE callerModule = NULL;
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCSTR>(returnAddress),
&callerModule)) {
return false;
}
// Prepare a std::string to receive the module path
std::string modulePath;
modulePath.resize(MAX_PATH);
// Get the module file name
DWORD pathLength = GetModuleFileNameA(callerModule, &modulePath[0], MAX_PATH);
if (pathLength == 0) {
// Failed to get the module file name
return false;
}
// Resize the string to the actual length of the path
modulePath.resize(pathLength);
std::string moduleName = modulePath;
// Find the position of the last backslash
size_t lastBackslash = modulePath.find_last_of('\\');
if (lastBackslash != std::string::npos) {
// Get the substring after the last backslash
moduleName = modulePath.substr(lastBackslash + 1);
}
// Compare the module name with the client filename
if (moduleName == Client::GetClientFilename()) {
return true;
}
return false;
Assistant
}
Quando eu converto em c++ ele está printando a dll
It seems you're encountering an issue where your C++ version of the IsCallerAuthorized() function is returning the name of a DLL instead of the executable (EXE) file. The root of this problem lies in how you're casting the returnAddress parameter in the GetModuleHandleExA function call.
In your C++ code, you have:
cpp
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCSTR>(returnAddress),
&callerModule)) {
return false;
}
Here, you're casting returnAddress to LPCSTR, which is a pointer to a const char. However, when using the GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS flag, the second parameter of GetModuleHandleExA should be an address (LPCVOID), not a string (LPCSTR).
The incorrect cast to LPCSTR causes the function to misinterpret the returnAddress as a module name string rather than an address within a module. This leads to unexpected behavior, such as retrieving the wrong module (e.g., a DLL instead of the EXE).
Solution:
To fix this issue, you should cast returnAddress to LPCVOID (a pointer to a const void), which correctly represents an address in memory. Here's the corrected code:
cpp
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCVOID>(returnAddress),
&callerModule)) {
return false;
}
By making this change, GetModuleHandleExA will correctly interpret the returnAddress as an address within the module, and it will return the handle to the correct module—the executable file.
Here's the corrected function:
cpp
bool Client::IsCallerAuthorized() {
// Get the return address of the current function call
void* returnAddress = _ReturnAddress();
// Retrieve the module handle for the caller module
HMODULE callerModule = NULL;
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCVOID>(returnAddress),
&callerModule)) {
return false;
}
// Prepare a std::string to receive the module path
std::string modulePath;
modulePath.resize(MAX_PATH);
// Get the module file name
DWORD pathLength = GetModuleFileNameA(callerModule, &modulePath[0], MAX_PATH);
if (pathLength == 0) {
// Failed to get the module file name
return false;
}
// Resize the string to the actual length of the path
modulePath.resize(pathLength);
std::string moduleName = modulePath;
// Find the position of the last backslash
size_t lastBackslash = modulePath.find_last_of('\\');
if (lastBackslash != std::string::npos) {
// Get the substring after the last backslash
moduleName = modulePath.substr(lastBackslash + 1);
}
// Compare the module name with the client filename
if (moduleName == Client::GetClientFilename()) {
return true;
}
return false;
}
Explanation:
Casting to LPCVOID: Changing the cast from reinterpret_cast<LPCSTR>(returnAddress) to reinterpret_cast<LPCVOID>(returnAddress) ensures that GetModuleHandleExA receives the correct type of parameter—an address within the module.
Proper Parameter Usage: When GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS is specified, the function expects the second parameter to be an address, not a module name. Passing the correct parameter type ensures the function operates as intended.
Note that LPCSTR is used for module names, but when the flag GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS is specified, the lpModuleName parameter is actually treated as an LPCVOID address.
Understanding Flags:
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS: Indicates that the function should retrieve the module handle for the module that contains the specified memory address.
Conclusion:
By correcting the type of the returnAddress parameter, your function should now correctly retrieve the module handle of the executable instead of a DLL, and your IsCallerAuthorized() function will work as intended.
Updated Code with Fix:
cpp
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCVOID>(returnAddress),
&callerModule)) {
return false;
}
Change the cast of returnAddress in your call to GetModuleHandleExA from reinterpret_cast<LPCSTR>(returnAddress) to reinterpret_cast<LPCVOID>(returnAddress), so it treats the address correctly.