MCPcopy Create free account
hub / github.com/dobin/RedEdr / ReadMemoryAsWString

Function ReadMemoryAsWString

RedEdrShared/process_query.cpp:171–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169
170
171std::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

Callers 1

ProcessEnumerateModulesFunction · 0.85

Calls 2

dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected