将数组 vb.net 传递给 vc++ 托管 DLL



我想将一维数组 vb.net 传递给 vc++ 托管 DLL,在 Visual Studio 2008.Dll 中创建。在 VB.net 中,在构建期间,它给出了一个错误。

//错误

error BC30657: 'abc' has a return type that is not supported or parameter 
type that are not supported .

我的 Dll 代码 MyCDll.h

#pragma once
using namespace System;
#include "stdafx.h"
namespace MyCDll
{       
public ref class Class1
{
public:
static int abc(int nu[])
{
int i,value=0;
for (i=1;i<5;i++)
{    
if(nu[i])
{
value=i+1;
}
}
//Return the position of the number in the array.
return value;                   
}           
};
}

vb.net 代码:

Imports System.IO
Imports System.Runtime.InteropServices
Module Module1
'Dim result As Integer
Sub Main()
Dim nums() As Integer = New Integer() {1, 2, 3, 4, 5, 6}
'Dim nums() As Integer =  {1, 2, 3, 4, 5, 6}
Dim obj As New MyCDll.Class1

Console.WriteLine(obj.abc(ByVal nums() As Integer)As Integer)
'result = obj.abc(ByVal nums()As Integer)
Console.ReadLine()
End Sub
End Module

托管C++中的数组需要使用托管语法。

static int abc(array<int>^ nu)

您没有正确调用 dll 函数。你编码它的方式会给你编译错误。 您需要像这样传递参数(整数数组(:

obj.abc(nums())

这将返回一个值。因此,您需要获取该值并将其转换为字符串,如下所示,然后打印它:

Console.WriteLine(CStr(obj.abc(nums())))

试试这个

Dim intArray As New List(Of Integer)
intArray.Add(1)
intArray.Add(2)
Dim sResult as string 
sResult = obj.abc(intArray.ToArray())

最新更新