【问题标题】:How to parse struct to C++ dll from C#?如何将结构从 C# 解析为 C++ dll?
【发布时间】:2010-12-23 22:54:50
【问题描述】:

我正在尝试调用非托管 C++ dll 中的函数。

它有这个原型:

  [DllImport("C:\\Program Files\\MySDK\\VSeries.dll", EntryPoint = "BII_Send_Index_Template_MT" )]
internal unsafe static extern Int32 BII_Send_Index_Template_MT(IntPtr pUnitHandle, ref BII_Template template, Int32 option, Boolean async);

  BII_Template template = new BII_Template();
  error_code = BII_Send_Index_Template_MT(pUnitHandle, ref template, option, false);

这就是我在 C# 中定义 BII_Template 结构的方式:

public unsafe struct BII_Template {
public ulong id;    
public ulong employee_id;  
public ulong password; 

public byte sensor_version;  
public byte template_version; 
public fixed char name[16];   
public byte finger;  
public byte admin_level;  
public byte schedule;  
public byte security_thresh; 
public fixed byte noise_level[18];   
public byte corramb;
public byte reference_x;
public byte reference_y;
public fixed byte ihcore[3];   
public fixed byte ivcore[3]; 
public byte temp_xoffset;  
public byte temp_yoffset;  
public byte index;    
public fixed byte inphase[5500]; 
};

它会构建,当我运行它时,dll 返回 error_code = "记录校验和无效。"

我假设我以错误的方式使用ref,或者结构中某些元素的大小错误。

----- 编辑 ------------

这是 C++ 中的结构:

typedef struct {
unsigned long   id;     
unsigned long   employee_id;    
unsigned long   password;   
unsigned char   sensor_version;     
unsigned char   template_version;   
char        name[16];   
unsigned char   finger;     
unsigned char   admin_level;        
unsigned char   schedule;           
unsigned char   security_thresh;    
unsigned char   noise_level[18];    
unsigned char   corramb ;           
unsigned char   reference_x ;
unsigned char   reference_y ;
unsigned char   ihcore[NUM_CORE];
unsigned char   ivcore[NUM_CORE];   
unsigned char   temp_xoffset;       
unsigned char   temp_yoffset;       
unsigned char   index;              
unsigned char   inphase[PACKED_ARRAY_SIZE]; 
} BII_Template;

【问题讨论】:

  • 在 C++ 中查看匹配结构的声明可能会很有用。
  • 顺便说一句,我没有看到初始化代码,你初始化了吗?
  • 如果有被调用函数的代码,可以调试单步执行,看参数是否正确传递
  • 我没有dll的源代码。只有 h 个文件。
  • BII_Send_Index_Template_MT 的 C++ 声明会很有用

标签: c# dll struct unmanaged ref


【解决方案1】:

经过大量测试,发现使用固定字符 ... 与 [MashalAs...] 的效果不同。进行此更改后,它起作用了。

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

公共结构 BII_Template { 公共单位 ID; 公共单位employee_id; 公共单位密码;

public byte sensor_version;
public byte template_version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] name;
public byte finger;

public byte admin_level;
public byte schedule;
public byte security_thresh;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
public byte[] noise_level;
public byte corramb;
public byte reference_x;
public byte reference_y;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ihcore;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ivcore;

public byte temp_xoffset;
public byte temp_yoffset;
public byte index;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5500)]
public byte[] inphase;

};

【讨论】:

    【解决方案2】:

    C++ 中的'unsigned long' 不等于 C# 中的 ulong。

    改用 uint。

    【讨论】:

      【解决方案3】:

      我的标准答案是:将 C++/CLI 程序集添加到您的 C# 项目中。那么问题就会自行解决。

      【讨论】:

        【解决方案4】:

        你需要在 BII_Template 上使用 structlayout.sequal 属性,详情请谷歌。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 2014-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 2012-12-01
        相关资源
        最近更新 更多