结构指针

时间:2010-01-09 12:44:35

标签: c# wrapper

我想用c#为board sdk编写包装器。 sdk中的函数实现是:

void WINAPI GetSysInfo(TC_INI_TYPE *TmpIni);

TC_INI_TYPE是如下结构:

typedef struct {
    WORD wCardNo;                        
    WORD wCardType;                 
    WORD wConnect;                      
    WORD wIRQ;                      
    char cbDir[LEN_FILEPATH];           
    WORD wAddress[MAX_CARD_NO];     
    WORD wMajorVer;                 
    WORD wMinorVer;                 
    WORD wChType[MAX_CHANNEL_NO];   
} TC_INI_TYPE;

我如何编写结构TC_INI_TYPE的包装器。

1 个答案:

答案 0 :(得分:4)

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 4)]
public struct TC_INI_TYPE
{
   public short wCardNo;
   public short wCardType;
   public short wConnect;
   public short wIRQ;
   [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 32)] // change 32 to LEN_FILEPATH
   public char[] cbDir;
   [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 32)] // change 32 to MAX_CARD_NO
   public short[] wAddress;
   public short wMajorVer;
   public short wMinorVer;
   [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 32)] // change 32 to MAX_CHANNEL_NO
   public short[] wChType;
}

您可能还想根据需要更改Pack值。

对于GetSysInfo,请执行以下操作:

[DllImport("")] 
private static extern void GetSysInfo([In,Out] ref TC_INI_TYPE tcIniType);