当前位置: 代码迷 >> C# >> 关于c#调取C++结构体带回调方法的有关问题
  详细解决方案

关于c#调取C++结构体带回调方法的有关问题

热度:91   发布时间:2016-05-05 03:23:09.0
关于c#调取C++结构体带回调方法的问题!
c++代码:
结构体:
typedef struct _UCS_CALLBACKINTERFACE{ 
void (*onConnectionSuccessful)(); //连接服务器成功回调 
void (*onConnectionFailed)(int reason); //连接服务器失败回调 
}
方法:
//初始化
int Ucs_init( UCS_CALLBACKINTERFACE *CallbackInterface )
//连接服务器
int Ucs_connect(const char *accountSid, const char *accountToken,const char *ClientNumber, const char *ClientPwd);

c#代码:
private void ponConnectionSuccessful(){}
private void ponConnectionFailed(int reason){}
public delegate void onConnectionSuccessfulEventHandler();
public delegate void onConnectionFailedEventHandler(int reason);

[StructLayout(LayoutKind.Sequential)]
unsafe public struct UCS_CALLBACKINTERFACE
{
public onConnectionSuccessfulEventHandler onConnectionSuccessful;
public onConnectionFailedEventHandler onConnectionFailed; 
}
[DllImport("UCSNetService.dll", EntryPoint = "Ucs_init")]
public static extern int Ucs_init(UCS_CALLBACKINTERFACE CallbackInterface);
 [DllImport("UCSNetService.dll", EntryPoint = "Ucs_connect")]
public static extern int Ucs_connect(string accountSid, string accountToken, string ClientNumber, string ClientPwd);

public void test()
{
int i = 0;
try {
onConnectionSuccessfulEventHandler zz = new onConnectionSuccessfulEventHandler(ponConnectionSuccessful);
onConnectionFailedEventHandler ss = new onConnectionFailedEventHandler(ponConnectionFailed);
UCS_CALLBACKINTERFACE uc = new UCS_CALLBACKINTERFACE();
uc.onConnectionSuccessful = zz;
uc.onConnectionFailed = ss;
i = Ucs_init(uc);
i = Ucs_connect(AccountSid, AuthToken, code, mavoike);
}
catch (Exception e) {
}
}
问题 :委托的回调方法ponConnectionSuccessful,ponConnectionFailed没有执行。有懂的朋友吗。帮看看怎么弄!感谢!
------解决思路----------------------
你的定义就不对
c++要传UCS_CALLBACKINTERFACE *
你C#定义为传UCS_CALLBACKINTERFACE
  相关解决方案