可以看到只是简单返回了mycommandhandler对象,mycommandhandler必须继承自idispatch接口来实现支持自动化的调用方式,它是这样实现的:
| class mycommandhandler : public idispatch { long refcount; public: mycommandhandler() :refcount(1){ } virtual hresult stdmethodcalltype gettypeinfocount( /* [out] */ uint *pctinfo){ return s_ok; } virtual hresult stdmethodcalltype gettypeinfo( /* [in] */ uint itinfo, /* [in] */ lcid lcid, /* [out] */ itypeinfo **pptinfo){ return s_ok; } virtual hresult stdmethodcalltype getidsofnames( /* [in] */ refiid riid, /* [size_is][in] */ lpolestr *rgsznames, /* [in] */ uint cnames, /* [in] */ lcid lcid, /* [size_is][out] */ dispid *rgdispid){ *rgdispid=1; return s_ok; } 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){ if(dispidmember==1) { messagebox(0,"hello world","hello",0); //place your code here frmweb->edit1->text="hello world(这也可以?)"; } return s_ok; } virtual hresult stdmethodcalltype queryinterface(refiid classid, void** intf) { if (classid == iid_idispatch) { *intf = (idispatch*)this; addref(); } else return e_nointerface; return s_ok; } virtual ulong stdmethodcalltype addref() { interlockedincrement(&refcount); return refcount; } virtual ulong stdmethodcalltype release() { interlockeddecrement(&refcount); if (refcount == 0) delete this; return refcount; } }; |