[英]Passing an Array to Pointer inside a Structure in C# application
我的图书馆不受管理。 我想使用非托管库创建一个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);
我收到错误消息“无法将类型byte []隐式转换为byte。
我尝试使用fixed和other。 什么都没用。
如何将数组(在这种情况下为Tranfer [])分配给结构中的指针(公共字节*缓冲区)。 我需要将其传递给上述功能吗?
您的结构DATA_BUFFER
使用字节*缓冲区。 但是我想你理解错误了。
尝试
fixed (byte* b = transfer) buffer_user.buffer = b;
在unsafe
环境中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.