| 169 | |
| 170 | |
| 171 | std::wstring ReadMemoryAsWString(HANDLE hProcess, LPCVOID remoteAddress, SIZE_T byteCount) { |
| 172 | if (!hProcess || !remoteAddress || byteCount == 0) { |
| 173 | throw std::invalid_argument("Invalid arguments provided to ReadMemoryAsWString"); |
| 174 | } |
| 175 | |
| 176 | // Allocate buffer to hold the memory content |
| 177 | std::vector<WCHAR> buffer(byteCount / sizeof(WCHAR) + 1, L'\0'); // Extra space for null-terminator |
| 178 | SIZE_T bytesRead = 0; |
| 179 | |
| 180 | // Read the memory from the target process |
| 181 | if (!ReadProcessMemory(hProcess, remoteAddress, buffer.data(), byteCount, &bytesRead)) { |
| 182 | throw std::runtime_error("ReadProcessMemory failed"); |
| 183 | } |
| 184 | |
| 185 | // Ensure the memory read is within bounds |
| 186 | SIZE_T wcharCount = bytesRead / sizeof(WCHAR); |
| 187 | if (wcharCount < buffer.size()) { |
| 188 | buffer[wcharCount] = L'\0'; // Null-terminate if not already |
| 189 | } |
| 190 | else { |
| 191 | buffer.back() = L'\0'; // Guarantee null-termination in case of truncation |
| 192 | } |
| 193 | |
| 194 | // Return as a wstring |
| 195 | return std::wstring(buffer.data()); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | // Enumerate all modules loaded in the process (DLL's), and their sections |
no test coverage detected