簡體   English   中英

在C#應用程序中將數組傳遞給結構內的指針

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM