current_process_memory.cpp 880 B

1234567891011121314151617181920212223242526
  1. #include "current_process_memory.h"
  2. #include <Windows.h>
  3. #include <cstring>
  4. namespace sunrise::client::memory {
  5. /** Copies one range out of this process using the Windows memory API. */
  6. bool read_current_process([[maybe_unused]] void* context,
  7. std::uintptr_t address,
  8. std::span<std::byte> output) noexcept {
  9. if (address == 0 || output.empty()) {
  10. return false;
  11. }
  12. SIZE_T copied = 0;
  13. const void* source = nullptr;
  14. static_assert(sizeof source == sizeof address);
  15. // Copy every address bit without an implementation-defined integer conversion.
  16. std::memcpy(&source, &address, sizeof source);
  17. return ReadProcessMemory(GetCurrentProcess(), source, output.data(), output.size(), &copied)
  18. != FALSE
  19. && copied == output.size();
  20. }
  21. } // namespace sunrise::client::memory