繁体   English   中英

VB.NET错误“无法将类型'Ushort'的值转换为'Ushort()'”,从本地C ++ DLL中读取二维字节数组

[英]VB.NET error “Value of type 'Ushort' cannot be converted to 'Ushort()'” reading two-dimensional array of bytes from a native C++ DLL

我收到VB错误:“类型'Ushort'的值不能转换为'Ushort()'”。

我有一个VB.NET Windows应用程序,该应用程序在本机(C ++)DLL中调用一个函数,以从DLL中的页面数组中读取特定的256字节页面。

函数的DLL源中的VISUAL-C ++声明。

extern "C" BASICDLL_API int __stdcall My_Read_High_Speed_Data(unsigned char ptr, unsigned short *buf)
{
    return BDLL_ReadBlock(0x00300000 + ptr, (unsigned char *)buf);
}

VB.NET DLL中函数的声明……。

<DllImport("MyDll.dll", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)>
Public Function My_Read_Parameters(ByVal board As Byte, ByVal params As UShort()) As Int32
End Function

声明VB.NET缓冲区以容纳DLL中每个64页的256个字节的页面

Dim input_page_buffer( 64, 256 ) As UInt16

VB.NET函数从DLL读取页面...................

function poll()                         
    dim page_index = 1
    dim success = My_Read_High_Speed_Data( page_index, input_page_buffer(1, 1)  )
end function

dll导入功能

..., ByVal params As UShort()

要求UShort()作为第二个参数,而c ++具有

..., unsigned short *buf)

将dll导入更改为UShort

正确阅读了问题( :)之后,我想我理解您正在尝试做的事情... C ++函数期望其buf参数有一个字节数组,然后BDLL_ReadBlock()函数将其填充256个字节。

为什么您将参数声明为unsigned short*而不是unsigned byte*我不明白,因为BDLL_ReadBlock()显然期望后者。

无论如何,要解决此问题,您必须从二维数组切换为锯齿数组 这是因为他们做不同的事情:

  • 在二维数组中,您将所有项目布置在一个网格中,非常类似于固定坐标系:
    Array(3, 3):
        0  1  2  3
     0  A  B  C  D
     1  E  F  G  H
     2  I  J  K  L
     3  M  N  O  P
  • 但是, 锯齿状的数组arrays的数组 ,这意味着您具有一组列和行,其中列的每一行不一定必须具有相同的数量:

     Array(3)(3): 0 1 2 3 0 {A, B, C, D} 1 {E, F, G, H} 2 {I, J, K, L} 3 {M, N, O, P} Array(3)(x): 0 1 2 3 4 0 {A, B} 1 {C, D, E, F, G} 2 {H, I, J} 3 {K} 

访问项目的区别在于,在2D数组中,一次只能获得一个项目

Array(1, 3) = H

...而在锯齿状数组中,您不必访问一项 ,但是您也可以引用整行:

'Single item.
Array(1)(3) = H

'Entire row.
Array(1) = {E, F, G, H}

继续...

由于BDLL_ReadBlock()似乎期望一个字节数组,因此您应该将C ++方法也更改为采用一个字节数组(如果可以访问源,则是这样):

extern "C" BASICDLL_API int __stdcall My_Read_High_Speed_Data(unsigned char ptr, unsigned char *buf)
{
    return BDLL_ReadBlock(0x00300000 + ptr, buf);
}

然后更改VB.NET声明:

<DllImport("MyDll.dll", CallingConvention:=CallingConvention.StdCall)>
Public Function My_Read_High_Speed_Data(ByVal ptr As Byte, ByVal buf As Byte()) As Integer
End Function

最后,将您的VB.NET数组更改为锯齿状数组,而不是2D数组:

Dim input_page_buffer(64 - 1)() As Byte
'In VB.NET you specify the upper bound INDEX of an array, not how many items it should contain.
'Thus "Dim Array(64)" creates an array of 65 items (since indexes are zero-based), and "Dim Array(64 - 1)" or "Dim Array(63)" creates an array of 64 items.

唯一的缺点是必须分别初始化锯齿数组的内部数组:

For x = 0 To input_page_buffer.Length - 1
    input_page_buffer(x) = New Byte(256 - 1) {}
Next

最后 ,调用函数:

Function poll()
    Dim page_index As Byte = 1

    'input_page_buffer(0) gives you the first 256-byte array.
    Dim success As Integer = My_Read_High_Speed_Data(page_index, input_page_buffer(0))

    ...
End Function

如果您不能更改C ++代码的源代码,那么只需用UShort(...)替换我代码中的每个Byte(...) UShort(...) ,您就应该做好了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM