Hello!

I would appreciate if someone could convert this code from VB6 to VB.net.

Option Explicit

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function Tibia_Hwnd() As Long
 Dim tibiaclient As Long
 tibiaclient = FindWindow("tibiaclient", vbNullString)
 Tibia_Hwnd = tibiaclient
End Function

Public Function ReadMemory(Address As Long) As Long
 Dim PID As Long, PHandle As Long
 If Tibia_Hwnd = 0 Then Exit Function
 GetWindowThreadProcessId Tibia_Hwnd, PID
 PHandle = OpenProcess(&H10, False, PID)
 If PHandle = 0 Then Exit Function
 ReadProcessMemory PHandle, Address, ReadMemory, 4, 0&
 CloseHandle PHandle
End Function

I get errors on

Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long

Thank you!

Private Declare Function ReadProcessMemory1 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer,ByVal lpBaseAdress As Integer, ByVal lpBuffer() As Byte, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

or

Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer

both is working

Edited 6 Years Ago by GeekByChoiCe: n/a

Thanks for the help, no errors now, but it just return 0
@

Button1.click = label1.text = readmemory(&H63FE6C)

.

Option Explicit

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function Tibia_Hwnd() As Long
 Dim tibiaclient As Long
 tibiaclient = FindWindow("tibiaclient", vbNullString)
 Tibia_Hwnd = tibiaclient
End Function

Public Function ReadMemory(Address As Long) As Long
 Dim PID As Long, PHandle As Long
 If Tibia_Hwnd = 0 Then Exit Function
 GetWindowThreadProcessId Tibia_Hwnd, PID
 PHandle = OpenProcess(&H10, False, PID)
 If PHandle = 0 Then Exit Function
 ReadProcessMemory PHandle, Address, ReadMemory, 4, 0&
 CloseHandle PHandle
End Function

Private Sub Label1_Click()
Label1.Caption = ReadMemory(&H63FE8C)
End Sub

This is the whole vb6 code, and it works.
This show my current HP on a game called Tibia.

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByVal lpdwProcessId As Long) As Long
	Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Long) As Long
	Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As String, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
	Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
	Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

	Public Function Tibia_Hwnd() As Long
		Dim tibiaclient As Long
		tibiaclient = FindWindow("tibiaclient", vbNullString)
		Tibia_Hwnd = tibiaclient
	End Function

	Public Function ReadMemory(ByVal Address As Long) As String
		Dim PID As Long, PHandle As Long

		If Tibia_Hwnd() = 0 Then
			Return "window not found"
		End If

		GetWindowThreadProcessId(Tibia_Hwnd, PID)
		PHandle = OpenProcess(&H10, False, PID)
		If PHandle = 0 Then
			Return "Handle not found"
		End If
		Dim buffer As New String(" "c, 4)
		ReadProcessMemory(PHandle, Address, buffer, 4, 0&)
		CloseHandle(PHandle)
		Return buffer
	End Function

	Private Sub Label1_Click()
		Label1.Caption = ReadMemory(&H63FE8C)
	End Sub
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByVal lpdwProcessId As Long) As Long
	Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Long) As Long
	Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As String, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
	Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
	Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

	Public Function Tibia_Hwnd() As Long
		Dim tibiaclient As Long
		tibiaclient = FindWindow("tibiaclient", vbNullString)
		Tibia_Hwnd = tibiaclient
	End Function

	Public Function ReadMemory(ByVal Address As Long) As String
		Dim PID As Long, PHandle As Long

		If Tibia_Hwnd() = 0 Then
			Return "window not found"
		End If

		GetWindowThreadProcessId(Tibia_Hwnd, PID)
		PHandle = OpenProcess(&H10, False, PID)
		If PHandle = 0 Then
			Return "Handle not found"
		End If
		Dim buffer As New String(" "c, 4)
		ReadProcessMemory(PHandle, Address, buffer, 4, 0&)
		CloseHandle(PHandle)
		Return buffer
	End Function

	Private Sub Label1_Click()
		Label1.Caption = ReadMemory(&H63FE8C)
	End Sub

Now it's: Handle not found.
But I just want to give you a big thankyou for taking your time to help me!

change:

Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByVal lpdwProcessId As Long) As Long

to

Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long

this function is getting the processID. so ByRef will solve it.

Everytime i press the button, the application crashes.. : (

Public Class Form1
    Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Long) As Long
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As String, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Public Function Tibia_Hwnd() As Long
        Dim tibiaclient As Long
        tibiaclient = FindWindow("tibiaclient", vbNullString)
        Tibia_Hwnd = tibiaclient
    End Function

    Public Function ReadMemory(ByVal Address As Long) As String
        Dim PID As Long, PHandle As Long

        If Tibia_Hwnd() = 0 Then
            Return "window not found"
        End If

        GetWindowThreadProcessId(Tibia_Hwnd, PID)
        PHandle = OpenProcess(&H10, False, PID)
        If PHandle = 0 Then
            Return "Handle not found"
        End If
        Dim buffer As New String(" "c, 4)
        ReadProcessMemory(PHandle, Address, buffer, 4, 0&)
        CloseHandle(PHandle)
        Return buffer
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ReadMemory(&H63FE88)
    End Sub
End Class

then do a step by step debugging and post the exact exception message.
set a breakpoint at "Public Function ReadMemory(ByVal Address As Long) As String"
Press F5
your application will then stop on the breakpoint and then press F11 to step through until it throws the exception.

Alright here is the correct code:

Module Module1

	Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As Integer) As Integer
	Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As Integer
	Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As String, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
	Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
	Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

	Public Function Tibia_Hwnd() As Integer
		Dim tibiaclient As Integer
		tibiaclient = FindWindow(vbNullString, "Tibia")
		Tibia_Hwnd = tibiaclient
	End Function

	Public Function ReadMemory(ByVal Address As Integer) As String
		Dim PID As Integer, PHandle As Integer

		Dim hwnd As Integer = Tibia_Hwnd()
		If hwnd = 0 Then
			Return "window not found"
		End If

		GetWindowThreadProcessId(hwnd, PID)
		PHandle = OpenProcess(&H1F0FFF, False, PID)
		If PHandle = 0 Then
			Return "Handle not found"
		End If
		Dim buffer As New String(" "c, 4)
		ReadProcessMemory(PHandle, &H5BC460, buffer, 4, 0)
		Console.WriteLine(buffer)
		CloseHandle(PHandle)
		Return buffer
	End Function


	Sub Main()
		ReadMemory(&H63FE88)
		Console.ReadLine()
	End Sub

End Module

Its working so far. Only bad thing is that the Address you try to read is not valid. So guess you have to find the correct Address before your code will work. try this address for testing: &H5BC460

If you run windows7 then make also sure you run this as Admin and set in the app.manifest the level to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
just in case.

im sorry i cant help you further :s

This article has been dead for over six months. Start a new discussion instead.