首页  编辑  

ReadProcessMemory示例

Tags: /C#/API调用/   Date Created:

[ DllImport ( "kernel32.dll" )]

static extern bool ReadProcessMemory( uint hProcess , IntPtr lpBaseAddress,

  IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);

[ DllImport ( "kernel32.dll" )]

static extern uint GetCurrentProcess();

private void button1_Click( object sender , EventArgs e)

{

    int [] i = { 1234 };

    IntPtr vBaseAddress = Marshal . UnsafeAddrOfPinnedArrayElement(i, 0 );

    byte [] vBuffer = new byte [ 4 ];

    IntPtr vBytesAddress = Marshal . UnsafeAddrOfPinnedArrayElement(vBuffer, 0 );

    uint vNumberOfBytesRead = 0 ;

   ReadProcessMemory(GetCurrentProcess(), vBaseAddress,

       vBytesAddress, 4 , ref vNumberOfBytesRead);

    int vInt = Marshal . ReadInt32(vBytesAddress);

   Text = vInt . ToString();

}