new_learner0
Member
- Joined
- May 3, 2011
- Messages
- 7
- Programming Experience
- Beginner
Hey All,
I am trying to use VirtualQueryEx to read the memory of a process (first instance of notepad in this case). This function returns a pointer to the MEMORY_BASIC_INFORMATION structure. I am trying to read this structure. But everytime I try to access a member of the structure, I get a zero, which indicates I am messing something up. I am copying my code here.
Any help will be greatly appreciated.
-Coder_in_distress
I am trying to use VirtualQueryEx to read the memory of a process (first instance of notepad in this case). This function returns a pointer to the MEMORY_BASIC_INFORMATION structure. I am trying to read this structure. But everytime I try to access a member of the structure, I get a zero, which indicates I am messing something up. I am copying my code here.
Any help will be greatly appreciated.
-Coder_in_distress
VB.NET:
Imports System.Runtime.InteropServices
Module Module1
Declare Auto Sub GetSystemInfo Lib "kernel32.dll" (ByRef Info As SYSTEM_INFO)
Public Declare Function VirtualQueryEx Lib "kernel32" Alias "VirtualQueryEx" (ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, <Out()> ByVal lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Long) As Long
End Function
Structure SYSTEM_INFO
Dim ProcessorArchitecture As Int16
Dim Reserved As Int16
Dim PageSize As Int32
Dim MinAppAddress As Int32
Dim MaxAppAddress As Int32
Dim ActiveProcMask As Int32
Dim NumberOfProcessors As Int32
Dim ProcessorType As Int32
Dim AllocGranularity As Int32
Dim ProcessorLevel As Int32
Dim ProcessorRevision As Int32
End Structure
Public Structure MEMORY_BASIC_INFORMATION
Dim BaseAddress As IntPtr
Dim AllocationBase As IntPtr
Dim AllocationProtect As UInt32
Dim RegionSize As IntPtr
Dim State As UInt32
Dim Protect As UInt32
Dim lType As UInt32
End Structure
Sub Main()
Dim ProcessToReadFrom As Process = Process.GetProcessesByName("notepad")(0)
Dim prHandle = ProcessToReadFrom.Handle
PrintDLLs(prHandle)
End Sub
Function PrintDLLs(ByVal handle As IntPtr)
Dim Address As Integer
Dim Sysinfo As SYSTEM_INFO
Dim MemInfo As MEMORY_BASIC_INFORMATION
GetSystemInfo(Sysinfo)
For Address = Sysinfo.MinAppAddress To Sysinfo.MaxAppAddress
Try
If VirtualQueryEx(handle, Address, MemInfo, Marshal.SizeOf(MemInfo)) = 0 Then
End If
Console.WriteLine(MemInfo.lType)
Address = Address + Sysinfo.PageSize
Catch ex As Exception
End Try
Next
End Function
End Module