使用C#中的结构数组调用非托管C ++代码

时间:2013-08-02 10:33:55

标签: c# pinvoke marshalling interopservices

以下是该函数的C ++声明:

unsigned moduleDescription (unsigned modulnumber, char * const name, t_c_pindescription * const p_inputs, t_c_pindescription * const p_outputs, t_c_pindescription * const p_intern);

以下是我的C ++结构定义:

#pragma pack(push,4)    
typedef struct     
{     
    char name[256];     
    char dim[64];     
    enum SigTypetype;     
} t_c_pindescription;

typdef enum SigType     
{
    T_ANALOG = 1,
    T_BINARY = 2,
    T_OPTIONAL
};     

#pragma pack(pop)     

以下是我的C#客户端代码:

public class Program     
{     
    [StructLayoutAttribute(LayoutKind.Sequential,CharSet = CharSet.Unicode)]     
    public struct t_c_pindescription     
    {     
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 256)]     
        public string pinname;     

        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]     
        public string Dimension;     

        public SigType mType;     
    }   

    public enumSigType
    {       
        T_ANALOG = 1,     
        T_BINARY = 2,     
        T_OPTIONAL = 4,     
    }

    [System.Runtime.InteropServices.DllImportAttribute("abc.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "moduleDescription",CharSet=CharSet.Ansi)]         
    public static extern int moduleDescription(int modulnumber,StringBuilder name,[Out] t_c_pindescription[] ppsainputs, [Out] t_c_pindescription[] ppsaoutputs, [Out] t_c_pindescription[] ppsaintern);   

    public static void Main(string[] args)     
    {     
        t_c_pindescription[] inputdesarray = new t_c_pindescription[200];     
        t_c_pindescription[] outputdesarray = new t_c_pindescription[200];     
        t_c_pindescription[] interndesarray = new t _c_pindescription[200];     
        StringBuilder sb = newStringBuilder();

        for(int i = 0; i < 10; i++)     
        {     
            moduleDescription(i, sb, inputdesarray, outputdesarray, interndesarray);     
        }    
    }     
}

这里我无法得到我的结构数组值,即inputdesarray,outputdesarray和interndesarray中的值。

0 个答案:

没有答案