发布编组C#结构以调用C .DLL

时间:2012-03-20 20:25:07

标签: c# marshalling

我很难编组我在C#程序中定义的结构,这是调用非托管C .DLL文件所必需的,我无法访问源代码。示例非托管C程序C程序可以调用此.DLL而不会出现问题。问题结构是下面的fa_keylist。我遇到问题的结构中包含多个子结构:

来自C头文件:

struct fa_keypart {
    short kp_start;                      
    short kp_leng;                       
    long  kp_flags;                    
};


struct fa_keydesc {
    long  k_flags;                       
    long  k_nparts;                      
    struct fa_keypart k_part [FA_NPARTS];                                        
};


struct fa_keylist {
    long  kl_nkeys;                     
    char  kl_reserve[4];                
    struct fa_keydesc *kl_key [FA_NKEYS];
}

在C#中,我把它定义为:

    [StructLayout(LayoutKind.Sequential)]
    public struct fa_keypart
    {
        public Int16 kp_start;                            
        public Int16 kp_leng;                                       
        public Int32 kp_flags;                      
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct fa_keydesc
    {
        public Int32 k_flags;                            
        public Int32 k_nparts;                                
        [MarshalAs(UnmanagedType.ByValArray)]
        public fa_keypart[] kparts;
    };


    [StructLayout(LayoutKind.Sequential)]
    public struct fa_keylist
    {
        public Int32 kl_nkeys;                                  
        public UInt32 kl_reserve;                              
        [MarshalAs(UnmanagedType.ByValArray)]
        public fa_keydesc[] kl_keys;    
    }

实际调用的DLLIMPORT签名定义为:

    [STAThread]
    [DllImport("F4AGFCFA.dll", EntryPoint = "cobfa_open", CallingConvention = CallingConvention.StdCall)]
    public static extern Int32 cobfa_open(
                        string fileName,
                        Int32 openFlags,
                        ref fa_keylist keyList,
                        Int32 recordLength);

对函数的调用编码为:

handle = cobfa_open(filename, fileFlags, ref keyList, 80);

顺便说一下,我尝试了很多不同的编组选项。我收到的当前错误是访问冲突(尝试读取或写入受保护的内存)。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要指定数组的大小。假设C中的FA_NPARTS为128,您可以执行以下操作:

[StructLayout(LayoutKind.Sequential)]
public struct fa_keydesc
{
    public Int32 k_flags;                            
    public Int32 k_nparts;                                
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
    public fa_keypart[] kparts;
};

UnmanagedType.ByValArray仅适用于SizeConst设置。