Click here to Skip to main content
15,887,394 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

c++
C#
typedef void(*callback_OnRspAuthenticate)(CThostFtdcRspAuthenticateField*, CThostFtdcRspInfoField*, int, bool);

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

C#
struct CThostFtdcRspInfoField
{
    TThostFtdcErrorIDType   ErrorID;
    TThostFtdcErrorMsgType  ErrorMsg;
};



C++/CLI
C#
public delegate void delegate_OnRspAuthenticate(CThostFtdcRspAuthenticateField^, CThostFtdcRspInfoField^, int, bool);

C#
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;
};

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



Posted
Comments
Sergey Alexandrovich Kryukov 9-Sep-15 4:24am    
There is no such concept in this context, "transform". Also, you cannot do the cast. You need to explain what you want to achieve.
—SA
Member 11862324 9-Sep-15 4:44am    
So how can i pass a struct to a delegate in c++/cli.
the struct is:
public value struct value_test
{
[MarshalAs(UnmanagedType::ByValTStr, SizeConst = 11)]
String^ BrokerID;

[MarshalAs(UnmanagedType::ByValTStr, SizeConst = 16)]
String^ UserID;

System::Char Type;
};
the delegate is :
public delegate void delegate_OnRspAuthenticate(CThostFtdcRspAuthenticateField^, CThostFtdcRspInfoField^, int, bool);

could i explict convert CThostFtdcRspAuthenticateField^ to IntPtr?
Member 11862324 9-Sep-15 4:48am    
i am sorry, i post the wrong code.
my question is how can i pass a struct to a delegate in c++/cli?
The Struct is:
///客户端认证响应
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;
};

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

if i change the delegate to
public delegate void delegate_OnRspAuthenticate(IntPtr, IntPtr, int, bool);
can it work?
if it work, how to convert a handle like "CThostFtdcRspAuthenticateField^" to "IntPtr"?
Thanks!
Philippe Mori 9-Sep-15 8:42am    
As far as I understand your code, there is no direct transformation between those structs as the manged one contains strings for the BrokerID and such while it is another struct (definition not shown) for the unmanaged code. Either, you have to define compatible struct with P/Invoke or do you own conversion code in mixed mode.

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900