Click here to Skip to main content
15,886,079 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Update:
i'm able to change the name but not read it..

i get this error (WHEN READING THE MEMORY):
Error 1 Value of type '1-dimensional array of Byte' cannot be converted to 'String'.

What I have tried:

Public Shared Function ReadMemory(Of T)(ByVal address As Integer) As T
        Return MemoryModule.ReadMemory(Of T)(address, 0, False)
    End Function

    Public Shared Function ReadMemory(ByVal address As Integer, ByVal length As Integer) As Byte()
        Return MemoryModule.ReadMemory(Of Byte())(address, length, False)
    End Function

    Public Shared Function ReadMemory(Of T)(ByVal address As Integer, ByVal value As Object, ByVal unicodeString As Boolean)
        Dim buffer As Byte()
        If (GetType(T) Is GetType(String)) Then
            If unicodeString Then
                buffer = Encoding.Unicode.GetBytes(value.ToString)
            Else
                buffer = Encoding.ASCII.GetBytes(value.ToString)
            End If
        ElseIf (GetType(T) Is GetType(Byte())) Then
            buffer = Encoding.Unicode.GetBytes(value.ToString)
        Else
            buffer = Encoding.ASCII.GetBytes(value.ToString)
        End If
        If Not MemoryModule.UpdateProcessHandle Then
            Return CType(Nothing, T)
        End If
        Dim lpBaseAddress As New IntPtr(address)
        Dim dwSize As New IntPtr(buffer.Length)
        If Not MemoryModule.ReadProcessMemory(MemoryModule.ProcessHandle, lpBaseAddress, buffer, dwSize, IntPtr.Zero) Then
            Return CType(Nothing, T)
        End If
        If (GetType(T) Is GetType(Byte())) Then
            Return Conversions.ToGenericParameter(Of T)(buffer)
        End If
        Dim handle As GCHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned)
        Dim local2 As T = Conversions.ToGenericParameter(Of T)(RuntimeHelpers.GetObjectValue(Marshal.PtrToStructure(handle.AddrOfPinnedObject, GetType(T))))
        handle.Free()
        Return local2
    End Function

    Public Shared Function WriteMemory(ByVal address As Integer, ByVal value As Object, ByVal [unicode] As Boolean) As Boolean
        Dim bytes As Byte()
        If Not MemoryModule.UpdateProcessHandle Then
            Return False
        End If
        If TypeOf value Is String Then
            If [unicode] Then
                bytes = Encoding.Unicode.GetBytes(value.ToString)
            Else
                bytes = Encoding.ASCII.GetBytes(value.ToString)
            End If
        Else
            bytes = MemoryModule.GetObjectBytes(RuntimeHelpers.GetObjectValue(value))
        End If
        Dim lpBaseAddress As New IntPtr(address)
        Dim nSize As New IntPtr(bytes.Length)
        Return MemoryModule.WriteProcessMemory(MemoryModule.ProcessHandle, lpBaseAddress, bytes, nSize, IntPtr.Zero)
    End Function

 Public Shared Function WriteMemory(ByVal address As Integer, ByVal value As Object) As Boolean
        Return MemoryModule.WriteMemory(address, RuntimeHelpers.GetObjectValue(value), False)
    End Function


And when trying to read the memory with that (HERE I GET THE ERROR)
Label2.Text = MemoryModule.ReadMemory(&H234BFD4, AscW((ChrW(0))))


and this is how i write memory:
MemoryModule.WriteMemory(&H234BFD4, (CrystalClearTextBox9.Text & ChrW(0)), False)
the write one is working fine
Posted
Updated 9-Apr-17 5:10am
v2
Comments
Tomas Takac 9-Apr-17 6:12am    
So this is a runtime exception? On which line exactly is it thrown?
Ralf Meier 9-Apr-17 6:48am    
I suppose that the Error occurs when you try to assign the Result from ReadMemory to the Label2.Text.
If I'm right the Errormessage is clear : you deliver an Array of Byte but the Label awaits a String - so you only need to create a string from your Byte-Array ... or add this conversion to you ReadMemory-Method ...

Have you tested with the debugger which Method is really called (the first or the second) ?
Do you get the right Type-Information inside the T-Object ?
SDHP442 9-Apr-17 8:56am    
can you help me with that conversion maybe?

1 solution

Convert the byte array to a string before assigning it to the label text:
VB
Label2.Text = System.Text.Encoding.Unicode.GetString(MemoryModule.ReadMemory(&H234BFD4, AscW((ChrW(0)))))
 
Share this answer
 
Comments
SDHP442 9-Apr-17 13:14pm    
Tried this one: Label2.Text = System.Text.Encoding.ASCII.GetString(MemoryModule.ReadMemory(&H234BFD4, AscW((ChrW(0)))))

It shows only the first 2 letters of the name, any idea ?

@CHill60
CHill60 9-Apr-17 14:01pm    
If you debug and examine the result of MemoryModule.ReadMemory(&H234BFD4, AscW((ChrW(0))) I presume the byte array only contains the first 2 letters of the name?
SDHP442 9-Apr-17 14:11pm    
Yes, is there any way to show the full name?
like if the name is TEST it show only TE
CHill60 9-Apr-17 14:14pm    
Pass a longer length to the ReadMemory function perhaps? Debug your ReadMemory function and work out why it stops after 2 characters?
SDHP442 9-Apr-17 14:19pm    
Really have no idea how to do that xD
MemoryModule.ReadMemory(&H234BFD4, AscW((ChrW(200)))
changed that Chrw from 0 to 50 - 200 can see now 3 letters of the name

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900