日期:2014-05-17  浏览次数:21029 次

C#调用C++/CLI本地代码的回调函数
c++ dll
CClientGlobal.h
class __declspec(dllexport) CClientGlobal
{
public:
typedef int (*pMsgHandleResponseFunc)(CMsg*,vector<CMsg*>&);
typedef int (*pRequestTimeOutFunc)(int _CMDID);
void Set_Response(pMsgHandleResponseFunc fp){};
void Set_Timeout(pRequestTimeOutFunc fp){};
void Set_Response(void* p){};
void Set_Timeout(void* p){};
}

c++/cli wrapper 库
native head
CClientGlobal.h
class __declspec(dllexport) CClientGlobal
{
public:
typedef int (*pMsgHandleResponseFunc)(CMsg*,vector<CMsg*>&);
typedef int (*pRequestTimeOutFunc)(int _CMDID);
void Set_Response(pMsgHandleResponseFunc fp){};
void Set_Timeout(pRequestTimeOutFunc fp){};
void Set_Response(void* p){};
void Set_Timeout(void* p){};

}

wrapper head
public ref class CClientGlobalWrapper
{
CClientGlobal* NativeClientGlobal;
public:
CClientGlobalWrapper(void);
~CClientGlobalWrapper();
public: //消息发送

typedef int (*pMsgHandleResponseFunc)(CMsg*,vector<CMsg*>&);
typedef int (*pRequestTimeOutFunc)(int _CMDID);


void Set_Response(void* fp);
void Set_Response(pMsgHandleResponseFunc fp);


void Set_Timeout(void* fp);
void Set_Timeout(pRequestTimeOutFunc fp);
};



我没有使用显示声明dll函数的方式,而是在C#中添加C++/cli封装的dll, 
CClientGlobalWrapper cgw = new CClientGlobalWrapper();
pMsgHandleResponseFunc msgh = new pMsgHandleResponseFunc(MsgHandle);
cgw.Set_Response() 方法只能看到void*的参数,而看不到函数指针的参数


有没有兄弟有这方面经验的,请帮忙解决一下

------解决方案--------------------
显然这个C++/CLR(不是cli)的封装有错误,既然你要提供C#调用,那么输入输出参数就必须是C#中可以识别的类型,但是你这里却用了非托管类型作为参数,那么虽然能够看到函数,但是调用不能。C++/CLR是可以同时识别非托管和托管类型的,且转换方便,那么你所做的事情就是在C++里面定义一个托管类型的pMsgHandleResponseFunc和pRequestTimeOutFunc,然后自己做转换。
------解决方案--------------------
探讨

显然这个C++/CLR(不是cli)……