新建From1(窗体),新建Command1(按钮CommandButton),代码:
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
Private Sub Command1_Click()
Dim sTest As String
Dim abyteString() As Byte
sTest = "码农库MaNongKu.com"
MsgBox MemoryViewer(StrPtr(sTest), 32)
MsgBox MemoryViewer(StrPtr(StrConv(sTest, vbFromUnicode)), 32)
End Sub