2 lv612219 lv612219 于 2016.01.13 16:03 提问

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 files\microsoft visual studio\vc98\atl\include\atlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void ,const struct _GUID &,void
*
)'
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.h(1827) : warning C4259: 'long __stdcall Itest::cli(unsigned char ,unsigned char *,int *)' : pure virtual function was not defined
d:\dll\testcom\testcom.h(80) : see declaration of 'cli'
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void *,const struct _GUID &,void
*
)'
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.h(1827) : error C2259: 'CComObject' : cannot instantiate abstract class due to following members:
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void ,const struct _GUID &,void
*
)'
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.h(1827) : warning C4259: 'long __stdcall Itest::cli(unsigned char ,unsigned char *,int *)' : pure virtual function was not defined
d:\dll\testcom\testcom.h(80) : see declaration of 'cli'
c:\program files\microsoft visual studio\vc98\atl\include\atlcom.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)

3个回答

caozhy
caozhy   Ds   Rxr 2016.01.14 02:51

代码不完整,Func test = (Func)GetProcAddress(hDll, "cli"); 这样是调用标准dll导出函数,不是com函数。com函数要先创建对象,转换成对应的接口才行。
你还得需要一个idl文件来表述接口

一个最简单的例子:
Func test = (Func)GetProcAddress(hDll, "cli");

lv612219
lv612219 接口参数都没用 调用都报错 应该是接口实现的问题
接近 2 年之前 回复
lv612219
lv612219 不知道错哪,大神直接指点下小白吧
接近 2 年之前 回复
lv612219
lv612219 COM接口定义很简单:STDMETHOD(cli)(void);
接近 2 年之前 回复
lv612219
lv612219 感觉是自己实现接口方法有问题: typedef int (*Func)(char*,char*); HMODULE hDll = LoadLibraryW(L"D:\\Dll\\test.dll"); Func test = (Func)GetProcAddress(hDll, "cli"); int i = test( "test1","test2" ) ; FreeLibrary(hDll); return S_OK;
接近 2 年之前 回复
lv612219
lv612219 目前情况用VS建立ATL project,用VS向导方式添加接口和方法,不存在代码不完整的情况了,也能编译通过,但是外部调用COM接口提示内部不能为written.
接近 2 年之前 回复
caozhy
caozhy   Ds   Rxr 2016.01.14 02:52
oyljerry
oyljerry   Ds   Rxr 2016.01.13 18:17

Com接口需要通过CoCreateInstance,QueryInterface来获取接口然后调用

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!