Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[VB 2008] Read/Write Longs,Doubles,Floats, and String

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Sun Jan 03, 2010 2:41 pm    Post subject: [VB 2008] Read/Write Longs,Doubles,Floats, and String Reply with quote

Hello, I'm rather new to these forums, but not that new to game hacking. I've been trying to figure out for a couple days now, how to properly use "ReadProcessMemory" and "WriteProcessMemory" to Read/Write Floats, Longs, Doubles, and Strings. I have a feeling I'm doing it wrong in my Module, but here's what I have so far:

Code:
 
Module ReadWritingMemory
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    Private Declare Function WriteProcessMemory1 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function WriteProcessMemory2 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function WriteProcessMemory3 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function ReadProcessMemory1 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function ReadProcessMemory2 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function ReadProcessMemory3 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    Const PROCESS_ALL_ACCESS = &H1F0FF

    Public Sub WriteInteger(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Integer)
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Sub
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Sub
        End If

        Dim hAddress, vBuffer As Integer
        hAddress = Address
        vBuffer = Value
        WriteProcessMemory1(hProcess, hAddress, CInt(vBuffer), vBuffer.ToString.Length, 0)
    End Sub

    Public Sub WriteFloat(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Single)
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Sub
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Sub
        End If

        Dim hAddress As Integer
        Dim vBuffer As Single

        hAddress = Address
        vBuffer = Value
        WriteProcessMemory2(hProcess, hAddress, vBuffer, vBuffer.ToString.Length, 0)
    End Sub

    Public Sub WriteLong(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Long)
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Sub
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Sub
        End If

        Dim hAddress As Integer
        Dim vBuffer As Long

        hAddress = Address
        vBuffer = Value
        WriteProcessMemory3(hProcess, hAddress, vBuffer, vBuffer.ToString.Length, 0)
    End Sub

    Public Function ReadInteger(ByVal ProcessName As String, ByVal Address As Integer) As Integer
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Function
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Function
        End If

        Dim hAddress, vBuffer As Integer
        hAddress = Address
        ReadProcessMemory1(hProcess, hAddress, vBuffer, 4, 0)
        Return vBuffer
    End Function

    Public Function ReadFloat(ByVal ProcessName As String, ByVal Address As Integer) As Single
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Function
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Function
        End If

        Dim hAddress As Integer
        Dim vBuffer As Single

        hAddress = Address
        ReadProcessMemory2(hProcess, hAddress, vBuffer, 4, 0)
        Return vBuffer
    End Function

    Public Function ReadLong(ByVal ProcessName As String, ByVal Address As Integer) As Long
        If ProcessName.EndsWith(".exe") Then
            ProcessName = ProcessName.Replace(".exe", "")
        End If
        Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
        If MyP.Length = 0 Then
            MessageBox.Show(ProcessName & " isn't open!")
            Exit Function
        End If
        Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
        If hProcess = IntPtr.Zero Then
            MessageBox.Show("Failed to open " & ProcessName & "!")
            Exit Function
        End If

        Dim hAddress As Integer
        Dim vBuffer As Long

        hAddress = Address
        ReadProcessMemory3(hProcess, hAddress, vBuffer, 4, 0)
        Return vBuffer
    End Function

End Module



Oh, and I didn't even bother attempting to try and Read/Write string yet. Seems way to complex.

Thanks in Advanced =)
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Jan 03, 2010 9:42 pm    Post subject: Reply with quote

you don't need to do all those declarations.

You probably will be fine with a cast for most data types, but you may have to allocate some memory for a byte array (like a string) then copy to an IntPtr and pass that.

Writing a string is just basically writing an array of bytes. The BitConverter class has a method for this. Maybe a byte array declaration would be best.



http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx

looks useful as well. PInvoke.net has a signature that uses it.

http://www.pinvoke.net/default.aspx/kernel32.WriteProcessMemory
http://www.pinvoke.net/default.aspx/kernel32.ReadProcessMemory


and you should really be using C# if you want to use a .NET language.


Last edited by hcavolsdsadgadsg on Mon Jan 04, 2010 1:52 am; edited 1 time in total
Back to top
View user's profile Send private message
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Mon Jan 04, 2010 12:08 am    Post subject: Reply with quote

unfortunately, I don't have the time to learn a new Language, even if it is similar to VB.NET.

I really don't want to say this, but unfortunately, I need to, this is stressing me out too much.

If someone could be so kind as to provide me with a VB.NET Code that will Read/Write Process Memory as the following: A Long, A Float, A Double, A String, A Integer.

I would also like to request that they could provide me with code that will Read the Address and output the value as Hex, and possibly convert the Hex to a String. Like how "4D" Hex is equal to the letter "M" in Ascii. My aim is to make a Trainer that will Edit Floats, Doubles, 1byte integer, 2byte integer, 4 byte integer, and 8 byte integers and String. If you could even link me to a tutorial for making VB Trainer's, I would take a look at that. Thanks.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Jan 04, 2010 1:50 am    Post subject: Reply with quote

You can do unsafe pointer trickery on IntPtr's as well in C#.

If you wanted to pass a float to it, you could do:

Code:
unsafe
{
    float f     = 1230.0213f;
    IntPtr p    = *(IntPtr*)&f
}




Or maybe you could use a byte array in the declaration and just convert everything to bytes before passing it with the BitConveter class!
This might actually be your best bet.

oh lord the possibilities!
Back to top
View user's profile Send private message
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Mon Jan 04, 2010 3:54 pm    Post subject: Reply with quote

It would help quite alot if you could use VB.NET. I'm trying to simplify this as much as possible. I believe I have the Other's working, I just need to figure out how to Read/Write Strings now. If someone could provide some VB.NET code on how to do so, that'd be greatly appreciated.
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Mon Jan 04, 2010 4:01 pm    Post subject: Reply with quote

Visual Basic Sucks.

Get that in your mind, you won't go far in hacking if your using that gay language.
Back to top
View user's profile Send private message MSN Messenger
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Mon Jan 04, 2010 4:04 pm    Post subject: Reply with quote

Okay. It's in my mind. I'm still going to use it.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Jan 04, 2010 4:12 pm    Post subject: Reply with quote

Mal1t1a wrote:
It would help quite alot if you could use VB.NET. I'm trying to simplify this as much as possible. I believe I have the Other's working, I just need to figure out how to Read/Write Strings now. If someone could provide some VB.NET code on how to do so, that'd be greatly appreciated.


I told you what to do and how to handle it already. I'm trying to get you to think a little / actually do something without just eating a face full of provided code.


The solution to all your problems:
The BitConveter class:
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

The Marshal class:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx

System.Text namespace, probably the ASCIIEncoding class
http://msdn.microsoft.com/en-us/library/system.text.aspx



THIS IS PROBABLY WHAT YOU WANT TO READ / SEE
aka: "fuck it"

The simplest way to handle everything is probably to just declare the buffer argument of Write/ReadProcessMemory as a byte array and just use BitConveter to have it spit out an array before you pass it. Or in the case of a string, ASCIIEncoding.


Code:
C#
int     butt    = 0x01234567;
byte[]  chode   = BitConverter.GetBytes(butt);

string str      = "bitchez call me Mal1t1a";
byte[] dongs    = System.Text.ASCIIEncoding.ASCII.GetBytes(str);



nasty verbose VB.NET
Dim butt As Integer = &H1234567
Dim chode As Byte() = BitConverter.GetBytes(butt)

Dim str As String = "bitchez call me Mal1t1a"
Dim dongs As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(str)



Last edited by hcavolsdsadgadsg on Mon Jan 04, 2010 4:26 pm; edited 4 times in total
Back to top
View user's profile Send private message
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Mon Jan 04, 2010 4:19 pm    Post subject: Reply with quote

Alright, I'll get to work on trying it with the Bit Converter, also, I Don't know much about the Marhsal Class. What's it even used for? Just so you don't have to Declare API's? That's all I can tell from reading about it.

Last edited by Mal1t1a on Mon Jan 04, 2010 4:22 pm; edited 1 time in total
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Jan 04, 2010 4:20 pm    Post subject: Reply with quote

Edited my post above.

edit: edited the edited edit...
Back to top
View user's profile Send private message
Mal1t1a
Cheater
Reputation: 0

Joined: 03 Jan 2010
Posts: 40

PostPosted: Mon Jan 04, 2010 4:29 pm    Post subject: Reply with quote

ahaha. I like it here. That is just about exactly what I want. I'll stop reading this article, and try that out "in my way" to see if it is.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites