| ||||||||||||
|
Getting the most out of IDispatch IntroductionDon't be fooled by the title. Even if you are not (will not be) dealing with theIDispatch interface directly, this article and the included source code could be very useful to you. In my previous article "Buried treasure in MFC: COleDispatchDriver", I introduced XYDispDriver, a class derived from COleDispatchDriver, that can be used to dynamically create and use com objects. The advantage of XYDispDriver is its simple interface. You create and use all (well, almost all) com objects the same way. There is no need for wrapper classes. You don't even need type libraries at compile time.
Then I realized that not all people love MFC as I do. By diving into the source code of Most of my code is borrowed from MFC, of course. Fortunately, XYDispDriverHere are some of the public methods ofXYDispDriver. Please note the changes from its previous version. class XYDispDriver { ... public: XYDispDriver(); ~XYDispDriver(); // create a com object with given prog id or class id bool CreateObject(LPCTSTR strProgID, DWORD dwClsContext = CLSCTX_ALL, LPCTSTR strServerName = NULL); bool CreateObject(CLSID clsid, DWORD dwClsContext = CLSCTX_ALL, LPCTSTR strServerName = NULL); // get the type of a property VARTYPE GetPropertyType(LPCTSTR strPropertyName); // get the property value VARIANT* GetProperty(LPCTSTR strPropertyName); // set the property value bool SetProperty(LPCTSTR strPropertyName, ...); // get return type of a method VARTYPE GetReturnType(LPCTSTR strMethodName); // get number of parameters in a method int GetParamCount(LPCTSTR strMethodName); // get the type of a parameter in a method VARTYPE GetParamType(LPCTSTR strMethodName, const int nParamIndex); // invoke a method VARIANT* InvokeMethod(LPCTSTR strMethodName, ...); // get the last error code as HRESULT HRESULT GetLastError() { return m_hRet; } // get exception info EXCEPINFO* GetExceptionInfo() { return m_pExceptInfo; } }; The The output values of For power users, the Sample codeThe following code demonstrates how to useXYDispDriver to create a com object and call its methods in a console application. #include "XYDispDriver.h" void main() { // declare the dispatch driver XYDispDriver disp; // create the com object from its prog id if(disp.CreateObject("XYDBREADER.XYDBReaderCtrl.1")) { // call the OpenDB method which takes 3 string arguments and returns // a bool VARIANT* pOutput = disp.InvokeMethod("OpenDB","XY_TAG","",""); if(pOutput&&pOutput->boolVal) { printf("Database opened\n"); // call the OpenQuery method which takes a long and a string // arguments and returns a long pOutput = disp.InvokeMethod("OpenQuery",0, "select * from XYTagName"); if(pOutput&&pOutput->lVal) { printf("Query opened: %d\n",pOutput->lVal); long nQueryID = pOutput->lVal; // call the GetAllRec method which takes a long argument // and returns a string (BSTR) pOutput = disp.InvokeMethod("GetAllRec",nQueryID); if(pOutput) { printf("Got data"); // print the output data wprintf(L"Returned data:\n%s\n",pOutput->bstrVal); // call the CloseQuery method which takes a long argument // and returns nothing disp.InvokeMethod("CloseQuery",nQueryID); } } // call the CloseDB method which takes no argument and returns // nothing disp.InvokeMethod("CloseDB"); } } printf("done\n"); } It is also possible to use Other FeaturesYou can use
The
Thank you for reading this article, please refer to my home page for other articles and programs. Recent Updates
Xiangyang Liu
| ||||||||||||

浙公网安备 33010602011771号