Vb.net调用C写的Dll含结构体指针参数的方法

时间:2022-10-05 13:59:47

VC Dll

#define NAME_LEN_MAX64

typedef
struct
{
  UINT uVendorID;
// 厂商ID
  char acVendorName[NAME_LEN_MAX]; // 厂商名称
  char acProductSeries[NAME_LEN_MAX]; // 产品系列
  char acProductName[NAME_LEN_MAX]; // 产品名称
  char acFriendlyName[NAME_LEN_MAX]; // 昵称,多个同类设备,后加端口号区分
  char acDevFileName[NAME_LEN_MAX]; // 驱动dll名称
  char acFileName[NAME_LEN_MAX]; // 内核驱动逻辑设备名
  char acFirmwareVersion[NAME_LEN_MAX]; // Firmware版本
  char acSensorType[NAME_LEN_MAX]; // sensor类型
  char acPortType[NAME_LEN_MAX]; // 接口类型
} tDSCameraDevInfo;

CameraGetDevList(tDSCameraDevInfo
*pDSCameraList, INT *piNums);

在Vb.net中使用

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> _Public Structure tDSCameraDevInfo
Dim iVendorID As Integer ' 厂商ID
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acVendorName() As Byte '厂商名称
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acProductSeries() As Byte ' 产品系列
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acProductName() As Byte '产品名称
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acFriendlyName() As Byte '昵称,多个同类设备,后加端口号区分
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acDevFileName() As Byte '驱动dll名称
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acFileName() As Byte '内核驱动逻辑设备名
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acFirmwareVersion() As Byte 'Firmware版本
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acSensorType() As Byte 'sensor类型
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> _
Dim acPortType() As Byte
End Structure

Public Declare Function GetDevList Lib "XXX.dll" (ByRef pDSCameraList As tDSCameraDevInfo, ByRef piNums As Integer) As emDSCameraStatus

调用时,

Dim m_asCamDevInfo(50) As tDSCameraDevInfo

Dim m_iNum As Integer

GetDevList(m_asCamDevInfo(
0), m_iNum)

但是这样调用后,结构体中的每个成员都是Byte型的,因此还需要通过下面方法将它转换一下。

System.Text.UTF8Encoding.UTF8.GetString(m_asCamDevInfo(0).acProductName)

如此,完成调用。