Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将字节数组传递到C++;vb.net中的动态链接库_Vb.net - Fatal编程技术网

如何将字节数组传递到C++;vb.net中的动态链接库

如何将字节数组传递到C++;vb.net中的动态链接库,vb.net,Vb.net,我需要从我的vb.net应用程序调用此C DLL函数: HRESULT WINAPI MyTestFunc(BYTE *ByteDef ); Parameters ByteDef The length of this array is variable. ByteDef[0] Range from 3 to 5. ByteDef[1] Range from 1 to 8. ByteDef[2] Range from 1 to 15. ByteDef[3] Range from 1 to 8. I

我需要从我的vb.net应用程序调用此C DLL函数:

HRESULT WINAPI MyTestFunc(BYTE *ByteDef );
Parameters
ByteDef The length of this array is variable.
ByteDef[0] Range from 3 to 5.
ByteDef[1] Range from 1 to 8.
ByteDef[2] Range from 1 to 15.
ByteDef[3] Range from 1 to 8. It must be 2 (8 bit data).
第一个问题是如何在vb.net应用程序中定义此函数? 我尝试了以下方法:

Declare Function MyTestFunc Lib "xxx.dll" (ByVal ByteDef As Byte()) As Integer
第二个问题是如何将barcodedef参数传递给它? 我试过的方法如下:

ByteDef = System.Text.Encoding.Default.GetBytes("3422", 0, 4)
Result = MyTestFunc(ByteDef)

该函数返回一条错误消息,说明条形码定义无效。

您的问题是这一行:

ByteDef = System.Text.Encoding.Default.GetBytes("3422", 0, 4)
无法将字符串转换为那样的字节数组,因为数字字符与实际数字不同。例如,字符
“3”
实际上以字节形式由
51
表示

对生成的阵列进行简单验证后,您会发现:

最后,如果
您要求字符串保存特定的字节,则应从不将字符串转换为字节数组。字符串是字符串,字节数组是字节数组。把它们分开,除非你有很好的理由转换成一个或另一个

在您的情况下,解决方案很简单,只需使用所需的字节初始化一个新的字节数组:

ByteDef = New Byte() {3, 4, 2, 2}

尝试将参数更改为Byte@JimHewitt:ByVal ByteDef,这样它就不再是字节数组了。将
BYTE*
映射到
BYTE()
非常好。实际上我的意思是
ByRef-ByteDef作为BYTE
。声明很好。你应该如何初始化数组,这是任何人的猜测。当然不能使用Encoding.GetBytes()。如果手册没有意义,那么就用电话联系作者或供应商。@JimHewitt:这仍然不能使它成为一个字节数组,这是OP需要的。正如我(和汉斯)所说,
ByVal-ByteDef-As-Byte()
非常好。