//################################################ // 必须要注意的是: // (1) 必须要用 #import "XXXX/HelloWorld.dll", 此时彼编译,编译器会生成 // helloworld.tlh, helloworld.tli // // (2) 必须要设置命名空间 using namespace HelloWorldLib; //################################################ // 必须要实现下面的接口 class coCallBack: public IDispatch { public: virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount( /* [out] */ UINT *pctinfo) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( /* [in] */ UINT iTInfo, /* [in] */ LCID lcid, /* [out] */ ITypeInfo **ppTInfo) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( /* [in] */ REFIID riid, /* [size_is][in] */ LPOLESTR *rgszNames, /* [in] */ UINT cNames, /* [in] */ LCID lcid, /* [size_is][out] */ DISPID *rgDispId) { return E_NOTIMPL; } virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke( /* [in] */ DISPID dispIdMember, /* [in] */ REFIID riid, /* [in] */ LCID lcid, /* [in] */ WORD wFlags, /* [out][in] */ DISPPARAMS *pDispParams, /* [out] */ VARIANT *pVarResult, /* [out] */ EXCEPINFO *pExcepInfo, /* [out] */ UINT *puArgErr) { MessageBox(NULL, "1111111", "2222222", MB_OK); return S_OK; } STDMETHOD_(ULONG, AddRef)() { return E_NOTIMPL; } STDMETHOD_(ULONG, Release)() { return E_NOTIMPL; } STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject) { *ppvObject = this; //调试发现 return S_OK; } }; coCallBack gpcallback; int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(NULL); // 获DLL中接口类的对象 IhelloATLControlPtr lcmHelloObj; HRESULT hRt = lcmHelloObj.CreateInstance( __uuidof(helloATLControl) ); if (hRt == S_OK) { // 执行导出函数功能 lcmHelloObj->ExportFun1(); } // 把本程序的回调类, 添加到 COM 中的连接容器中(vect 中),COM 中调用容器中的所有类回调函数,通知Client IConnectionPointContainer *lpConnectionPointContainer = NULL; lcmHelloObj.QueryInterface(IID_IConnectionPointContainer, (void **)&lpConnectionPointContainer ); if(lpConnectionPointContainer) { IConnectionPoint *lpConnectionPoint=NULL; lpConnectionPointContainer->FindConnectionPoint ( __uuidof(_IhelloATLControlEvents), &lpConnectionPoint); lpConnectionPointContainer->Release(); lpConnectionPointContainer = NULL; if (lpConnectionPoint) { DWORD ldwCookie=0; // 添加到COM中的类容器中 lpConnectionPoint->Advise( &gpcallback, &ldwCookie); } } lcmHelloObj->ExportFun1(); lcmHelloObj = NULL; CoUninitialize(); return 0; }