converta o código em c++ puro bool Client::IsCallerAuthorized() { void* returnAddress =…
- User
converta o código em c++ puro
bool Client::IsCallerAuthorized() { void* returnAddress = _ReturnAddress(); std::string path = GetDirectoryFilename(); std::string client = GetClientFilename();
HMODULE callerModule = NULL; if (!GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)returnAddress, &callerModule)) { return false; } char modulePath[MAX_PATH]; if (!GetModuleFileNameA(callerModule, modulePath, MAX_PATH)) { return false; } const char* moduleName = modulePath; const char* lastBackslash = strrchr(modulePath, '\\'); if (lastBackslash != NULL) { moduleName = lastBackslash + 1; } char moduleNameOriginal[MAX_PATH]; strcpy_s(moduleNameOriginal, moduleName); if (strcmp(moduleNameOriginal, Client::GetClientFilename().c_str()) == 0) { return true; } return false;}
- Assistant
Sure! Here's the converted code in pure C++ using modern C++ features like
std::string,std::vector, andstd::filesystem(available in C++17 and later). This code avoids C-style strings and functions, making it more idiomatic C++:#include <windows.h> #include <string> #include <vector> #include <filesystem> bool Client::IsCallerAuthorized() { // Get the return address of the caller void* returnAddress = _ReturnAddress(); // Get the caller module handle from the return address HMODULE callerModule = NULL; if (!GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCSTR>(returnAddress), &callerModule)) { return false; } // Get the module file path std::vector<char> modulePath(MAX_PATH); if (!GetModuleFileNameA(callerModule, modulePath.data(), static_cast<DWORD>(modulePath.size()))) { return false; } // Convert the module path to a std::string std::string modulePathStr(modulePath.data()); // Extract the module filename using std::filesystem std::filesystem::path moduleFilePath(modulePathStr); std::string moduleName = moduleFilePath.filename().string(); // Compare the module name with the client filename return moduleName == Client::GetClientFilename(); }Explanation:
- Avoided C-style strings and functions: Replaced C-style arrays and functions like
char[],strcpy_s, andstrcmpwithstd::stringandstd::vector<char>. - Used
std::vector<char>for buffer management: This ensures safe memory management when dealing with fixed-size character buffers required by Windows API functions. - Utilized
std::filesystemfor path manipulation: Thestd::filesystemlibrary provides a convenient and platform-independent way to handle file paths, making the code cleaner and more maintainable. - Simplified string comparisons: Used the
==operator to comparestd::stringobjects directly. - Removed unnecessary variables: Eliminated variables like
moduleNameOriginaland redundant code for copying and comparing strings.
Note: Make sure your project is set to use at least C++17 standard to utilize the
<filesystem>library. You may need to add the appropriate compiler flags (e.g.,/std:c++17for MSVC or-std=c++17for GCC/Clang).Dependencies:
-
Include the necessary headers:
#include <windows.h> #include <string> #include <vector> #include <filesystem> -
Ensure that you link against the required libraries when compiling on Windows (usually handled automatically by your development environment).
Additional Considerations:
- Exception Handling: The Windows API functions used (
GetModuleHandleExAandGetModuleFileNameA) return error codes. The code checks for failure and returnsfalsein such cases. - Platform Specificity: This code uses Windows-specific APIs and will only compile and run on Windows systems.
Usage:
- This function can be used within your
Clientclass to verify if the caller is authorized by checking if the module name of the caller matches the expected client filename. - Replace
Client::GetClientFilename()with your actual implementation that returns the expected client filename as astd::string.
- Avoided C-style strings and functions: Replaced C-style arrays and functions like