[英]Passing an Array to Pointer inside a Structure in C# application
I am having a unmanaged library . 我的图书馆不受管理。 I want to create a C# application using the unmanaged library.
我想使用非托管库创建一个C#应用程序。 I am using 'DllImport' to import the functions from the unmanaged library.
我正在使用'DllImport'从非托管库导入函数。
In C++ the structure and the function calling the structure looks as shown below 在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);
In C# I defined the structure and function as shown below 在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);
I am getting error 'Cannot implicity convert type byte[] to byte . 我收到错误消息“无法将类型byte []隐式转换为byte。
I tried using fixed and other . 我尝试使用fixed和other。 Nothing is working .
什么都没用。
How do I assign an array (In this case Tranfer[]) to the pointer (public byte* buffer ) in the structure . 如何将数组(在这种情况下为Tranfer [])分配给结构中的指针(公共字节*缓冲区)。 I need to pass it to above mentioned function?
我需要将其传递给上述功能吗?
Your struct DATA_BUFFER
uses a byte* buffer. 您的结构
DATA_BUFFER
使用字节*缓冲区。 But I guess you understand the error. 但是我想你理解错误了。
Try 尝试
fixed (byte* b = transfer) buffer_user.buffer = b;
inside an unsafe
context of course. 在
unsafe
环境中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.