Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenProcess handle invalid for ReadProcessMemory
    primarykey
    data
    text
    <p>I've made this simple class to open a process and read memory from it: The problem is when I call <code>ReadDWORD</code> with any memory address <code>ReadProcessMemory</code> fails with error code 6: <code>ERROR_INVALID_HANDLE, The handle is invalid</code>. And I can't figure out what I'm doing wrong.</p> <p>If I put the <code>OpenProcess</code> part in the <code>ReadDWORD</code> function it works fine. Is there something wrong with how I store the handle? Why does it become invalid before I use it?</p> <p><strong>Memory.h</strong></p> <pre><code>#ifndef MEMORY_H #define MEMORY_H #include &lt;windows.h&gt; #include &lt;psapi.h&gt; #pragma comment(lib, "psapi.lib") #include &lt;iostream&gt; class Memory { public: Memory(); Memory(DWORD offset); ~Memory(); DWORD ReadDWORD(DWORD addr); private: HANDLE m_hProc; DWORD m_Offset; }; #endif </code></pre> <p><strong>Memory.cpp</strong></p> <pre><code>#include "Memory.h" Memory::Memory() { Memory(0); } Memory::Memory(DWORD offset) { m_hProc = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, false, 5444); // 5444 is the PID of a process I'm testing this with m_Offset = offset; } Memory::~Memory() { CloseHandle(m_hProc); } DWORD Memory::ReadDWORD(DWORD addr) { // Optional memory offset addr += m_Offset; DWORD value = -1; int result = ReadProcessMemory(m_hProc, (LPVOID)addr, &amp;value, sizeof(DWORD), NULL); if (result == 0) std::cout &lt;&lt; "ReadProcessMemory error: " &lt;&lt; GetLastError() &lt;&lt; std::endl; return value; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload