7,785
社区成员




'一个Form1,一个文本框Text1,一个命令按钮Command1
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByVal lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Dim arr(3, 4) As Long
Private Sub Command1_Click()
Call TransARR(arr(0, 0))
End Sub
Private Sub Form_Load()
arr(0, 0) = 1200
arr(3, 4) = 200
Form1.Caption = "传送数组首地址并取得数组元素的内容"
End Sub
'传送数组首地址并取得数组元素的内容
Public Function TransARR(ByVal str As Long)
' 声明一些需要的变量
Dim arrAdress As Long '数组首地址
Dim ReadValue As Long '数组地址相应内容
Dim hwnd As Long ' 储存 FindWindow 函数返回的句柄
Dim pid As Long ' 储存进程标识符( Process Id )
Dim pHandle As Long ' 储存进程句柄
Dim chenjl1031 As Variant '转换成16进制
arrAdress = VarPtr(str) '取得数组首地址
chenjl1031 = "&H" & (Hex(arrAdress))
'Debug.Print "chenjl1031=" & chenjl1031
' 首先取得目标窗口的句柄
hwnd = FindWindow(vbNullString, "传送数组首地址并取得数组元素的内容")
Debug.Print "hwnd=" & hwnd
If (hwnd = 0) Then
MsgBox "Window not found!"
Exit Function
End If
' 取得进程标识符pid
Call GetWindowThreadProcessId(hwnd, pid)
' 使用进程标识符取得进程句柄
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
If (pHandle = 0) Then
MsgBox "Couldn't get a process handle!"
Exit Function
End If
'这里只获得了arr(0, 0)的内容,其他元素自己想办法
ReadProcessMemory pHandle, ByVal arrAdress, ReadValue, 4, 0&
' 关闭进程句柄
CloseHandle pHandle
'输出指定地址内容
Text1.Text = ReadValue
End Function
Private Sub Command3_Click()
Dim aryGet() As Long
Dim i As Long
Dim j As Long
If GetArray(aryGet()) Then
For i = 0 To 3
For j = 0 To 3
Print "aryGet(" & i & "," & j & ")=" & aryGet(i, j)
Next j
Next i
End If
End Sub
Private Function GetArray(aryTmp() As Long) As Boolean
Dim i As Long
Dim j As Long
ReDim aryTmp(3, 3)
For i = 0 To 3
For j = 0 To 3
aryTmp(i, j) = i * j
Next j
Next i
GetArray = True
End Function
Option Explicit
Private Sub Form_Load()
Dim arr(3, 4) As String
arr(0, 0) = "asdf"
arr(3, 4) = "asdf"
Call Fun(arr)
End Sub
Public Function Fun(arr() As String)
Debug.Print UBound(arr, 1), UBound(arr, 2)
End Function