Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Other Code Examples

View the contents of a block of memory

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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