本篇文章介绍了如何用ATL写一个简单的COM+组件(Dll)。
1、VC新建工程,ATL->ATL Project(名称为ATLCOMP)->类型选择Dynamic Library(DLL)->勾选Support COM+ 1.0->Finish
2、工程视图,ATLCOMP工程,右键->Add->Class->ATL->ATL COM+ 1.0 Component->类名为JustATestCOMP,ProgID为ATLCOMP.JustATestCOMP
3、切换到类视图,ATLCOMP项目下的IJustATestCOMP接口上右键Add Method
名称:Add
参数1:[in]LONG a
参数2:[in]LONG b
参数3:[out,retval]LONG* c
4、类视图,ATLCOMP项目下的IJustATestCOMP接口上右键Add Method
名称:SayHiTo
参数1:[in]BSTR someOne
参数2:[out,retval]BSTR* retValue
5、打开JustATestCOMP.cpp完成两个函数
STDMETHODIMP CJustATestCOMP::Add(LONG a, LONG b, LONG* c) { // TODO: Add your implementation code here *c = a+b; return S_OK; } STDMETHODIMP CJustATestCOMP::SayHiTo(BSTR someOne, BSTR* retValue) { // TODO: Add your implementation code here CComBSTR sResult("Hi "); CComBSTR sName(someOne); CComBSTR sMark("!"); sResult.AppendBSTR(sName); sResult.AppendBSTR(sMark); *retValue = sResult.Copy(); return S_OK; }
6、编译
7、注册
regsvr32 ATLCOMP.dll
8、反注册
regsvr32 /u ATLCOMP.dll
9、注册到COM+
9.1打开组件管理器
#32位系统32位COM,64位系统64位COM dcomcnfg #64位系统32位COM comexp.msc -32
9.2选择到“Components Services-》Computers-》My Computer->COM+ Applications”
9.3右键“New-》Application(ATLCOMP)”
9.4选择到“Components Services-》Computers-》My Computer->COM+ Applications->ATLCOMP->Components”
9.5右键“New-》Component-》导入已注册的组件-》选择ATLCOMP.JustATestCOMP-》确定”
10、COM+反注册
找到对应的应用或组件,直接删除就好了