VB如何避免由C语言编写的dll返回的数组
VB如何处理由C语言编写的dll返回的数组
本人用C编写了一个dll,提供一个函数Func返回一个variant类型,返回值是二维数组,但是数组的大小不确定,vb中调用该函数后得到返回值后,如何处理该数组呢?如何获得该数组的行数和列数呢?如何获取数据呢?因为我要对二维数组操作填充单元格。本人是初学者,忘指教,不胜感激!
------解决方案--------------------
A 表示DLL中返回的数组:
MsgBox UBound(A, 1) '返回数组的一维上标
MsgBox LBound(A, 1) '返回数组的一维下标
MsgBox UBound(A, 2) '返回数组的二维上标
MsgBox LBound(A, 2) '返回数组的二维下标
------解决方案--------------------
Private Sub Command1_Click()
Dim x(2, 5) As Long ' 假设它就是你得到的数组
M1 = UBound(x, 1)
M2 = UBound(x, 2)
N1 = LBound(x, 1)
N2 = LBound(x, 2)
y00 = x(0, 0)
y01 = x(0, 1)
y11 = x(1, 1)
………………
End Sub
------解决方案--------------------
'模拟你的c函数
Private Function getArr() As String()
Dim a(3, 2) As String
Dim i As Integer
Dim j As Integer
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
a(i, j) = "www" & i & j
Next j
Next i
getArr = a
End Function
Private Sub CommandButton1_Click()
Dim mya() As String
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
mya = getArr
m = 0
For i = LBound(mya, 1) To UBound(mya, 1)
m = m + 1
n = 0
For j = LBound(mya, 2) To UBound(mya, 2)
n = n + 1
Cells(m, n) = mya(i, j)
Next j
Next i
End Sub
------解决方案--------------------
本人用C编写了一个dll,提供一个函数Func返回一个variant类型,返回值是二维数组,但是数组的大小不确定,vb中调用该函数后得到返回值后,如何处理该数组呢?如何获得该数组的行数和列数呢?如何获取数据呢?因为我要对二维数组操作填充单元格。本人是初学者,忘指教,不胜感激!
------解决方案--------------------
A 表示DLL中返回的数组:
MsgBox UBound(A, 1) '返回数组的一维上标
MsgBox LBound(A, 1) '返回数组的一维下标
MsgBox UBound(A, 2) '返回数组的二维上标
MsgBox LBound(A, 2) '返回数组的二维下标
------解决方案--------------------
Private Sub Command1_Click()
Dim x(2, 5) As Long ' 假设它就是你得到的数组
M1 = UBound(x, 1)
M2 = UBound(x, 2)
N1 = LBound(x, 1)
N2 = LBound(x, 2)
y00 = x(0, 0)
y01 = x(0, 1)
y11 = x(1, 1)
………………
End Sub
------解决方案--------------------
'模拟你的c函数
Private Function getArr() As String()
Dim a(3, 2) As String
Dim i As Integer
Dim j As Integer
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
a(i, j) = "www" & i & j
Next j
Next i
getArr = a
End Function
Private Sub CommandButton1_Click()
Dim mya() As String
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
mya = getArr
m = 0
For i = LBound(mya, 1) To UBound(mya, 1)
m = m + 1
n = 0
For j = LBound(mya, 2) To UBound(mya, 2)
n = n + 1
Cells(m, n) = mya(i, j)
Next j
Next i
End Sub
------解决方案--------------------
- VB code
Function GetCode(CodeValue As Variant) As Variant Dim var As Variant var = Func(CodeValue) if isarray(var) then 'var是数组 dim i as long dim j as long for i=lbound(var,1) to ubound(var,1) for j=lbound(var,2) to ubound(var,2) msgbox var(i,j) '编历出数组数据. next next end if '接下来应该怎么操作呢,判断var是否是数组 '获取数组的行数和列数,然后填充单元格 End Sub
------解决方案--------------------
获得数组的维数用这个代码:
- VB code
Function GetArryCount(Temp() As Integer) As Integer Dim i As Integer Dim T As Integer Const ItemCount = 100 On Error GoTo Err1 For i = 0 To ItemCount T = UBound(Temp, i + 1) Next i Err1: GetArryCount = i End Function
------解决方案--------------------