2010-01-09 233 views
3

我想用c#編寫一個板子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

回答

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; 
} 

您可能還想根據需要更改包值。

對於GetSysInfo,請執行下列操作:

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

看起來不錯。不要忘記GetSysInfo聲明。 – 2010-01-09 15:34:41

+0

雖然不確定64位系統上的對齊方式。我認爲它是以8字節的邊界對齊的。 – codekaizen 2010-01-09 15:51:27