473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Reading a processes memory

I am trying to read the memory being used by a process but I can't
quite figure out how to do it (or if it's even possible). I can get a
reference to the process using Process.GetProcessesByName and I can get the
base address using Process.MainModule.BaseAddress (which returns a IntPtr).
I thought that by using IntPtr.ToPointer() and casting to a char* I would be
able to read the memory as a stream of chars but it doesn't work because it
always throws a NullReferenceException when I try and dereference the
pointer.
Can anybody help me out here?

Thanks
class Class1 { [STAThread] static unsafe void Main(string[] args) {
Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*)
pm.BaseAddress.ToPointer(); char c = *ptr; // Throws
System.NullReferenceException Console.WriteLine(c);
Console.ReadLine(); } }


Nov 16 '05 #1
4 13788
Hi Matt:

Every process in Win32 has it's own address space, and actually if you
check the BaseAddress for every process on the system you'll find many
of them are the same.

To pull this off you'll need to PInvoke OpenProcess and
ReadProcessMemory. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemory will copy bytes from the other
process into your address space.

--
Scott
http://www.OdeToCode.com

On Mon, 16 Aug 2004 11:05:04 -0500, "Matt Burland" <wjousts@[no
spam]hotmail.com> wrote:
I am trying to read the memory being used by a process but I can't
quite figure out how to do it (or if it's even possible). I can get a
reference to the process using Process.GetProcessesByName and I can get the
base address using Process.MainModule.BaseAddress (which returns a IntPtr).
I thought that by using IntPtr.ToPointer() and casting to a char* I would be
able to read the memory as a stream of chars but it doesn't work because it
always throws a NullReferenceException when I try and dereference the
pointer.
Can anybody help me out here?

Thanks
class Class1 { [STAThread] static unsafe void Main(string[] args) {
Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*)
pm.BaseAddress.ToPointer(); char c = *ptr; // Throws
System.NullReferenceException Console.WriteLine(c);
Console.ReadLine(); } }


Nov 16 '05 #2
Thanks for your help. I checked out the OpenProcess and ReadProcessMemory
and with a little fiddling managed to get it to read the memory. Great. Now
the problem is to see if I can alter it and write it back. I tried using
WriteProcessMemory, and I've set the DesiredAccess when opening the process
to PROCESS_VM_READ | PROCESS_VM_WRITE but it comes back with a system error
code for ERROR_ACCESS_DENIED?
Any ideas what I need to do to be able to write stuff back (if it's even
possible)? Here's my code now, it opens the process, reads 200 bytes,
displays it on the console and then tries to write the same 200 bytes back:

class Class1

{

[DllImport("Kernel32.dll")]

public static extern IntPtr OpenProcess(int dwDesiredAccess, bool
bInheritHandle, Int32 dwProcessId);

[DllImport("Kernel32.dll")]

public static extern unsafe bool ReadProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytesRead);

[DllImport("Kernel32.dll")]

public static extern unsafe bool WriteProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytesWritten);

[DllImport("Kernel32.dll")]

public static extern int GetLastError();

public static readonly int PROCESS_VM_READ = 0x0010;

public static readonly int PROCESS_VM_WRITE = 0x0020;

[STAThread]

static unsafe void Main(string[] args)

{

Process[] p = Process.GetProcessesByName("notepad");

ProcessModule pm = p[0].MainModule;

Console.WriteLine(pm.BaseAddress + ":" + p[0].Id);

byte[] buffer = new byte[200];

fixed(byte* cptr = &buffer[0])

{

int x = 0;

int* xptr = &x;

IntPtr hProcess = OpenProcess(PROCESS_VM_READ,false,p[0].Id);

Console.WriteLine(hProcess);

bool result = ReadProcessMemory(hProcess,pm.BaseAddress,cptr,200 ,xptr);

Console.WriteLine(result + ":" + x);

for (int i=0; i<200; i++)

{

byte b = *(cptr+i);

string s = b.ToString("x");

s = s.PadLeft(2,'0');

Console.Write(s + " ");

}

x = 0;

Console.WriteLine();
result = WriteProcessMemory(hProcess,pm.BaseAddress,cptr,20 0,xptr);

Console.WriteLine(result+":"+*xptr);

Console.WriteLine(GetLastError());
}

Console.ReadLine();

}

}
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:fu********************************@4ax.com...
Hi Matt:

Every process in Win32 has it's own address space, and actually if you
check the BaseAddress for every process on the system you'll find many
of them are the same.

To pull this off you'll need to PInvoke OpenProcess and
ReadProcessMemory. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemory will copy bytes from the other
process into your address space.

--
Scott
http://www.OdeToCode.com

On Mon, 16 Aug 2004 11:05:04 -0500, "Matt Burland" <wjousts@[no
spam]hotmail.com> wrote:
I am trying to read the memory being used by a process but I can't
quite figure out how to do it (or if it's even possible). I can get a
reference to the process using Process.GetProcessesByName and I can get thebase address using Process.MainModule.BaseAddress (which returns a IntPtr).I thought that by using IntPtr.ToPointer() and casting to a char* I would beable to read the memory as a stream of chars but it doesn't work because italways throws a NullReferenceException when I try and dereference the
pointer.
Can anybody help me out here?

Thanks
class Class1 { [STAThread] static unsafe void Main(string[] args) {
Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*)
pm.BaseAddress.ToPointer(); char c = *ptr; // Throws
System.NullReferenceException Console.WriteLine(c);
Console.ReadLine(); } }

Nov 16 '05 #3
Actually I forgot to add the PROCESS_VM_WRITE in my sample and from looking
at the documentation I noticed I also need PROCESS_VM_OPERATION. When I set
both of those I get a different error: ERROR_NOACCESS.

"Matt Burland" <wjousts@[no spam]hotmail.com> wrote in message
news:cf**********@hood.uits.indiana.edu...
Thanks for your help. I checked out the OpenProcess and ReadProcessMemory
and with a little fiddling managed to get it to read the memory. Great. Now the problem is to see if I can alter it and write it back. I tried using
WriteProcessMemory, and I've set the DesiredAccess when opening the process to PROCESS_VM_READ | PROCESS_VM_WRITE but it comes back with a system error code for ERROR_ACCESS_DENIED?
Any ideas what I need to do to be able to write stuff back (if it's even
possible)? Here's my code now, it opens the process, reads 200 bytes,
displays it on the console and then tries to write the same 200 bytes back:
class Class1

{

[DllImport("Kernel32.dll")]

public static extern IntPtr OpenProcess(int dwDesiredAccess, bool
bInheritHandle, Int32 dwProcessId);

[DllImport("Kernel32.dll")]

public static extern unsafe bool ReadProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytesRead);

[DllImport("Kernel32.dll")]

public static extern unsafe bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytesWritten);

[DllImport("Kernel32.dll")]

public static extern int GetLastError();

public static readonly int PROCESS_VM_READ = 0x0010;

public static readonly int PROCESS_VM_WRITE = 0x0020;

[STAThread]

static unsafe void Main(string[] args)

{

Process[] p = Process.GetProcessesByName("notepad");

ProcessModule pm = p[0].MainModule;

Console.WriteLine(pm.BaseAddress + ":" + p[0].Id);

byte[] buffer = new byte[200];

fixed(byte* cptr = &buffer[0])

{

int x = 0;

int* xptr = &x;

IntPtr hProcess = OpenProcess(PROCESS_VM_READ,false,p[0].Id);

Console.WriteLine(hProcess);

bool result = ReadProcessMemory(hProcess,pm.BaseAddress,cptr,200 ,xptr);

Console.WriteLine(result + ":" + x);

for (int i=0; i<200; i++)

{

byte b = *(cptr+i);

string s = b.ToString("x");

s = s.PadLeft(2,'0');

Console.Write(s + " ");

}

x = 0;

Console.WriteLine();
result = WriteProcessMemory(hProcess,pm.BaseAddress,cptr,20 0,xptr);

Console.WriteLine(result+":"+*xptr);

Console.WriteLine(GetLastError());
}

Console.ReadLine();

}

}
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:fu********************************@4ax.com...
Hi Matt:

Every process in Win32 has it's own address space, and actually if you
check the BaseAddress for every process on the system you'll find many
of them are the same.

To pull this off you'll need to PInvoke OpenProcess and
ReadProcessMemory. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemory will copy bytes from the other
process into your address space.

--
Scott
http://www.OdeToCode.com

On Mon, 16 Aug 2004 11:05:04 -0500, "Matt Burland" <wjousts@[no
spam]hotmail.com> wrote:
I am trying to read the memory being used by a process but I can't
quite figure out how to do it (or if it's even possible). I can get a
reference to the process using Process.GetProcessesByName and I can get thebase address using Process.MainModule.BaseAddress (which returns a IntPtr).I thought that by using IntPtr.ToPointer() and casting to a char* I
would
beable to read the memory as a stream of chars but it doesn't work
because
italways throws a NullReferenceException when I try and dereference the
pointer.
Can anybody help me out here?

Thanks
class Class1 { [STAThread] static unsafe void Main(string[] args) {
Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*)
pm.BaseAddress.ToPointer(); char c = *ptr; // Throws
System.NullReferenceException Console.WriteLine(c);
Console.ReadLine(); } }


Nov 16 '05 #4
Hi Matt:

I'm afraid I have not worked with WriteProcessMemory much to know how
to troubleshoot this. I do know some pages will be marked as read only
pages - not sure if there is a guaranteed solution to unprotect
them... :/

--s

On Mon, 16 Aug 2004 15:57:29 -0500, "Matt Burland" <wjousts@[no
spam]hotmail.com> wrote:
Actually I forgot to add the PROCESS_VM_WRITE in my sample and from looking
at the documentation I noticed I also need PROCESS_VM_OPERATION. When I set
both of those I get a different error: ERROR_NOACCESS.


--
Scott
http://www.OdeToCode.com
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Williams | last post by:
Hi, I am trying to understand the performance implications of running a number of separate ActiveXexe processes as opposed to a single ActiveXexe with multiple threads on a Windows 2000 server....
6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
7
by: eriwik | last post by:
Hi, I'm working on a small application which processes PNG-images and need to read parts of them into structures and/or variables and I was wondering what assumptions one can make about how the...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
4
by: AN | last post by:
Greetings, We make an ASP.NET web application and we host it for our customers. We have provisioned hardware and hope to be able to service around 200 customers on this hardware. The web...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
by: garyusenet | last post by:
Hello. I have two programmes both a couple of years old but not .net. One is a DOS based programme and one is a C++ programme. They both run under windows. I would like to write a programme...
12
by: Sune | last post by:
Hi all, I want to make data stored in-memory (not disk) available to several processes. My concern is that poorly written C applications with dangling pointers may(will) damage the data in this...
4
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.