//###################################### ######### <Br/> // note the following: <br/> // (1) You must use # import "xxxx/helloworld. DLL ". In this case, the compiler will generate <br/> // helloworld. tlh, helloworld. TLI <br/> // (2) The namespace using namespace helloworldlib must be set; <br/> //################################## ############# </P> <p> // The following interface must be implemented <br/> class cocallback: public idispatch <br/>{< br/> Public: <br/> virtual hresult stdmethodcalltype gettypeinfocount (<br/>/* [out] */uint * pctinfo) <br/>{< br/> return e_notimpl; <br/>}</P> <p> virtual hresult stdmethodcalltype gettypeinfo (<br/>/* [in] */uint itinfo, <br/>/* [in] */lcid, <br/>/* [out] */itypeinfo ** pptinfo) <br/>{< br/> return e_notimpl; <br/>}</P> <p> virtual hresult stdmethodcalltype getidsofnames (<br/>/* [in] */refiid riid, <br/>/* [size_is] [in] */lpolestr * rgsznames, <br/>/* [in] */uint cnames, <br/>/* [in] */lcid, <br/>/* [size_is] [out] */dispid * rgdispid) <br/>{< br/> return e_notimpl; <br/>}</P> <p> virtual/* [local] */hresult stdmethodcalltype invoke (<br/>/* [in] */dispid dispidmember, <br/>/* [in] */refiid riid, <br/>/* [in] */lcid, <br/>/* [in] */word wflags, <br/>/* [out] [in] */dispparams * pdispparams, <br/>/* [out] */variant * pvarresult, <br/>/* [out] */variable info * p1_info, <br/>/* [out] */uint * puargerr) <br/> {<br/> MessageBox (null, "1111111", "2222222", mb_ OK ); </P> <p> return s_ OK; <br/>}</P> <p> stdmethod _ (ulong, addref )() <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod _ (ulong, release )() <br/>{</P> <p> return e_notimpl; <br/>}</P> <p> stdmethod (QueryInterface) (refiid riid, void ** ppvobject) <br/>{< br/> * ppvobject = This; // debugging result <br/> return s_ OK; <br/>}</P> <p> }; </P> <p> cocallback gpcallback; </P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> coinitialize (null); </P> <p> // obtain the object of the Interface Class in the DLL <br/> ihelloatlcontrolptr lcmhelloobj; <br/> hresult HDR = lcmhelloobj. createinstance (_ uuidof (helloatlcontrol); <br/> If (xr = s_ OK) <br/>{< br/> // execute the export function <br/> lcmhelloobj-> exportfun1 (); <br/>}</P> <p> // Add the callback class of the program to the connection container (in vect) in COM ), to notify the client <br/> iconnectionpointcontainer * lpconnectionpointcontainer = NULL; <br/> lcmhelloobj. queryInterface (iid_iconnectionpointcontainer, (void **) & lpconnectionpointcontainer); <br/> If (lpconnectionpointcontainer) <br/>{< br/> iconnectionpoint * lpconnectionpoint = NULL; <br/> lpconnectionpointcontainer-> findconnectionpoint (_ uuidof (_ ihelloatlcontrolevents), & lpconnectionpoint); </P> <p> lpconnectionpointcontainer-> release (); <br/> lpconnectionpointcontainer = NULL; </P> <p> If (lpconnectionpoint) <br/>{< br/> DWORD ldwcookie = 0; <br/> // Add to the class container in COM <br/> lpconnectionpoint-> advise (& gpcallback, & ldwcookie ); <br/>}</P> <p> lcmhelloobj-> exportfun1 (); <br/> lcmhelloobj = NULL; </P> <p> couninitialize (); </P> <p> return 0; <br/>}< br/>