16 out of 33 rated this helpful - Rate this topic

ReadProcessMemory function

Applies to: desktop apps only

Reads data from an area of memory in a specified process. The entire area to be read must be accessible or the operation fails.

Syntax

BOOL WINAPI ReadProcessMemory(
  __in   HANDLE hProcess,
  __in   LPCVOID lpBaseAddress,
  __out  LPVOID lpBuffer,
  __in   SIZE_T nSize,
  __out  SIZE_T *lpNumberOfBytesRead
);

Parameters

hProcess [in]

A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process.

lpBaseAddress [in]

A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.

lpBuffer [out]

A pointer to a buffer that receives the contents from the address space of the specified process.

nSize [in]

The number of bytes to be read from the specified process.

lpNumberOfBytesRead [out]

A pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

The function fails if the requested read operation crosses into an area of the process that is inaccessible.

Remarks

ReadProcessMemory copies the data in the specified address range from the address space of the specified process into the specified buffer of the current process. Any process that has a handle with PROCESS_VM_READ access can call the function.

The entire area to be read must be accessible, and if it is not accessible, the function fails.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Debugging Functions
Process Functions for Debugging
OpenProcess
VirtualAllocEx
WriteProcessMemory

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
W7 wont run read process memory
You may need to check your access permissions for "SE_DEBUG_NAME" for the current processes token. If not enabled. Enabled it. This must be done as administrator of course. void PrivilegeCheck() { LPCTSTR lpszPrivilege = "SeDebugPrivilege"; BOOL bEnablePrivilege = TRUE; HANDLE token; PRIVILEGE_SET privset; BOOL bResult; printf("Setting SeDebugPrivilege\r\n"); privset.PrivilegeCount = 1; privset.Control = PRIVILEGE_SET_ALL_NECESSARY; privset.Privilege[0].Attributes = 0; if (!LookupPrivilegeValue(NULL, lpszPrivilege, &privset.Privilege[0].Luid)) ErrorExit("LookupPrivilegeValue"); if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)) ErrorExit("OpenProcessToken"); if (!PrivilegeCheck(token, &privset, &bResult)) ErrorExit("PrivilegeCheck"); if (bResult) printf(" We have debug privileges for the system\r\n"); else printf(" Nope, Try again. Attempting to get it...\r\n"); if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) { printf("OpenProcessToken() error %u\n", GetLastError()); return FALSE; } printf("SetPrivilege() return value: %d\n\n", SetPrivilege(hToken, lpszPrivilege, bEnablePrivilege)); }
Not working on Win7 systems
I'm using ReadProcessMemory Win32 api to read variable value from an exe.This api works on Windows XP systems.But when i try to read variable value on Win7 the ReadProcessMemory API fails with an error code 126. Code Snippet: Version of DbgHelp library used : 6.12 ReadProcessMemory(m_processHandle,(LPVOID)addr,(LPVOID)&tempVar,2,NULL) Im getting the address using PSYMBOL_INFO structure available in the dbghelp library.
it works to read 64bit from 32bit application

i just found out that it works to read memory from a 64 bit app by a 32 bit app (at least as long as the high 32bit of the address is zero)



for those who want to get the notify icon rectangle within WinXP64 (i think the function of the subroutines defined by me are clear ennough not to place here)


  


typedef struct
{
INT iBitmap;
INT idCommand;
BYTE fsState;
BYTE fsStyle;
BYTE bReserved[6];
UINT64 dwData;
UINT64 iString;
//
} TBBUTTON64;
  


BOOL _TrayTools_GetSysTrayIconRectTB(HWND in_hClient, INT in_TrayID, RECT *out_Rect)
{
/* locals */
INT lv_N;
INT lv_BtnCnt;
BOOL lv_Result;
BOOL lv_IsWOW64;
HWND lv_hNotifyTB;
VOID *lv_hProcMem;
HANDLE lv_hProcess;
INT_PTR lv_UserData[2];
UINT64 lv_UserData64[2];
INT lv_BtnInfoSize;
TBBUTTON lv_BtnInfo;
TBBUTTON64 lv_BtnInfo64;
  


// find the toolbar
if ((lv_hNotifyTB = Shell_GetSysTaskbarNotifyWindowTB()) == NULL)
return FALSE;
  
// is this 32bit process running on x64?
lv_IsWOW64 = Is32BitBuildOnWin64();
  
// determine size
if (lv_IsWOW64)
lv_BtnInfoSize = sizeof(TBBUTTON64);
else
lv_BtnInfoSize = sizeof(TBBUTTON);
  
// allocate button structure in context of explorer process
if ((lv_hProcMem = AllocProcessHWND(lv_hNotifyTB, lv_BtnInfoSize, &lv_hProcess)) == NULL)
return FALSE;
  
// not yet found
lv_Result = FALSE;
  
// get number of toolbar buttons
lv_BtnCnt = SendMessage(lv_hNotifyTB, TB_BUTTONCOUNT, 0,0);
  
// loop through buttons
for (lv_N = lv_BtnCnt-1; lv_N >= 0; --lv_N)
{
// get button info
if (SendMessage(lv_hNotifyTB, TB_GETBUTTON, lv_N, (LPARAM)lv_hProcMem))
{
// adjust for WOW64?
if (lv_IsWOW64)
{
// get the actual contents
if (ReadProcess(lv_hProcess, lv_hProcMem, &lv_BtnInfo64, sizeof(TBBUTTON64)))
{
// the userdata starts with the attached window and uID (in XP/Vista/Win7)
if (ReadProcess(lv_hProcess, (VOID*)lv_BtnInfo64.dwData, lv_UserData64, 2*sizeof(UINT64)))
{
// compare
if (lv_UserData64[0] == (UINT64)in_hClient && (lv_UserData64[1]&0xFFFFFFFF) == in_TrayID)
{
// got it, not hidden?
lv_Result = ((lv_BtnInfo64.fsState & TBSTATE_HIDDEN) == 0);
              // loop no further
break;
}
}
}
}
else
{
// get the actual contents
if (ReadProcess(lv_hProcess, lv_hProcMem, &lv_BtnInfo, sizeof(TBBUTTON)))
{
// the userdata starts with the attached window and uID (in XP/Vista/Win7)
if (ReadProcess(lv_hProcess, (VOID*)lv_BtnInfo.dwData, lv_UserData, 2*sizeof(INT_PTR)))
{
// compare
if (lv_UserData[0] == (INT_PTR)in_hClient && lv_UserData[1] == in_TrayID)
{
// got it, not hidden?
lv_Result = ((lv_BtnInfo.fsState & TBSTATE_HIDDEN) == 0);
              // loop no further
break;
}
}
}
}
}
}
  
// found the button?
if (lv_Result)
{
// get the item rectangle, reuse processmemory as a RECT is smaller than TBBUTTON
lv_Result = SendMessage(lv_hNotifyTB, TB_GETITEMRECT, lv_N, (LPARAM)lv_hProcMem);
  
// get the contents
if (lv_Result)
lv_Result = ReadProcess(lv_hProcess, lv_hProcMem, out_Rect, sizeof(RECT));
  
// to screen coordinates
if (lv_Result)
ClientToScreenRect(lv_hNotifyTB, out_Rect);
}
  
// free process memory again
FreeProcess(lv_hProcess, lv_hProcMem, TRUE);
  
// done
return lv_Result;
}


Calling from 32bit process to read 64bit process
Calling from 32bit process to read 64bit process fails :(..
Please suggest how this can be worked out.
What about x86 and x64 compatibility?
What happens, if I call this function from a 32 bit process to read from a 64 bit process? How can I pass a 64 bit pointer to it?

I have same problem, calling from 32bit process to read from 64bit process and it fails :(
Re: What about x86 and x64 compatibility?

If you want to start a 64 bit app. from a 32 bit application for debugging (using DEBUG_PROCESS or DEBUG_ONLY_THIS_PROCESS flags) then the CreateProcess will fail and GetLastError() function will return code 50 ("The request is not supported."). If the debugger app. is a 64 bit app. and the debuggee app. is a 64 bit app. too then it will work (assuming you have the necessary rights).