Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows
Visual Basic > Other sample source codes
View the contents of a block of memory
View the contents of a block of memory 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. 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" Debug.Print MemoryViewer(StrPtr(sTest), 32) Debug.Print MemoryViewer(StrPtr(StrConv(sTest, vbFromUnicode)), 32) End Sub
Privacy Policy
|
Link to Us
|
Links