vb如何将一个二维数a(3,4)传递给一个函数

zcdg909 2008-04-03 12:02:45
vb如何将一个二维数a(3,4)传递给一个函数
...全文
76 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
关注 接分
回复
meiZiNick 2008-05-01
有问题请先GOOGLE,BAIDU
回复
UltraBejing 2008-05-01
接分先!
回复
像C那样的做法就是传首地址,然后指针移动。
VB如果要这样做,也可以。
用byref传入数组的首个元素,比如 CALL ABC(A(0))
然后在函数ABC内用COPYMEMORY来得到整个数组。因为只要知道数组的长度和类型,那么整个数组的字节数是可以计算出来的。
回复
shenyangyu 2008-04-04
用variant类型
回复
东方之珠 2008-04-04
[Quote=引用 9 楼 WallesCai 的回复:]
像C那样的做法就是传首地址,然后指针移动。
VB如果要这样做,也可以。
用byref传入数组的首个元素,比如 CALL ABC(A(0))
然后在函数ABC内用COPYMEMORY来得到整个数组。因为只要知道数组的长度和类型,那么整个数组的字节数是可以计算出来的。
[/Quote]

是的,而且速度很快!看来你是支持我的!
回复
forbearORfolie 2008-04-04
全局声明数组,然后把函数改成过程
回复
东方之珠 2008-04-03
如果做得好呢,像C++那样只传递二维数组首地址a就可以啦。不过要用到VarPtr函数。
回复
qiu5208 2008-04-03
用byref传址即可.
回复
clear_zero 2008-04-03
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

'---------------------------
欢迎来我的小店坐坐: 程序员雅琪的小店
回复
东方之珠 2008-04-03
下面程序演示:像C++一样传送数组首地址并返回数组元素的值

'一个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
回复
swankie 2008-04-03
定义一维的,然后二维使用就没事了。

Private Function ddd(d() As Long) As Boolean
Dim dd As Integer
dd = d(1, 1)
print dd
End Function



回复
swankie 2008-04-03
网上的例子,我试过可以用的。

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


回复
zzyong00 2008-04-03
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
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-04-03 12:02
社区公告
暂无公告