错误C3699和c ++ struct和c ++ / cli struct之间的转换 [英] Error C3699 and Transform between c++ struct and c++/cli struct

查看:167
本文介绍了错误C3699和c ++ struct和c ++ / cli struct之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



c ++

 typedef  void (* callback_OnRspAuthenticate )(CThostFtdcRspAuthenticateField *,CThostFtdcRspInfoField *, int  bool ); 



  struct  CThostFtdcRspAuthenticateField 
{
TThostFtdcBrokerIDType BrokerID;
TThostFtdcUserIDType UserID;
TThostFtdcProductInfoType UserProductInfo;
};



  struct  CThostFtdcRspInfoField 
{
TThostFtdcErrorIDType ErrorID;
TThostFtdcErrorMsgType ErrorMsg;
};







C ++ / CLI

  public   delegate   void  delegate_OnRspAuthenticate(CThostFtdcRspAuthenticateField ^,CThostFtdcRspInfoField ^, int  bool ); 



  public   value   struct  CThostFtdcRspAuthenticateField 
{
[MarshalAs(UnmanagedType :: ByValTStr,SizeConst = 11 )]
String ^ BrokerID;
[MarshalAs(UnmanagedType :: ByValTStr,SizeConst = 16 )]
String ^ UserID;
[MarshalAs(UnmanagedType :: ByValTStr,SizeConst = 11 )]
String ^ UserProductInfo;
};



  public   value   struct  CThostFtdcRspInfoField 
{
Int32 ErrorID ;
[MarshalAs(UnmanagedType :: ByValTStr,SizeConst = 81 )]
String ^ ErrorMsg;
};







解决方案

您可以创建一个新的托管CLI(ref)结构,将其传递给委托,该委托可以与您的非托管结构在语义上等效。您需要有一些绑定代码,它们应该将非托管结构克隆到托管结构。你必须按成员递归地执行此成员。字符串应该从C ++或C风格的字符串克隆到.NET字符串,这可以通过类 System.Runtime.InteropServices.Marshal 来完成。



您可以在目标CLI类型上没有任何.NET属性的情况下执行此操作,因为您可以在克隆方法中显式克隆,这将特定于您的输入和输出类型。



我还建议重新考虑将混合模式项目分解为托管和非托管部分,包括将整个项目迁移到托管的选项。但是,我不能提出任何建议,因为我不知道你的非托管部分的作用(性能,与一些非托管子系统的兼容性等)



-SA


c++

typedef void(*callback_OnRspAuthenticate)(CThostFtdcRspAuthenticateField*, CThostFtdcRspInfoField*, int, bool);


struct CThostFtdcRspAuthenticateField
{
    TThostFtdcBrokerIDType  BrokerID;
    TThostFtdcUserIDType    UserID;
    TThostFtdcProductInfoType   UserProductInfo;
};


struct CThostFtdcRspInfoField
{
    TThostFtdcErrorIDType   ErrorID;
    TThostFtdcErrorMsgType  ErrorMsg;
};




C++/CLI

public delegate void delegate_OnRspAuthenticate(CThostFtdcRspAuthenticateField^, CThostFtdcRspInfoField^, int, bool);


public value struct CThostFtdcRspAuthenticateField
{
      [MarshalAs(UnmanagedType::ByValTStr, SizeConst = 11)]
      String^ BrokerID;
      [MarshalAs(UnmanagedType::ByValTStr, SizeConst = 16)]
      String^ UserID;
      [MarshalAs(UnmanagedType::ByValTStr, SizeConst = 11)]
      String^ UserProductInfo;
};


public value struct CThostFtdcRspInfoField
{
      Int32	ErrorID;
      [MarshalAs(UnmanagedType::ByValTStr, SizeConst = 81)]
      String^	ErrorMsg;
};




解决方案

You can create a new managed CLI ("ref") structure to pass it to a delegate, which can be "semantically equivalent" to your unmanaged structure. You need to have some binding code which should clone the unmanaged structure to managed one. You have to do this member by member, recursively. Strings should be cloned from C++ or C-style strings to .NET strings, which can be done through the class System.Runtime.InteropServices.Marshal.

You can do it without any .NET attributes on target CLI type, because you can do the cloning explicitly in your clone method, which will be specific for your input and output types.

I also would suggest to rethink decomposition of your mixed-mode project into managed and unmanaged parts, including the option to migrate the whole thing to the managed. However, I cannot advise anything certain, because I don't know the role of your unmanaged part (performance, compatibility with some unmanaged sub-system, etc.)

—SA


这篇关于错误C3699和c ++ struct和c ++ / cli struct之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆