用ATL连接点封装一个由dll导出的回调函数
例子:
一个dll中的一个函数vdb_test实现功能:两个数相加,但结果由回调函数process返回:
dll中的定义:
typedef long (*vdb_callback_proc)(void *param, long total);
VDB_DECLARE(int) vdb_test
( int n1, int n2,
vdb_callback_proc process,
void *param
);
实现:
VDB_DECLARE(int) vdb_test(int n1, int n2, vdb_callback_proc process, void *param)
{
int sum = n1 +n2;
process(param, sum);
return 0;
}
com封装中,用STL的连接点实现回调:idl中回调函数:
dispinterface _IC_callbackEvents
{
properties:
methods:
[id(1), helpstring("method cprocess")] int __cdecl cprocess([in] void* param, [in] long total);
};
test函数实现;STDMETHODIMP CC_callback::test(int n1, int n2, long param, int *ret)
{
void * cparam;
long total = 0;
cparam = (void *)param;
vdb_test(n1, n2, Fire_cprocess, cparam);
return S_OK;
}
我是学习杨老师com应用系列,来做这个例子的,因为工程中的函数要复杂的多,所以做了个小的例子,原理是一致的,出现的问题是,在dll中的vdb_test的第3个参数是用回调函数做的参数,而在com中包装vdb_test时用的Fire_cprocess做参数,不知道思路上实现有没问题?
2)编译时报错:C_callback.cpp(19) : error C2664: 'vdb_test' : cannot convert parameter 3 from 'int (void *,long)' to 'int (__cdecl *)(void *,long)'
None of the functions with this name in scope match the target type
问题和 http://topic.csdn.net/u/20070126/10/4a89e09f-add9-4867-bb9f-f2f676984172.html 出现的问题类似, 就是楼主提到的 "谢谢,杨老师的文章我看过了,我也了解用连接点技术可以实现“客户端实现,服务器端调用”。我现在遇到的问题是,怎么封装我已有的dll导出函数。说得白一点就是,怎么找到一个函数,能够塞到我的get callback方法中。"
我初学com,有说的不明确的地方,请指出 我在线等补充!