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

问题描述:

我得到VB错误:“类型'Ushort'不能转换为'Ushort()'“。我有一个VB.NET窗口应用程序调用本地(C++)DLL中的函数,从DLL中的页面数组中读取特定的256字节页面。VB.NET错误“类型'Ushort'的值不能转换为'Ushort()'”从本地C++ DLL读取字节的二维数组

VISUAL-C++的DLL源内声明,函数的....

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缓冲宣言持有64页,每页256个字节每个从DLL ...............

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

+0

解决了构建问题。现在我将测试...... –

+2

考虑到这些事实:** 1)**该参数称为'buf'(可能是_buffer_的简称)** 2)**该参数被转换为一个'unsigned char * '(通常映射到一个字节数组)** 3)**函数内部调用另一个称为_BDLL \ _Read ** Block ** _ - ...的函数我认为参数应该至少是某种排列的数组。如果不得不猜测'BDLL_ReadBlock()'用从(无论)中读取的字节填充'buf'“数组”。 –

正确地读取问题后(:))我想我明白你在做什么...... C++函数需要一个参数为buf的字节数组,然后用BDLL_ReadBlock()填充256字节功能。

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

无论如何,要解决这个问题,你将不得不从二维数组切换到jagged array。这是因为他们做不同的事情:

  • 在一个二维数组,你必须在网格布局,很像一个固定的坐标系中的所有项目:
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)(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} 
    

当访问项目是在一个二维数组,你只能得到差一项在一个时间:

Array(1, 3) = H 

...而在交错数组,你不必访问只有一个项目,但你也可以重新ference一整行:

'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阵列更改为锯齿状一个,而不是一个二维数组:

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++代码的来源,然后只需更换我的代码中每Byte(...)UShort(...),你应该是好去!