Overview
Idispatch interface: interface for interface scheduling. You can call functions of the interface without using header files.
Idispatch interface functions
- 1> getidsofnames-obtain the interface function id based on the interface function name
- 2> invoke-according to the interface function ID, call the interface function
- 3> gettypeinfo-interface for getting specified type information
- 4> gettypeinfocount-number of interfaces for getting specified type information
Idispatch Project
Create a project: when adding components and interfaces, select "dual" to add support for the idispatch interface.
Project changes
- File changes. The interface's parent interface is the idispatch interface. When an interface function is added, the interface function ID is automatically added.
- Implementation class of components: the parent class adds idispathimpl.
Getidsofnames function hresult getidsofnames (refiid riid, // reserved value, iid_nullolechar far * rgsznames, // interface function name String Array unsigned int cnames, // The length of the array lcid, // language ID, locale_system_defaultdispid far * rgdispid // returns the queried interface function ID array); invoke function hresult invoke (dispid dispidmember, // interface function ID refiid riid, // reserved parameter, iid_nulllcid lcid, // language idword wflags, // type of the interface function dispparams far * pdispparams, // parameter variant far * pvarresult of the function, // The Return Value of the function is reset info far * pexcepinfo, // the error message unsigned int far * puargerr // error code );
Instance
1. Create an ATL Project atldisp
1. Create an ATL Project atldisp: Next --> application type: Dynamic Link Library DLL; supported options: Support for COM + 1.0, support for Part registrants --> complete.
2. Add the mymath class: Right click -- add -- class --> select atlcom + 1.0 component --> mymath for short; Other Default values; com progid: atldisp --> next, complete.
3. add interface methods add () and showmsg (): In the Class View, right-click the imymath interface and choose add --- Method --> Method Name: Add. Parameter features [ID (1)] hresult add ([in] int nadd1, [in] int nadd2, [out, retval] int * nresult); [ID (2)] hresult showmsg (void );
4. Add the add () method: Add the following in mymath. cpp:
STDMETHODIMP CMyMath::Add(int nAdd1, int nAdd2, int* nResult){ *nResult=nAdd1+nAdd2;return S_OK;}STDMETHODIMP CMyMath::ShowMsg(void){ MessageBox(NULL,L"Hello Dispatch!!!",L"ATLDisp",MB_OK);return S_OK;}
2. Create the useatldisp project of mfcdialog
1. Create an MFC dialog useatldisp project in a solution
2. In useatldisp. cpp:
Bool cuseatldispapp: initinstance () {coinitialize (0); // now initialize com... return false;} int cuseatldispapp: exitinstance () {couninitialize (); // uninstall comreturn cwinapp: exitinstance ();}
3. Button code
3. Button code 3. Button code
Idispatch * cuseatldispdlg: getdispatch () {CLSID = {0}; clsidfromprogid (L "atldisp", & CLSID); // obtain classididispatch * pidispatch = NULL Based on progid; cocreateinstance (CLSID, null, clsctx_server, iid_idispatch, (lpvoid *) & pidispatch); // create a component and return the idispatch interface if (pidispatch = NULL) {afxmessagebox (L "failed to create the idispatch interface");} return pidispatch;} // obtain the idvoid of the interface function based on the name of the interface function cuseatispdlg: onbnclickedbuttongetidsofnames () {idispatch * pidispatch = getdispatch (); wchar * pszname = l "showmsg"; // adddispid nid = {0}; pidispatch-> getidsofnames (iid_null, & pszname, 1, locale_system_default, & nid); cstring Strid; Strid. format (L "nid = % d", NID); MessageBox (Strid); // Add 1; showmsg 2} // void cuseatldispdlg: onbnclickedbuttonnoparam () {idispatch * pidispatch = getdispatch (); wchar * pszname = l "showmsg"; dispid nid = {0}; pidispatch-> getidsofnames (iid_null, & pszname, 1, locale_system_default, & nid); // call the function using invoke. The parameter and return value are dispparams Params = {0}; variant varresult = {0}; pidispatch-> invoke (NID, iid_null, locale_system_default, dispatch_method, interval ms, & varresult, null, null);} // void cuseatldispdlg: onbnclickedbuttonparams () {idispatch * pidispatch = getdispatch (); wchar * pszname = l "add"; dispid nid = {0}; pidispatch-> getidsofnames (iid_null, & pszname, 1, locale_system_default, & nid ); // call the function using invoke to construct parameters and return values variant varresult = {0}; dispparams Params = {0}; variant varparam [3] = {0}; Params. cargs = 3; Params. rgvarg = varparam; // The Order of the parameters is from right to left // The first parameter long nvalue = 0; varparam [0]. vt = vt_i4 | vt_byref; varparam [0]. plval = & nvalue; // The second parameter varparam [1]. vt = vt_i4; varparam [1]. lval = 100; // The third parameter varparam [2]. vt = vt_i4; varparam [2]. lval = 100; // call the function pidispatch-> invoke (NID, iid_null, locale_system_default, dispatch_method, interval ms, & varresult, null, null); cstring strvalue; strvalue. format (L "nresult: % d", nvalue); MessageBox (strvalue); // output result}
4. Interface