Hey there all, I am looking for some help with WriteProcessMemory. I am using VB 2005 and Trying to write a value to memory. Here is my write function, It does not error, but nothing ever gets written to the address, any help would be appreciated.
VB Code:
  1. Public Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
  2.     Public Const PROCESS_VM_WRITE As Integer = &H20
  3.     Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
  4.     Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Byte, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
  5.     Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByRef lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
  6.     Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
  7.     Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As IntPtr
  8.  
  9.     Private Sub setValue(ByVal hWnd As IntPtr, ByVal Address As Integer, ByVal nSize As Integer, ByVal value As Integer)
  10.         Dim pId As Integer        ' Process ID
  11.         Dim pHandle As IntPtr     ' Process Handle, changed to IntPtr
  12.         'Dim lvi As ListViewItem
  13.         Try
  14.             If nSize <= 0 OrElse Address <= 0 Then Exit Sub ' Get the ProcId of the Window
  15.             GetWindowThreadProcessId(hWnd, pId)
  16.  
  17.             ' Now use the pId to get a process handle
  18.             pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
  19.  
  20.             If (pHandle.ToInt32 = 0) Then
  21.                 Exit Sub
  22.             End If
  23.  
  24.             WriteProcessMemory(pHandle, Address, value, nSize, 0&)
  25.  
  26.             'Close the handle
  27.             CloseHandle(pHandle)
  28.         Catch ex As System.Exception
  29.             MsgBox(ex.Message)
  30.         End Try
  31.     End Sub

I can read from this location no problem, but when I try to write, it just plain never happens. Thanks in advance.