问题描述
- VC++ COMM接口问题,本人小白一枚,临时需要COMM,所以求详细指导。
-
本人小白一个,临时需要COM接口用,网上照教程来COM接口编译报错,步骤如下
test.dll导出函数原型如下:
extern "C" __declspec(dllexport) int __stdcall cli (char*,char*);想在VC或VS下用C++包装下这个导出函数为COM接口
目前尝试了VC 6.0环境下添加了ATL COM工程,添加了ATL简单对象并新增方法Ctest如下(通过VC向导):
STDMETHODIMP Ctest::cli(char lp_sServer, char *lp_sCmd, int *ReturnVal)
{
// TODO: Add your implementation code here
typedef int (__stdcall *Func)(char,char*);
HMODULE hDll = LoadLibrary("test.dll");
Func test = (Func)GetProcAddress(hDll, "cli");
if( test( "test1","test2" ) != 0 )
{
return -1;
}
FreeLibrary(hDll);
return S_OK;
}编译报错如下:
cannot instantiate abstract class due to following members:
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void ,const struct _GUID &,void
* )'
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1827) : warning C4259: 'long __stdcall Itest::cli(unsigned char ,unsigned char *,int *)' : pure virtual function was not defined
d:dlltestcomtestcom.h(80) : see declaration of 'cli'
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void *,const struct _GUID &,void
* )'
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1827) : error C2259: 'CComObject' : cannot instantiate abstract class due to following members:
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void ,const struct _GUID &,void
* )'
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1827) : warning C4259: 'long __stdcall Itest::cli(unsigned char ,unsigned char *,int *)' : pure virtual function was not defined
d:dlltestcomtestcom.h(80) : see declaration of 'cli'
c:program filesmicrosoft visual studiovc98atlincludeatlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void *,const struct _GUID &,void
* )'
Error executing cl.exe.TESTCOM.dll - 2 error(s), 2 warning(s)
解决方案
代码不完整,Func test = (Func)GetProcAddress(hDll, "cli"); 这样是调用标准dll导出函数,不是com函数。com函数要先创建对象,转换成对应的接口才行。
你还得需要一个idl文件来表述接口
一个最简单的例子:
Func test = (Func)GetProcAddress(hDll, "cli");
解决方案二:
链接没复制上
http://stackoverflow.com/questions/2187425/how-do-i-use-a-com-dll-with-loadlibrary-in-c
解决方案三:
Com接口需要通过CoCreateInstance,QueryInterface来获取接口然后调用