473,545 Members | 289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetProc essesByName and I can get the
base address using Process.MainMod ule.BaseAddress (which returns a IntPtr).
I thought that by using IntPtr.ToPointe r() 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 NullReferenceEx ception 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.GetProc essesByName("no tepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLi ne(pm.BaseAddre ss); char* ptr = (char*)
pm.BaseAddress. ToPointer(); char c = *ptr; // Throws
System.NullRefe renceException Console.WriteLi ne(c);
Console.ReadLin e(); } }


Nov 16 '05 #1
4 13805
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
ReadProcessMemo ry. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemo ry 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.GetProc essesByName and I can get the
base address using Process.MainMod ule.BaseAddress (which returns a IntPtr).
I thought that by using IntPtr.ToPointe r() 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 NullReferenceEx ception 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.GetProc essesByName("no tepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLi ne(pm.BaseAddre ss); char* ptr = (char*)
pm.BaseAddress .ToPointer(); char c = *ptr; // Throws
System.NullRef erenceException Console.WriteLi ne(c);
Console.ReadLi ne(); } }


Nov 16 '05 #2
Thanks for your help. I checked out the OpenProcess and ReadProcessMemo ry
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
WriteProcessMem ory, and I've set the DesiredAccess when opening the process
to PROCESS_VM_READ | PROCESS_VM_WRIT E but it comes back with a system error
code for ERROR_ACCESS_DE NIED?
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("Kern el32.dll")]

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

[DllImport("Kern el32.dll")]

public static extern unsafe bool ReadProcessMemo ry(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytes Read);

[DllImport("Kern el32.dll")]

public static extern unsafe bool WriteProcessMem ory(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytes Written);

[DllImport("Kern el32.dll")]

public static extern int GetLastError();

public static readonly int PROCESS_VM_READ = 0x0010;

public static readonly int PROCESS_VM_WRIT E = 0x0020;

[STAThread]

static unsafe void Main(string[] args)

{

Process[] p = Process.GetProc essesByName("no tepad");

ProcessModule pm = p[0].MainModule;

Console.WriteLi ne(pm.BaseAddre ss + ":" + p[0].Id);

byte[] buffer = new byte[200];

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

{

int x = 0;

int* xptr = &x;

IntPtr hProcess = OpenProcess(PRO CESS_VM_READ,fa lse,p[0].Id);

Console.WriteLi ne(hProcess);

bool result = ReadProcessMemo ry(hProcess,pm. BaseAddress,cpt r,200,xptr);

Console.WriteLi ne(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.WriteLi ne();
result = WriteProcessMem ory(hProcess,pm .BaseAddress,cp tr,200,xptr);

Console.WriteLi ne(result+":"+* xptr);

Console.WriteLi ne(GetLastError ());
}

Console.ReadLin e();

}

}
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:fu******** *************** *********@4ax.c om...
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
ReadProcessMemo ry. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemo ry 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.GetProc essesByName and I can get thebase address using Process.MainMod ule.BaseAddress (which returns a IntPtr).I thought that by using IntPtr.ToPointe r() 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 NullReferenceEx ception 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.GetProc essesByName("no tepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLi ne(pm.BaseAddre ss); char* ptr = (char*)
pm.BaseAddress .ToPointer(); char c = *ptr; // Throws
System.NullRef erenceException Console.WriteLi ne(c);
Console.ReadLi ne(); } }

Nov 16 '05 #3
Actually I forgot to add the PROCESS_VM_WRIT E in my sample and from looking
at the documentation I noticed I also need PROCESS_VM_OPER ATION. 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.in diana.edu...
Thanks for your help. I checked out the OpenProcess and ReadProcessMemo ry
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
WriteProcessMem ory, and I've set the DesiredAccess when opening the process to PROCESS_VM_READ | PROCESS_VM_WRIT E but it comes back with a system error code for ERROR_ACCESS_DE NIED?
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("Kern el32.dll")]

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

[DllImport("Kern el32.dll")]

public static extern unsafe bool ReadProcessMemo ry(IntPtr hProcess, IntPtr
lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytes Read);

[DllImport("Kern el32.dll")]

public static extern unsafe bool WriteProcessMem ory(IntPtr hProcess, IntPtr lpBaseAddress, byte* lpBuffer, int nSize, int* lpNumberOfBytes Written);

[DllImport("Kern el32.dll")]

public static extern int GetLastError();

public static readonly int PROCESS_VM_READ = 0x0010;

public static readonly int PROCESS_VM_WRIT E = 0x0020;

[STAThread]

static unsafe void Main(string[] args)

{

Process[] p = Process.GetProc essesByName("no tepad");

ProcessModule pm = p[0].MainModule;

Console.WriteLi ne(pm.BaseAddre ss + ":" + p[0].Id);

byte[] buffer = new byte[200];

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

{

int x = 0;

int* xptr = &x;

IntPtr hProcess = OpenProcess(PRO CESS_VM_READ,fa lse,p[0].Id);

Console.WriteLi ne(hProcess);

bool result = ReadProcessMemo ry(hProcess,pm. BaseAddress,cpt r,200,xptr);

Console.WriteLi ne(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.WriteLi ne();
result = WriteProcessMem ory(hProcess,pm .BaseAddress,cp tr,200,xptr);

Console.WriteLi ne(result+":"+* xptr);

Console.WriteLi ne(GetLastError ());
}

Console.ReadLin e();

}

}
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:fu******** *************** *********@4ax.c om...
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
ReadProcessMemo ry. Looking those API functions up in the SDK on MSDN
will get you started. ReadProcessMemo ry 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.GetProc essesByName and I can get thebase address using Process.MainMod ule.BaseAddress (which returns a IntPtr).I thought that by using IntPtr.ToPointe r() 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 NullReferenceEx ception 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.GetProc essesByName("no tepad"); ProcessModule pm =
p[0].MainModule; Console.WriteLi ne(pm.BaseAddre ss); char* ptr = (char*)
pm.BaseAddress .ToPointer(); char c = *ptr; // Throws
System.NullRef erenceException Console.WriteLi ne(c);
Console.ReadLi ne(); } }


Nov 16 '05 #4
Hi Matt:

I'm afraid I have not worked with WriteProcessMem ory 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_WRIT E in my sample and from looking
at the documentation I noticed I also need PROCESS_VM_OPER ATION. 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
1836
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. I have been told that Windows does not allocate a fixed memory space to its processes, so they are always paged off to disk when inactive....
6
1769
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') >>> f.write('dan\n') >>> f.write('pat\n') >>> f.close()
7
6195
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 compiler places things in memory. Or more specific, I want to read the color-palette into a array of structs with the following format:
9
23058
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 to a pool. If I assign only one application to a applicaton pool and have multiple worker processes assigned to that pool. Will my application be...
4
7819
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 servers are in a stateless farm and have 2 GB of RAM. We are using ASP.NET 1.1 when using a dedicated application pool for each virtual directory. Each...
35
3989
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? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire...
6
5247
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
1259
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 that is capable of reading variables from these programmes. Can you please tell me where I start with such a project? I am aware that it probabally...
12
4968
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 memory segment if it is open to all, i.e. shared memory mapped into all processes memory area. I don't want to use TCP/IP client/server between...
4
5788
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
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.