我在C++中有這樣的結構,我需要轉換爲C#,所以我可以從一個byte []創建這個結構。c#中正確的C++結構編組#
struct TRANS_RECORD_DATA {
int size, code;
int ThrowItemCount;
int ItemCount;
int ItemSubStart;
int DataSize;
BYTE Data[sizeof(sRECORD_ITEM) * 200]; // sizeof(sRECORD_ITEM) = 548
};
C#版本:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct TRANS_RECORD_DATA {
public int size, code;
public int ThrowItemCount;
public int ItemCount;
public int ItemSubStart;
public int DataSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 548*200)]
public byte[] Data;
};
我使用這個通用的功能給我從字節數組結構:
T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T stuff = (T) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof (T));
handle.Free();
return stuff;
}
但它給我:
AccessViolation異常。
我相信我知道爲什麼,但我不知道如何解決它。 byte[]
我有,需要被映射到結構中,並不總是有大小爲548 * 200的Data
成員。這個數字是最大的。但似乎我使用的GenericMethod,總是嘗試創建該數據總是548 * 200的結構,然後它顯然會拋出一個AccessViolation,因爲要映射的數據已經結束。
例如下面的代碼:
var bytes = new byte[26];
var structure = ByteArrayToStructure<TRANS_RECORD_DATA>(bytes);
應該返回一個TRANS_RECORD_DATA
與所有那些詮釋成員值爲0,最後是byte[] Data
將只有剩餘的兩個字節。 (26 - 24 = 2)。但它似乎一直嘗試創建完整的548 * 200字節[],然後導致訪問衝突。
有沒有辦法解決這個問題?
這當然不行。只需聲明結構*,不包含*數據元素。您可以直接從* bytes [] *參數訪問數據。它從索引24開始。 –