ReadProcessMemory

  • Thread starter Thread starter Luke
  • Start date Start date
L

Luke

I am trying to convert some VB6 projects to VB.NET, however I'm having
trouble finding documentation of a new format for some functions, in
specific ReadProcessMemory. Since .NET no longer supports the 'as any'
declare I found myself trying integers & longs, however all attempts throw
back the error message 'Object reference not set to an instance of an
object.'.
My API declare is as follows

Public Declare Function ReadProcessMemory Lib "kernel32" Alias
"ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Integer,
ByVal lpBuffer As String, ByVal nSize As Integer, ByVal
lpNumberOfBytesWritten As Integer) As Integer
and the calling code is:

ReadProcessMemory(vProcessID, 16777216, vOut, 1, vNum)

at the time of calling, vProcessID is assigned the processID, 16777216 is
the base address of memory to be read in dec. vOut is an empty string and
vNum is an empty integer. I'm sure this will be an easy one for someone and
I'd appreciate the help.

Regards,

Luke
 
Hi Luke,

You can search this newsgroup for Long than you find your answers probably,
one I have copied for you it is from Tom Shelton. It is more complete, so
when you want to know more search for this question on long and/or Tom
Shelton.

1. Change your longs to integers - long is 64-bit in .NET
2. Loose the alias, let the runtime determine the correct function
3. Declare handle types as IntPtr
4. Pass System.Text.StringBuilder to API's that are going to fill the
sting buffer

I hope this helps?

Cor
 
Luke,
at the time of calling, vProcessID is assigned the processID

In addition to what Cor said, the first parameter should be a process
*handle*, not a process ID.



Mattias
 
Hi Herfried,
That's not really necessary.

Do you ever see me giving other answers on an API question than to tell that
it is better to use managed code.

But this time it was upgrading and I saw that Upgrading is a dead newsgroup.

So I could tell to search this newsgroup or copy it from Tom. So I did.

Using the stringbuilder is not wrong I thought, but it was my mainreason to
tell to search the newsgroup for the full answers. (Although thinking it
really over now it handles of course immutable strings so it is wrong)

:-))

Cor
 
Luke,
In addition to the others comments:

Do you actually need ReadProcessMemory?

Remember that the layout of Objects in the managed heap is different then
how VB6 did things. And things have the ability to move during the call to
ReadProcessMemory if the GC decides to collect garbage!

To handle the "As Any" you can overload the Declare for each actual type you
will be using.

If you don't have it you may want to consider purchasing ".NET and COM - The
Complete Interoperability Guide" by Adam Nathan from MS Press. He has a
couple of sections on getting unmanaged Win32 APIs to work with .NET. He
also includes a C# listing of all the Win32 functions.

Hope this helps
Jay
 
Hi,

You usually get Object reference not set to an instance of an
object when you are using byval when you should use byref. If you look the
documentation for a function use byval for in and byref for out. This is
what the api should look like. Be sure to add imports
system.runtime.interopservices to the top of your file.

Declare Function ReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory"
_

(ByVal hProcess As Integer, _

<MarshalAs(UnmanagedType.AsAny)> ByVal lpBaseAddress As Object, _

<MarshalAs(UnmanagedType.AsAny)> ByRef lpBuffer As Object, ByVal nSize As
Integer, _

ByRef lpNumberOfBytesWritten As Integer) As Integer



http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/readprocessmemory.asp

Ken
 
Hi Luke,

Did you have any concern on this issue?

I would appreciate if you could provide an update by posting a message in
the Newsgroup thread letting us know whether the information provided in
the response was useful in resolving the issue. If you still have
questions, please reply to the thread to let us know you need further
assistance.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top