tags:

views:

58

answers:

1
+3  Q: 

structure pointer

hello every one. i want to write wrapper for a board sdk with c#. the function implementation in sdk is :

void WINAPI GetSysInfo(TC_INI_TYPE *TmpIni);

that TC_INI_TYPE is a structure as below:

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;

how i can write wrapper for structure TC_INI_TYPE.

+4  A: 
[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;
}

You might want to also change the Pack value based on what you need.

For GetSysInfo, do the following:

[DllImport("")] 
private static extern void GetSysInfo([In,Out] ref TC_INI_TYPE tcIniType); 
SwDevMan81
Looks good. Don't forget the GetSysInfo declaration.
Hans Passant
Not sure about the alignment on a 64-bit system, though. I think it's aligned on 8-byte boundaries.
codekaizen