Solved

ReadProcessMemory only returning one byte

Posted on 2011-03-16
1
541 Views
Last Modified: 2012-05-11
Hi, I have the following C# code. It's meant to return an Integer, but it only returns 256 at the most.
It appears that I'm missing the next byte.
Does anybody have any ideas?

Thanks
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);
bool result = ReadProcessMemory(hProcess, baseAddress, cptr, 8, null);

Open in new window

0
Comment
Question by:Netlink2
1 Comment
 
LVL 5

Accepted Solution

by:
MedievalWarrior earned 500 total points
ID: 35147319
You don't need to allocate the buffer just pass the pointer to the Integer variable ( This may require some modification to P/Invoke siganture) and it will copy it but make sure you have the correct baseAddress and your using the correct Length of the data type...

** It's meant to return an Integer **

Which type of Integer ? Your code indicates you want a LONG integer because your requesting 8 bytes. If you want Integer then request 4 bytes. You can use the Integer variable as the buffer instead of a byte array and request to read 4 bytes or use a Long variable and request 8 bytes. If you use byte buffer then you could use BitConverter.ToInt32() but make sure you specify the correct offsets. You might also want to allow the last parameter of ReadProcessMemory() to fill in the amount of bytes read so that you can make sure that what you request is the same as what has been returned.


bool result = ReadProcessMemory(hProcess, baseAddress, xptr, 4, cbRead); 

// If cbRead = 4 then you know the call returned you the correct amount of bytes requested

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now