|
-
Jul 31st, 2004, 11:45 PM
#1
Thread Starter
Frenzied Member
API: ReadProcessMemory [Resolved]
I HATE USING API's in .NET....
Anyway....
VB Code:
Private Const PROCESS_VM_READ = (&H10)
<DllImport("kernel32", EntryPoint:="OpenProcess")> _
Public Shared Function OpenProcess(ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
End Function
This Seems to be working correctly....
VB Code:
<DllImport("kernel32", EntryPoint:="CloseHandle")> _
Public Shared Function CloseHandle(ByVal hObject As Integer) As Integer
End Function
VB Code:
<DllImport("kernel32", EntryPoint:="ReadProcessMemory")> _
Public Shared Function ReadProcessMemory(ByVal hProcess As Integer, _
ByVal lpBaseAddress As Integer, _
ByRef lpBuffer As Object, _
ByVal nSize As Integer, _
ByRef lpNumberOfBytesWritten As Integer) As Integer
End Function
This seems to be giving me the problem....
if it Fails the return value is 0
VB Code:
Private Function ReadStringPointer(ByVal intProcessId As Integer, ByVal intPointer As Integer) As String
'Read Pointer
Dim ProcessHandle As Integer = OpenProcess(PROCESS_VM_READ, 0, intProcessId)
If ProcessHandle = 0 Then
MsgBox("Invaild ProcessHandle")
Exit Function
End If
Dim Buffer As String = New String(Chr(0), 260)
Dim intRet As Integer = Winamp.ReadProcessMemory(ProcessHandle, intPointer, Buffer, 260, vbNull)
Winamp.CloseHandle(ProcessHandle)
MsgBox("Process Handle: " & ProcessHandle & vbCrLf & _
"Return Value: " & intRet)
Return Buffer
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
-
Aug 1st, 2004, 01:45 AM
#2
Sleep mode
Try to set breakpoint and see if it's getting the processhandle right ? and where exactly the function terminates ?
-
Aug 1st, 2004, 02:12 AM
#3
Thread Starter
Frenzied Member
I copied all the code into a new Test Project...
VB Code:
#Region " API "
Private Const PROCESS_VM_READ = (&H10)
<DllImport("User32.dll", EntryPoint:="SendMessage")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal lParam As Integer, ByVal wParam As Integer) As IntPtr
End Function
<DllImport("kernel32", EntryPoint:="OpenProcess")> _
Public Shared Function OpenProcess(ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
End Function
<DllImport("kernel32", EntryPoint:="CloseHandle")> _
Public Shared Function CloseHandle(ByVal hObject As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="FindWindowA")> _
Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("kernel32", EntryPoint:="ReadProcessMemory")> _
Public Shared Function ReadProcessMemory(ByVal hProcess As Integer, _
ByVal lpBaseAddress As Integer, _
ByRef lpBuffer As Object, _
ByVal nSize As Integer, _
ByRef lpNumberOfBytesWritten As Integer) As Integer
End Function
#End Region
'Constants
Private Const WM_USER As Int32 = &H400
Private Const IPC_GETPLAYLISTFILE = 211
Private Const IPC_GETPLAYLISTTITLE = 212
Public Const WA_TITLE = 212
Public Const WA_TRACK = 125
Private Const IPC_ISPLAYING = 104
Private Const IPC_GETLISTPOS As Long = 125
Public Shared Function FindWinampProcess() As Process
Dim RunningProcesses() As Process = Process.GetProcessesByName("winamp")
If RunningProcesses.Length > 0 Then
'Were Set
Return RunningProcesses(0)
Else
Return Nothing
End If
End Function
Private Function ReadStringPointer(ByVal intProcessId As Integer, ByVal intPointer As Integer) As String
'Read Pointer
Dim ProcessHandle As Integer = OpenProcess(PROCESS_VM_READ, 0, intProcessId)
Debug.WriteLine("ProcessHandle: " & ProcessHandle) 'Seems Like a Vaild Handle
If ProcessHandle = 0 Then
MsgBox("Invaild ProcessHandle")
Exit Function
End If
Dim Buffer As String = New String(Chr(0), 260)
Debug.WriteLine("Before ReadProcessMemory") ' Last Thing Displayed
Dim intRet As Integer = ReadProcessMemory(ProcessHandle, intPointer, Buffer, 260, vbNull) ' Stops Here, Treats it like Application.Exit
Debug.WriteLine("After ReadProcessMemory")
CloseHandle(ProcessHandle)
Debug.WriteLine("After CloseHandle")
MsgBox("Process Handle: " & ProcessHandle & vbCrLf & _
"Return Value: " & intRet)
Return Buffer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intProcessId As Integer = FindWinampProcess.Id
Dim intWinamphWnd As Integer = FindWinampProcess.MainWindowHandle.ToInt32
Dim intSongIndex As Integer = SendMessage(New IntPtr(intWinamphWnd), WM_USER, 0, IPC_GETLISTPOS).ToInt32
Dim intPointer As Integer = SendMessage(New IntPtr(intWinamphWnd), WM_USER, intSongIndex, WA_TITLE).ToInt32
MsgBox("Winamp Process ID: " & intProcessId & vbCrLf & _
"Winamp hWnd: " & intWinamphWnd & vbCrLf & _
"Song Index: " & intSongIndex & vbCrLf & _
"Pointer: " & intPointer)
'All This Seems Vaild
MsgBox(ReadStringPointer(intProcessId, intPointer))
MsgBox("Testing") 'THis Never Gets Displayed
'no form nothing
End Sub
VB Code:
'Working VB 6 Code
Private Function ReadProcessPointer(lpProcessHandle As Long, lpPointer As Long) As Byte()
MsgBox lpProcessHandle & " : " & lpPointer
Dim ProcessHandle As Long
ProcessHandle = OpenProcess(PROCESS_VM_READ, 0, lpProcessHandle)
If ProcessHandle = 0 Then
Exit Function
End If
Dim bRet As Long
Dim Buffer(MAX_PATH) As Byte
Dim dwRead As Integer
MsgBox "PH:" & ProcessHandle
bRet = ReadProcessMemory(ProcessHandle, lpPointer, Buffer(0), MAX_PATH, 0)
MsgBox "bRet:" & bRet
ReadProcessPointer = Buffer
CloseHandle ProcessHandle
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
-
Aug 1st, 2004, 03:38 AM
#4
Thread Starter
Frenzied Member
Problem Solved: 
VB Code:
Option Explicit On
Imports System.Runtime.InteropServices
Public Class ProcessMemoryReader
Private Class ProcessMemoryReaderAPI
Public Const PROCESS_VM_READ = &H10
<DllImport("kernel32.dll")> _
Public Shared Function OpenProcess( _
ByVal dwDesiredAccess As Int32, _
ByVal bInheritHandle As Int32, _
ByVal dwProcessId As Int32) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Public Shared Function ReadProcessMemory( _
ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
ByVal buffer() As Byte, _
ByVal size As Int32, _
ByRef lpNumberOfBytesRead As Int32) As Int32
End Function
<DllImport("kernel32.dll")> _
Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Int32
End Function
End Class
#Region " Member Variables "
Private m_ReadProcess As Process = Nothing
Private m_hProcess As IntPtr = IntPtr.Zero
Private m_blnProcessOpen As Boolean
#End Region
#Region " Member Methods "
Public Sub OpenProcess()
m_hProcess = ProcessMemoryReaderAPI.OpenProcess( _
ProcessMemoryReaderAPI.PROCESS_VM_READ, _
1, _
m_ReadProcess.Id _
)
If m_hProcess.ToInt32 = 0 Then
Throw New Exception("OpenProcess Faild!")
Else
m_blnProcessOpen = True
End If
End Sub
Public Sub CloseProcess()
If ProcessMemoryReaderAPI.CloseHandle(m_hProcess) = 0 Then
Throw New Exception("CloseHandle failed")
Else
m_blnProcessOpen = False
End If
End Sub
Public Function ReadMemory(ByVal MemoryAddress As IntPtr, ByVal intCount As Integer) As Byte()
If Not m_blnProcessOpen Then
Throw New Exception("Process not Open!")
End If
Dim buffer(intCount - 1) As Byte
ProcessMemoryReaderAPI.ReadProcessMemory(m_hProcess, MemoryAddress, buffer, intCount, Nothing)
Return buffer
End Function
#End Region
#Region " Member Properties "
Public Property ReadProcess() As Process
Get
Return m_ReadProcess
End Get
Set(ByVal Value As Process)
m_ReadProcess = Value
End Set
End Property
Public ReadOnly Property IsProcessOpen() As Boolean
Get
Return m_blnProcessOpen
End Get
End Property
#End Region
Protected Overrides Sub Finalize()
If m_blnProcessOpen Then
Me.CloseProcess()
End If
MyBase.Finalize()
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|