Converting this from VB6 to VBnet?

Servoris

Member
Joined
Dec 28, 2004
Messages
6
Location
USA
Programming Experience
1-3
Thanx for any help. The code below worked well in VB6 but as I was hobbiest in vb6, I am floundering in VBnet. Please help me convert this, VBnet seems to dislike this code.





Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

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

Declare Function ReadProcessMemoryStr Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As String, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

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

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Dim hwnd As Long
Dim pid As Long
Dim pHandle As Long
Dim hProcess As Long
Dim Val1 As String * 16
Dim Val2 As Long
Dim Val3 As Long
Dim Myhex

hwnd = FindWindow(vbNullString, "any program window name here")
If hwnd = 0 Then
Exit Sub
End If

GetWindowThreadProcessId (hwnd, pid)

pHandle = OpenProcess(&H1F0FFF, False, pid)
If pHandle = 0 Then
Exit Sub
End If

'example

ReadProcessMemoryStr pHandle, &H1000070, Val1, 1, 0& 'Reads as String
Text1 = Val1
ReadProcessMemoryVal pHandle, &H1000070, Val2, 1, 0& 'Reads as value
Text2 = Val2

ReadProcessMemoryVal pHandle, &H1000070, Val3, 1, 0& 'Reads as hex
Myhex = Hex(Val3)
Text3 = Myhex

WriteProcessMemory pHandle, &H1000070, 4, 1, 0& 'Set value to 4
 
Last edited:
The code converter converts it as is. But the code does not work in VBnet. Works great in VB6.

Thanx
 
Last edited:
Not there yet!

OK, I got most of this workout except for this section:

Dim Val1 As String

ReadProcessMemoryStr(pHandle, &H1000070, Val1, 1, 0&) 'Reads as String

TextBox1.Text = Val1



In VB6 it would look like this:

Dim Val1 As String * 1

ReadProcessMemoryStr(pHandle, &H1000070, Val1, 1, 0&) 'Reads as String

TextBox1.Text = Val1

The Dim Val1 As String * 1 allows the string to be entered as text for that number of characters, but VBnet does not recognize the * 1. As a result I can't get the character. [I am using * 1 as an example if you set the number of bytes read to 5 and set the string to * 5 it would return 5 characters.]

Any way to fix this is most appreciated. With this every thing will work



 
Last edited:
dim Val1 as Char

ReadProcessMemoryStr(pHandle, &H1000070, Val1, 1, 0&) 'Reads as String

Textbox1= Val1

that should do it, but i'm not sure
 
Thanx for your interest!

Hey guys I got it all working. It took me some brain storming or should I say some brain melting. Any way here is the finished code in VBnet. It seems to work fine.


PublicDeclareFunction FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName AsString, ByVal lpWindowName AsString) AsInteger

PublicDeclareFunction GetWindowThreadProcessId Lib "user32" (ByVal hWnd AsInteger, ByRef lpdwProcessId AsInteger) AsInteger

PublicDeclareFunction OpenProcess Lib "kernel32" (ByVal dwDesiredAccess AsInteger, ByVal bInheritHandle AsInteger, ByVal dwProcessId AsInteger) AsInteger

PublicDeclareFunction WriteProcessMemoryVal Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess AsInteger, ByVal lpBaseAddress AsInteger, ByRef lpBuffer AsInteger, ByVal nSize AsInteger, ByVal lpNumberOfBytesWritten AsInteger) AsInteger

PublicDeclareFunction ReadProcessMemoryStr Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess AsInteger, ByVal lpBaseAddress AsInteger, ByVal lpBuffer AsString, ByVal nSize AsInteger, ByVal lpNumberOfBytesWritten AsInteger) AsInteger

PublicDeclareFunction ReadProcessMemoryVal Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess AsInteger, ByVal lpBaseAddress AsInteger, ByRef lpBuffer AsInteger, ByVal nSize AsInteger, ByVal lpNumberOfBytesWritten AsInteger) AsInteger

PublicDeclareFunction CloseHandle Lib "kernel32" (ByVal hObject AsInteger) AsInteger





Dim hwnd As Integer
Dim pid As Integer
Dim pHandle As Integer
Dim hProcess As Integer
Dim Val1 As String
Dim Val2 As Long
Dim Val3 As Long
Dim Valtext As String
Dim Myhex

hwnd = FindWindow(vbNullString, "any_program_window_name")
If hwnd = 0 Then
ExitSub
EndIf

Call GetWindowThreadProcessId(hwnd, pid)

pHandle = OpenProcess(&H1F0FFF,
False, pid)
If pHandle = 0 Then
ExitSub
EndIf

Val1 = NewString(CChar(" "), 16)
ReadProcessMemoryStr(pHandle, &H404270, Val1, 1, 0&) 'Reads as String
Valtext = Strings.Left(Val1, InStr(Val1, Chr(0)) + 16)
TextBox1.Text = Valtext

ReadProcessMemoryVal(pHandle, &H404270, Val2, 1, 0&)
'Reads as value
TextBox2.Text = Val2

ReadProcessMemoryVal(pHandle, &H404270, Val3, 1, 0&)
'Reads as hex
Myhex = Hex(Val3)
TextBox3.Text = Myhex

WriteProcessMemoryVAL(pHandle, &H404270, 124, 1, 0&) 'Write a value of 124

CloseHandle(hProcess)

 
Last edited:
Back
Top