vb2010~傳"陣列"給C原生dll使用,傳址呼叫使用方法
注:vb 的 二維陣列宣告AA(x,y) 本質上與C 宣告二維陣列 AA[x][y] 是一樣的, 實質上都是連續的串接一維陣列!!
=============
Part A. (由C/C++ 產生dll , 非COM元件型動態dll)
=============
// dll.cpp : 定義 DLL 應用程式的匯出函式。
#include "stdafx.h"
#include
#include
//using namespace std;
extern "C"
{
__declspec(dllexport) void add(int a ,int b , float &c);
__declspec(dllexport) void add_array(int* data , int *c,int *d );
__declspec(dllexport) void add_array2D(int* data , int *c,int *d, int *e);
}
__declspec(dllexport) void add_array2D(int* data , int *c,int *d ,int *e)
{
*c= data[0];
*d= data[1];
*e= data[2];
}
__declspec(dllexport) void add_array(int* data , int *c,int *d)
{
*c= data[0];
*d= data[1];
}
__declspec(dllexport) void add(int a ,int b , float &c)
{
c= (float) a+b;
}
//==========================
PartB . (由 vb2010 重新引用 dll, 並對應傳址呼叫 傳陣列進出dll)
//================================
Imports System.Runtime.InteropServices
Public Class Form1
Public Declare Auto Sub ADD Lib "dll" Alias "add" (ByVal a As Integer, ByVal b As Integer, ByRef c As Single)
'__declspec(dllexport) void add_array(int* data , int *c,int *d );
Public Declare Auto Sub ADD_array Lib "dll" Alias "add_array" (ByRef data As Int32, ByRef c As Int32, ByRef d As
Int32)
'__declspec(dllexport) void add_array2D(int* data[] , int *c,int *d)
Public Declare Auto Sub ADD_array2D Lib "dll" Alias "add_array2D" (ByRef data As Int32, ByRef c As Int32, ByRef
d As Int32, ByRef e As Int32)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As Int32 = 0
Dim d As Int32 = 0
Dim ee As Int32 = 0
'ADD(11, 22, cc)
Dim data As Int32() = {4, 8}
Dim data2D(,) As Int32 =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
'data2D(0, 0) = 1
'data2D(0, 1) = 2
'data2D(0, 2) = 3
'data2D(1, 0) = 4
'data2D(1, 1) = 5
'data2D(1, 2) = 6
'data2D(2, 0) = 7
'data2D(2, 1) = 8
'data2D(2, 2) = 9
' ADD_array(data(0), c, d) '------- ok
ADD_array2D(data2D(0, 0), c, d, ee) '------------ok
MsgBox("c=" + c.ToString + "d=" + d.ToString + "e=" + ee.ToString)
End Sub
End Class