VB and VBA Users Source Code: View the contents of a block of memory
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
View the contents of a block of memory
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, March 26, 2001
Hits:
656
Category:
Windows API
Article:
This routine can be invaluable when you are trying to read the contents of a memory location. A demonstration routine can be found at the bottom of the post. Option Explicit Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long) 'Purpose : Examines the contents of a block of memory. 'Inputs : lStartAddress The memory address to start returning the contents of. ' lNumBytes The number of bytes to return. 'Outputs : Returns a string containing a table of the contents of the memory block. 'Author : Andrew Baker 'Date : 11/09/2000 'Notes : 'Revisions : Public Function MemoryViewer(ByVal lStartAddress As Long, ByVal lNumBytes As Long) As String Const clNumBytesPerRow = 16 Dim lThisByte As Long, lThisChar As Long Dim abyteBuffer() As Byte Dim lNumWholeBytes As Long 'Create output buffer ReDim abyteBuffer(0 To lNumBytes - 1) As Byte 'Copy memory to buffer Call CopyMemory(abyteBuffer(0), ByVal lStartAddress, lNumBytes) 'Create Header MemoryViewer = String$(90, "_") & vbNewLine & "Start Address = &H" & Hex$(lStartAddress) & vbTab & " Number of Bytes = " & lNumBytes & vbNewLine & vbNewLine & "Address: OS: Memory:" & String(3 * clNumBytesPerRow - 8, " ") & "ASCII:" 'Determine how many bytes required to fill whole number of rows lNumWholeBytes = lNumBytes + IIf(lNumBytes Mod clNumBytesPerRow, 16 - (lNumBytes Mod clNumBytesPerRow), 0) 'Loop through buffer, displaying clNumBytesPerRow bytes per row. For lThisByte = 0 To lNumWholeBytes - 1 If (lThisByte Mod clNumBytesPerRow) = 0 Then '-----------Starting a new row 'Append this byte and add address/offset. MemoryViewer = MemoryViewer & (vbNewLine & Right$("00000000" & Hex$(lStartAddress + lThisByte), 8) & " " & Right$("0000" & Hex$(lThisByte), 4) & " ") & (Right$("0" & Hex(abyteBuffer(lThisByte)), 2) & " ") ElseIf (lThisByte Mod clNumBytesPerRow) = clNumBytesPerRow - 1 Then '-----------End of row 'Display ASCII values at end of row. MemoryViewer = MemoryViewer & " " For lThisChar = (lThisByte - clNumBytesPerRow + 1) To lThisByte If lThisChar >= lNumBytes Then 'End of block MemoryViewer = MemoryViewer & String(lNumWholeBytes - lNumBytes, "") Exit For ElseIf abyteBuffer(lThisChar) >= 32 And abyteBuffer(lThisChar) <= 126 Then 'Valid ASCII Alpha Char MemoryViewer = MemoryViewer & Chr$(abyteBuffer(lThisChar)) Else 'Invalid ASCII Char MemoryViewer = MemoryViewer & "." End If Next ElseIf lThisByte < lNumBytes Then '-----------Append this byte MemoryViewer = MemoryViewer & (Right$("0" & Hex(abyteBuffer(lThisByte)), 2) & " ") Else 'Past end of required memory MemoryViewer = MemoryViewer & ".. " End If Next 'Create Footer MemoryViewer = MemoryViewer & vbNewLine & String$(90, "_") End Function 'Demonstration routine Sub Test() Dim sTest As String Dim abyteString() As Byte sTest = "IAMBORED" 'StrPtr returns the pointer to the first byte, the length of the string 'is stored in the previous 4 bytes. Note, VB strings are stored as unicde Debug.Print MemoryViewer(StrPtr(sTest) - 4, LenB(asdasd) + 4) 'View the string as an ANSI string Debug.Print MemoryViewer(StrPtr(StrConv(sTest, vbFromUnicode)) - 4, (LenB(asdasd)/2) + 4) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder