Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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#应用程序中向结构内的指针传递数组_C# - Fatal编程技术网

在C#应用程序中向结构内的指针传递数组

在C#应用程序中向结构内的指针传递数组,c#,C#,我有一个非托管的库。我想使用非托管库创建一个C#应用程序。我正在使用“DllImport”从非托管库导入函数 在C++中,调用结构的结构和函数如下所示: typedef struct _DATA_BUFFER { UCHAR *buffer; // variable length array UINT32 length; // lenght of the array UINT32 transferCount;

我有一个非托管的库。我想使用非托管库创建一个C#应用程序。我正在使用“DllImport”从非托管库导入函数

在C++中,调用结构的结构和函数如下所示:
typedef struct _DATA_BUFFER {

UCHAR *buffer;                // variable length array      
UINT32 length;                 // lenght of the array     
UINT32 transferCount;               

} DATA_BUFFER,*P_DATA_BUFFER;

byte  Write (HANDLE handle, DATA_CONFIG *dataConfig, DATA_BUFFER *writeBuffer, UINT32 Timeout);        
在C#中,我定义了如下所示的结构和功能

[StructLayout(LayoutKind.Sequential, Pack = 1)] 

public unsafe struct DATA_BUFFER
{
public byte* buffer;
public UInt32 length;
public UInt32 transfercount;             
};

[DllImport("Library.dll")]

public unsafe static extern byte Write([In] IntPtr hHandle, DATA_CONFIG* dataConfig,   DATA_BUFFER* WriteBuffer, UInt32 timeout);


for (byte i = 0; i < length; i++)
transfer[i] = i;

DATA_BUFFER buffer_user = new DATA_BUFFER();
buffer_user.length = length;
buffer_user.buffer = transfer;

return_status = Write(handle , &dataconfig , &buffer_user , 1000);
[StructLayout(LayoutKind.Sequential,Pack=1)]
公共不安全结构数据\u缓冲区
{
公共字节*缓冲区;
公共UInt32长度;
公共UInt32转让计数;
};
[DllImport(“Library.dll”)]
公共不安全的静态外部字节写入([In]IntPtr hHandle,DATA_CONFIG*dataConfig,DATA_BUFFER*WriteBuffer,UInt32超时);
用于(字节i=0;i
我收到错误“无法将类型byte[]隐式转换为byte”

我试着用固定的和其他的。什么都没用


如何将数组(在本例中为transfer[])分配给结构中的指针(公共字节*缓冲区)。我需要将其传递给上述函数?

您的结构
数据缓冲区
使用字节*缓冲区。但我想你理解这个错误

试一试


当然,在不安全的
环境中。

哪里会发生这种错误?我在所有代码中没有看到一个字节[]。我假设它不是完整的代码,传输是
byte[]
我的答案对你有用吗?如果您能将其标记为已接受,那就太好了;)
fixed (byte* b = transfer) buffer_user.buffer = b;