Results 1 to 16 of 16

Thread: Read memory - Reading string from address

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Read memory - Reading string from address

    Below i am using this common readmemory function. Im calling the function like so

    Code:
    msgbox ReadMemory(hWnd, &H883221, 10)
    However it just msg's a long number rather than a string, and i know theres a string at that address. Ive got to edit something in the functon and possible the way i call it, any ideas how?
    thanks

    Code:
    Public Function ReadMemory(hWnd As Long, Address As Long, Bytes As Long, Optional strReplaceWith As String) As String
        'Runs For Not Unicode Strings (VB-Strings)
        On Error Resume Next
        Dim pId As Long        ' Used to hold the Process Id
        Dim pHandle As Long    ' Holds the Process Handle
        Dim bytValue As Long   'Stores the value of a byte in the memory
        Dim i As Long
        Dim Text As String
        Dim BytePass As Integer
       
        ' Get the ProcId of the Window
        GetWindowThreadProcessId hWnd, pId
    
        ' use the pId to get a handle
        pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
    
        If (pHandle = 0) Then
             shpCon.FillColor = vbRed
             Timer1.Enabled = False
     
             Exit Function
        End If
    
        If Address = 0 Then Exit Function
    
        shpCon.FillColor = vbGreen
        Timer1.Enabled = True
        BytePass = Bytes
        If Bytes = 2 Then BytePass = 4
        For i = 1 To Bytes Step 2
           ' Read Byte to Byte
           
           ReadProcessMemory pHandle, Address + i - 1, bytValue, BytePass, 0&
           'value now contains the long value of the byte located in [Address + i - 1] pos.
           'ReadMemory is a string...
           '
       
    
           ReadMemory = bytValue
    
        Next
        'to write numeric values you can ..(Must) use WriteValue API
        If LenB(strReplaceWith) <> 0 Then
            'No Unicode!!
            WriteString pHandle, Address, StrPtr(strReplaceWith), LenB(strReplaceWith), 0&
        End If
        'Close the Handle
        CloseHandle pHandle
    End Function

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Read memory - Reading string from address

    Well from what I remember is that you need CopyMemory and the appropriate variable type to recieve the string. I believe, but don't take my word for it, that you will need a byte array and then use StrConv to convert the byte array into the final string you are looking for.



    Good Luck
    Option Explicit should not be an Option!

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Read memory - Reading string from address

    As far as I know you can't actually read a specific memory location - well you can but it's all virtualized. What I mean by that is that Windows is a 'Protected Operating System' so hardware is virtualized. When you think your app is speaking to physical hardware it's just windows "pretending" to allow you to speak to physical memory. It's not the actual computer's actual hardware memory address. So just because one application thinks it can see some data at a specific address doesn't mean that another application will see the same data at that same address.

    If you're just looking for some way for applications to share some common data then you might look at a technology called 'Memory Mapped Files'. This is not actually a file - it's just memory but by using the MMF standards you are able to have more than one app reading and writing to the same 'physical' memory.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    IanS, the address i am reading contain the data i am after, i know this because all the other address the program reads contain the correct values however those values are long variables so i can read them, however this address is a string and i cant seem to read it due to an error in my coding.

    i tried a string convert in the function (ie convert the function return before it was returned) but that didnt work, maybe the code i used was wrong, any code anyone can suggest would be welcomed

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Read memory - Reading string from address

    I've never used these particular API before but I've had a quick go a a couple of untested functions based on your code and the MSDN reference.
    Code:
    Public Function ReadProcessBytes(ByVal hWnd As Long, ByVal Address As Long, ByVal Size As Long) As Byte()
        Dim pId As Long
        Dim pHandle As Long
        Dim readsz As Long
        Dim buf() As Byte
        
        If Address <> 0 And Size > 0 Then
            GetWindowThreadProcessId hWnd, pId
            pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
            If pHandle Then
                ReDim buf(Size - 1)
                ReadProcessMemory pHandle, Address, buf(0), Size, readsz
                CloseHandle pHandle
            End If
        End If
        
        If readsz Then
            If readsz <> Size Then ReDim Preserve buf(readsz - 1)
        Else
            buf = vbNullString
        End If
        
        ReadProcessBytes = buf
    End Function
    
    Public Function WriteProcessBytes(ByVal hWnd As Long, ByVal Address As Long, Bytes() As Byte) As Long
        Dim pId As Long
        Dim pHandle As Long
        Dim writesz As Long
        
        If Address <> 0 Then
            GetWindowThreadProcessId hWnd, pId
            pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
            If pHandle Then
                WriteProcessMemory pHandle, Address, Bytes(LBound(Bytes)), UBound(Bytes) - LBound(Bytes) + 1, writesz
                CloseHandle pHandle
            End If
        End If
        
        WriteProcessBytes = writesz
    End Function
    reading a non unicode string would work like this...
    Code:
        Dim sTest As String
        sTest = StrConv(ReadProcessBytes(hWnd, Address, Size), vbUnicode)
    have you considered that you might be reading a pointer value?
    W o t . S i g

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    Quote Originally Posted by Milk View Post
    I've never used these particular API before but I've had a quick go a a couple of untested functions based on your code and the MSDN reference.
    Code:
    Public Function ReadProcessBytes(ByVal hWnd As Long, ByVal Address As Long, ByVal Size As Long) As Byte()
        Dim pId As Long
        Dim pHandle As Long
        Dim readsz As Long
        Dim buf() As Byte
        
        If Address <> 0 And Size > 0 Then
            GetWindowThreadProcessId hWnd, pId
            pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
            If pHandle Then
                ReDim buf(Size - 1)
                ReadProcessMemory pHandle, Address, buf(0), Size, readsz
                CloseHandle pHandle
            End If
        End If
        
        If readsz Then
            If readsz <> Size Then ReDim Preserve buf(readsz - 1)
        Else
            buf = vbNullString
        End If
        
        ReadProcessBytes = buf
    End Function
    
    Public Function WriteProcessBytes(ByVal hWnd As Long, ByVal Address As Long, Bytes() As Byte) As Long
        Dim pId As Long
        Dim pHandle As Long
        Dim writesz As Long
        
        If Address <> 0 Then
            GetWindowThreadProcessId hWnd, pId
            pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
            If pHandle Then
                WriteProcessMemory pHandle, Address, Bytes(LBound(Bytes)), UBound(Bytes) - LBound(Bytes) + 1, writesz
                CloseHandle pHandle
            End If
        End If
        
        WriteProcessBytes = writesz
    End Function
    reading a non unicode string would work like this...
    Code:
        Dim sTest As String
        sTest = StrConv(ReadProcessBytes(hWnd, Address, Size), vbUnicode)
    have you considered that you might be reading a pointer value?
    oh it could be, ill find out. Plus thanks for code, ill give it a bash

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    ok i used ur function milk however i got a runtime error "out of memory" any ideas?

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Read memory - Reading string from address

    I never tested them sorry if I messed them up.

    I used buf() = vbnullstring to show a fail state by returning an array with a Ubound of -1 and an Lbound of 0. It appears that StrConv can't handle that although it can handle a null string (which is what the fail state buf() converts to). To avoid the error you could use...
    Code:
    sTest = StrConv(CStr(ReadProcessBytes(hWnd, Address, Size)), vbUnicode)
    Unfortunately if buf() is in it's fail state then the function has failed somehow, either because one or more of the parameters is incorrect or more likely because readsz has returned 0 where it should return the number of bytes read, which all going well should be the same as Size.

    Can you show me the declares you have used for Read/WriteProcessMemory and can you confirm that you are not using On Error Resume Next.

    Edit: No need, I see the code you posted is just a cut and paste from Allapi, I assume you are using the declares from there as well. I'll test them at some point later today to see If I notice anything.
    Last edited by Milk; Feb 1st, 2010 at 05:43 AM.
    W o t . S i g

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    thanks for helping, ill get working on this when i get home. look forward to ur outcome

  10. #10
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Read memory - Reading string from address

    Sorry not had a chance to test, and possibly won't have for a few days. Big job just landed, never rains but it pours eh.
    W o t . S i g

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    Milk, sorry to bother your code below worked however, it only returned the first letter of the string. Is there a way to make the function find all the letter in the string?
    i did some searching and as i expected the other letters in the string are stored in the next address' along ,so im assuming the function has to loop through the address somehow? but im not sure how to do that.
    Also not all the address have a letter in it, sometimes u get a blank address in the sequence, so i guess u would have to take that into consideration too.

    please could you help ?

    vb Code:
    1. sTest = StrConv(CStr(ReadProcessBytes(hWnd, Address, Size)), vbUnicode)

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    vb Code:
    1. Public Function ReadProcessBytes(ByVal hWnd As Long, ByVal Address As Long, ByVal Size As Long) As Byte()
    2.     Dim pId As Long
    3.     Dim pHandle As Long
    4.     Dim readsz As Long
    5.     Dim buf() As Byte
    6.    
    7.     If Address <> 0 And Size > 0 Then
    8.         GetWindowThreadProcessId hWnd, pId
    9.         pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
    10.         If pHandle Then
    11.             ReDim buf(Size - 1)
    12.             ReadProcessMemory pHandle, Address, buf(0), Size, readsz
    13.             CloseHandle pHandle
    14.         End If
    15.     End If
    16.    
    17.     If readsz Then
    18.         If readsz <> Size Then ReDim Preserve buf(readsz - 1)
    19.     Else
    20.         buf = vbNullString
    21.     End If
    22.    
    23.     ReadProcessBytes = buf
    24. End Function

    being called
    vb Code:
    1. sTest = StrConv(CStr(ReadProcessBytes(hWnd, Address, Size)), vbUnicode)

    bump. This almost resolved just need someone to show me how to edit the code in the above function so i can loop through addresses so that it returns whole strings and not just the first letter of each of the string.

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    bump

  14. #14
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Read memory - Reading string from address

    Quote Originally Posted by alacn View Post
    bump
    I think if you do it again, the moderators will do some actions against you...!

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    47

    Re: Read memory - Reading string from address

    Quote Originally Posted by akhileshbc View Post
    I think if you do it again, the moderators will do some actions against you...!
    i dont see why, its a valid question which hasnt been answered yet

  16. #16
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: Read memory - Reading string from address

    The question is valid of course but bumping threads is considered poor netiquette. I have yet to find a forum where it is not frowned upon.
    Slower than a crippled Vista
    More buggy than a fresh XP install
    Look! Down the road, some 50 miles behind the drunken snail.
    It's Ubuntu!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width