Results 1 to 4 of 4

Thread: API: ReadProcessMemory [Resolved]

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    API: ReadProcessMemory [Resolved]

    I HATE USING API's in .NET....


    Anyway....

    VB Code:
    1. Private Const PROCESS_VM_READ = (&H10)
    2.  
    3.     <DllImport("kernel32", EntryPoint:="OpenProcess")> _
    4.     Public Shared Function OpenProcess(ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    5.  
    6.     End Function

    This Seems to be working correctly....

    VB Code:
    1. <DllImport("kernel32", EntryPoint:="CloseHandle")> _
    2.     Public Shared Function CloseHandle(ByVal hObject As Integer) As Integer
    3.  
    4.     End Function


    VB Code:
    1. <DllImport("kernel32", EntryPoint:="ReadProcessMemory")> _
    2.     Public Shared Function ReadProcessMemory(ByVal hProcess As Integer, _
    3.         ByVal lpBaseAddress As Integer, _
    4.         ByRef lpBuffer As Object, _
    5.         ByVal nSize As Integer, _
    6.         ByRef lpNumberOfBytesWritten As Integer) As Integer
    7.  
    8.     End Function

    This seems to be giving me the problem....
    if it Fails the return value is 0

    VB Code:
    1. Private Function ReadStringPointer(ByVal intProcessId As Integer, ByVal intPointer As Integer) As String
    2.         'Read Pointer
    3.  
    4.         Dim ProcessHandle As Integer = OpenProcess(PROCESS_VM_READ, 0, intProcessId)
    5.  
    6.         If ProcessHandle = 0 Then
    7.             MsgBox("Invaild ProcessHandle")
    8.             Exit Function
    9.         End If
    10.  
    11.         Dim Buffer As String = New String(Chr(0), 260)
    12.  
    13.         Dim intRet As Integer = Winamp.ReadProcessMemory(ProcessHandle, intPointer, Buffer, 260, vbNull)
    14.  
    15.         Winamp.CloseHandle(ProcessHandle)
    16.  
    17.         MsgBox("Process Handle: " & ProcessHandle & vbCrLf & _
    18.             "Return Value: " & intRet)
    19.  
    20.         Return Buffer
    21.  
    22.     End Function


    This is driving me up the wall. After I grab the Proccess Id and the Pointer I call ReadStringPointer and my app just exits.... it doesnt throw exceptions, just exits. I dont even get to see the form (calling from Form_load)

    Orginally, I used IntPtr's for the Handles but it didnt work so i tried just integers.
    Last edited by <ABX; Aug 1st, 2004 at 08:26 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try to set breakpoint and see if it's getting the processhandle right ? and where exactly the function terminates ?

  3. #3

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I copied all the code into a new Test Project...


    VB Code:
    1. #Region " API "
    2.  
    3.     Private Const PROCESS_VM_READ = (&H10)
    4.  
    5.     <DllImport("User32.dll", EntryPoint:="SendMessage")> _
    6.     Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal lParam As Integer, ByVal wParam As Integer) As IntPtr
    7.  
    8.     End Function
    9.  
    10.     <DllImport("kernel32", EntryPoint:="OpenProcess")> _
    11.     Public Shared Function OpenProcess(ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    12.  
    13.     End Function
    14.  
    15.     <DllImport("kernel32", EntryPoint:="CloseHandle")> _
    16.     Public Shared Function CloseHandle(ByVal hObject As Integer) As Integer
    17.  
    18.     End Function
    19.  
    20.     <DllImport("user32.dll", EntryPoint:="FindWindowA")> _
    21.     Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    22.  
    23.     End Function
    24.  
    25.     <DllImport("kernel32", EntryPoint:="ReadProcessMemory")> _
    26.     Public Shared Function ReadProcessMemory(ByVal hProcess As Integer, _
    27.         ByVal lpBaseAddress As Integer, _
    28.         ByRef lpBuffer As Object, _
    29.         ByVal nSize As Integer, _
    30.         ByRef lpNumberOfBytesWritten As Integer) As Integer
    31.  
    32.     End Function
    33.  
    34.  
    35. #End Region
    36.  
    37.     'Constants
    38.  
    39.     Private Const WM_USER As Int32 = &H400
    40.     Private Const IPC_GETPLAYLISTFILE = 211
    41.     Private Const IPC_GETPLAYLISTTITLE = 212
    42.     Public Const WA_TITLE = 212
    43.     Public Const WA_TRACK = 125
    44.  
    45.     Private Const IPC_ISPLAYING = 104
    46.     Private Const IPC_GETLISTPOS As Long = 125
    47.  
    48.     Public Shared Function FindWinampProcess() As Process
    49.         Dim RunningProcesses() As Process = Process.GetProcessesByName("winamp")
    50.  
    51.         If RunningProcesses.Length > 0 Then
    52.             'Were Set
    53.             Return RunningProcesses(0)
    54.  
    55.         Else
    56.             Return Nothing
    57.         End If
    58.     End Function
    59.  
    60.     Private Function ReadStringPointer(ByVal intProcessId As Integer, ByVal intPointer As Integer) As String
    61.         'Read Pointer
    62.  
    63.         Dim ProcessHandle As Integer = OpenProcess(PROCESS_VM_READ, 0, intProcessId)
    64.  
    65.         Debug.WriteLine("ProcessHandle: " & ProcessHandle) 'Seems Like a Vaild Handle
    66.  
    67.         If ProcessHandle = 0 Then
    68.             MsgBox("Invaild ProcessHandle")
    69.             Exit Function
    70.         End If
    71.  
    72.         Dim Buffer As String = New String(Chr(0), 260)
    73.         Debug.WriteLine("Before ReadProcessMemory") ' Last Thing Displayed
    74.         Dim intRet As Integer = ReadProcessMemory(ProcessHandle, intPointer, Buffer, 260, vbNull) ' Stops Here, Treats it like Application.Exit
    75.         Debug.WriteLine("After ReadProcessMemory")
    76.  
    77.         CloseHandle(ProcessHandle)
    78.         Debug.WriteLine("After CloseHandle")
    79.  
    80.         MsgBox("Process Handle: " & ProcessHandle & vbCrLf & _
    81.             "Return Value: " & intRet)
    82.  
    83.         Return Buffer
    84.  
    85.     End Function
    86.  
    87.  
    88.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    89.         Dim intProcessId As Integer = FindWinampProcess.Id
    90.         Dim intWinamphWnd As Integer = FindWinampProcess.MainWindowHandle.ToInt32
    91.  
    92.         Dim intSongIndex As Integer = SendMessage(New IntPtr(intWinamphWnd), WM_USER, 0, IPC_GETLISTPOS).ToInt32
    93.         Dim intPointer As Integer = SendMessage(New IntPtr(intWinamphWnd), WM_USER, intSongIndex, WA_TITLE).ToInt32
    94.  
    95.         MsgBox("Winamp Process ID: " & intProcessId & vbCrLf & _
    96.             "Winamp hWnd: " & intWinamphWnd & vbCrLf & _
    97.             "Song Index: " & intSongIndex & vbCrLf & _
    98.             "Pointer: " & intPointer)
    99. 'All This Seems Vaild
    100.  
    101.         MsgBox(ReadStringPointer(intProcessId, intPointer))
    102.  
    103.         MsgBox("Testing") 'THis Never Gets Displayed
    104. 'no form nothing
    105.     End Sub


    VB Code:
    1. 'Working VB 6 Code
    2.  
    3. Private Function ReadProcessPointer(lpProcessHandle As Long, lpPointer As Long) As Byte()
    4.  
    5. MsgBox lpProcessHandle & " : " & lpPointer
    6.  
    7. Dim ProcessHandle As Long
    8.  
    9. ProcessHandle = OpenProcess(PROCESS_VM_READ, 0, lpProcessHandle)
    10.  
    11. If ProcessHandle = 0 Then
    12.     Exit Function
    13. End If
    14.  
    15. Dim bRet As Long
    16.  
    17. Dim Buffer(MAX_PATH) As Byte
    18.  
    19. Dim dwRead As Integer
    20. MsgBox "PH:" & ProcessHandle
    21. bRet = ReadProcessMemory(ProcessHandle, lpPointer, Buffer(0), MAX_PATH, 0)
    22.  
    23. MsgBox "bRet:" & bRet
    24.  
    25. ReadProcessPointer = Buffer
    26.  
    27. CloseHandle ProcessHandle
    28.  
    29. End Function
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Problem Solved:

    VB Code:
    1. Option Explicit On
    2.  
    3. Imports System.Runtime.InteropServices
    4.  
    5. Public Class ProcessMemoryReader
    6.  
    7.     Private Class ProcessMemoryReaderAPI
    8.  
    9.         Public Const PROCESS_VM_READ = &H10
    10.  
    11.         <DllImport("kernel32.dll")> _
    12.         Public Shared Function OpenProcess( _
    13.             ByVal dwDesiredAccess As Int32, _
    14.             ByVal bInheritHandle As Int32, _
    15.             ByVal dwProcessId As Int32) As IntPtr
    16.  
    17.         End Function
    18.  
    19.         <DllImport("kernel32.dll")> _
    20.         Public Shared Function ReadProcessMemory( _
    21.             ByVal hProcess As IntPtr, _
    22.             ByVal lpBaseAddress As IntPtr, _
    23.             ByVal buffer() As Byte, _
    24.             ByVal size As Int32, _
    25.             ByRef lpNumberOfBytesRead As Int32) As Int32
    26.  
    27.         End Function
    28.  
    29.         <DllImport("kernel32.dll")> _
    30.         Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Int32
    31.  
    32.         End Function
    33.  
    34.     End Class
    35.  
    36. #Region " Member Variables "
    37.  
    38.     Private m_ReadProcess As Process = Nothing
    39.     Private m_hProcess As IntPtr = IntPtr.Zero
    40.     Private m_blnProcessOpen As Boolean
    41.  
    42. #End Region
    43.  
    44. #Region " Member Methods "
    45.  
    46.     Public Sub OpenProcess()
    47.  
    48.         m_hProcess = ProcessMemoryReaderAPI.OpenProcess( _
    49.             ProcessMemoryReaderAPI.PROCESS_VM_READ, _
    50.             1, _
    51.             m_ReadProcess.Id _
    52.             )
    53.  
    54.         If m_hProcess.ToInt32 = 0 Then
    55.             Throw New Exception("OpenProcess Faild!")
    56.         Else
    57.             m_blnProcessOpen = True
    58.         End If
    59.  
    60.     End Sub
    61.  
    62.     Public Sub CloseProcess()
    63.  
    64.         If ProcessMemoryReaderAPI.CloseHandle(m_hProcess) = 0 Then
    65.             Throw New Exception("CloseHandle failed")
    66.         Else
    67.             m_blnProcessOpen = False
    68.         End If
    69.  
    70.     End Sub
    71.  
    72.     Public Function ReadMemory(ByVal MemoryAddress As IntPtr, ByVal intCount As Integer) As Byte()
    73.  
    74.         If Not m_blnProcessOpen Then
    75.             Throw New Exception("Process not Open!")
    76.         End If
    77.  
    78.         Dim buffer(intCount - 1) As Byte
    79.  
    80.         ProcessMemoryReaderAPI.ReadProcessMemory(m_hProcess, MemoryAddress, buffer, intCount, Nothing)
    81.  
    82.         Return buffer
    83.  
    84.     End Function
    85.  
    86. #End Region
    87.  
    88. #Region " Member Properties "
    89.  
    90.     Public Property ReadProcess() As Process
    91.         Get
    92.             Return m_ReadProcess
    93.         End Get
    94.         Set(ByVal Value As Process)
    95.             m_ReadProcess = Value
    96.         End Set
    97.     End Property
    98.  
    99.     Public ReadOnly Property IsProcessOpen() As Boolean
    100.         Get
    101.             Return m_blnProcessOpen
    102.         End Get
    103.     End Property
    104.  
    105. #End Region
    106.  
    107.     Protected Overrides Sub Finalize()
    108.         If m_blnProcessOpen Then
    109.             Me.CloseProcess()
    110.         End If
    111.         MyBase.Finalize()
    112.     End Sub
    113. End Class
    Last edited by <ABX; Aug 1st, 2004 at 08:25 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width