在将C风格的结构体转换为.NET对应结构体时候产生很多问题,有如下两个C语言结构体:
typedef struct _wfs_ptr_retract_bins
{
WORD wRetractBin;
USHOR TusRetractCount;
} WFSPTRRETRACTBINS, * LPWFSPTRRETRACTBINS;
typedef struct _wfs_ptr_status
{
WORD fwDevice;
WORD fwMedia;
WORD fwPaper[16];
WORD fwToner;
WORD fwInk;
WORD fwLamp;
LPWFSPTRRETRACTBINS* lppRetractBins;
USHORT usMediaOnStacker;
LPSTR lpszExtra;
} WFSPTRSTATUS, * LPWFSPTRSTATUS;
如何在C#中定义与其相同的结构体?我从非托管DLL获得一个WFSPTRSTATUS类型的指针lpBuffer,通过这个指针解析其中的值赋到C#中定义的结构体。下面是我定义的结构体,但是转化时出错。
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct WFSPTRRETRACTBINS
{
public ushort wRetractBin;
public ushort usRetractCount;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct WFSPTRSTATUS
{
public ushort fwDevice;
public ushort fwMedia;
[MarshalAsAttribute(UnmanagedType.U2,SizeConst = 16)]
public ushort fwPaper;
public ushort fwToner;
public ushort fwInk;
public ushort fwLamp;
public IntPtr lppRetractBins; /// LPWFSPTRRETRACTBINS* 类型
public ushort usMediaOnStacker;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string lpszExtra;
}
各位大侠救命啊!很急很急。
[MarshalAs(UnmanagedType.ByValArray,ArraySubType= UnmanagedType.U2, SizeConst = 16)]
public ushort[] fwPaper;
[MarshalAsAttribute(UnmanagedType.LPSTR,SizeConst = 255)]
public string lpszExtra;