Option Explicit
'=======================================================================
' drMemory - Cross-Process Memory Buffer support
'
' (c) 2003 "Dr Memory" ==> Jim White
' MathImagics
' Uki, NSW, Australia
' Puttenham, Surrey, UK
'=======================================================================
'=======================================================================
' Excerpted and abbreviated by WorkHorse.
'=======================================================================
Private fpHandle As Long ' the foreign-process instance handle. When we want
' memory on NT platforms, this is returned to us by
' OpenProcess, and we pass it in to VirtualAllocEx.
' We must preserve it, as we need it for read/write
' operations, and to release the memory when we've
' finished with it.
''================== WinNT/2000 Process Memory functions
Private Declare Function OpenProcess Lib "KERNEL32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function VirtualAllocEx Lib "KERNEL32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "KERNEL32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function WriteProcessMemory Lib "KERNEL32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As Long, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "KERNEL32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As Long, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
'================== Common Platform
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Const PAGE_READWRITE = &H4
Const MEM_RESERVE = &H2000&
Const MEM_RELEASE = &H8000&
Const MEM_COMMIT = &H1000&
Const PROCESS_VM_OPERATION = &H8
Const PROCESS_VM_READ = &H10
Const PROCESS_VM_WRITE = &H20
Public Function drMemoryAlloc(ByVal xpWindow As Long, ByVal nBytes As Long) As Long
' Returns pointer to a share-able buffer (size nBytes) in target process that owns xpWindow
Dim xpThread As Long ' target control's thread id
Dim xpID As Long ' process id
' Assumes using WIN NT type OS. -WorkHorse
xpThread = GetWindowThreadProcessId(xpWindow, xpID)
drMemoryAlloc = VirtualAllocNT(xpID, nBytes)
End Function
Public Sub drMemoryRead(ByVal xpBuffer As Long, ByVal myBuffer As Long, ByVal nBytes As Long)
' Assumes using WIN NT type OS. -WorkHorse
ReadProcessMemory fpHandle, xpBuffer, myBuffer, nBytes, 0
End Sub
Public Sub drMemoryFree(ByVal mPointer As Long)
' Assumes using WIN NT type OS. -WorkHorse
VirtualFreeNT mPointer
End Sub
Private Function VirtualAllocNT(ByVal fpID As Long, ByVal memSize As Long) As Long
fpHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, fpID)
VirtualAllocNT = VirtualAllocEx(fpHandle, ByVal 0&, ByVal memSize, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
End Function
Private Sub VirtualFreeNT(ByVal MemAddress As Long)
Call VirtualFreeEx(fpHandle, ByVal MemAddress, 0&, MEM_RELEASE)
CloseHandle fpHandle
End Sub