I recently encountered this problem when I was working on the project.
The project dynamically obtains an interface function of a component based on the COM component.
Generally, the method for creating a COM component is to obtain the corresponding interface according to its CLSID number, and then call the corresponding interface function through the returned pointer. As follows:
Coinitialize (null); iptest * pmask = NULL; hresult hR = cocreateinstance (clsid_test, null, clsctx_inproc, iid_itest, (lpvoid *) & iptest); If (! Succeeded (HR) {afxmessagebox (_ T ("COM component creation failed, check whether it is installed and registered"); Return 0 ;}iptest-> somefunc (); // call the corresponding interface function iptest-> release (); couninitialize ();
Now, the project obtains the CLSID of the corresponding COM component based on the input TLB file, then calls the cocreateinstance () function and passes it to the CLSID it just read.
Baidu and Google found the solution in just half a day.
It is encapsulated as a function.
#include <windows.h>#include <comdef.h>
Itypelib * ptypelib = NULL; itypeinfo * ptypeinfo = NULL; typeattr * ptypeattr = NULL; void cleanup (); guid trim (const cstring & strfilename) Throw (STD: runtime_error ); // strfilename -- enter TLB, ocx file path guid getclsidfromfile (const cstring & strfilename) Throw (STD: runtime_error) {_ bstr_t STR; hresult hr; tchar * pwszprogid = NULL; hR = loadtypelib (strfilename, & ptypelib); // load the type library information if (failed (HR) {cleanup (); Return GUID ();} uint icount = ptypelib-> gettypeinfocount (); // obtain the number of types for (INT I = 0; I <icount; I ++) {If (failed (hR = (ptypelib-> gettypeinfo (I, & ptypeinfo) // obtain the type description {cleanup (); throw runtime_error ("the input file does not contain related COM component interfaces"); Return GUID ();} If (failed (hR = (ptypeinfo-> gettypeattr (& ptypeattr )))) // obtain the type attributes {cleanup (); throw runtime_error ("the input file does not contain related COM component interfaces"); Return GUID ();} if (tkind_coclass = ptypeattr-> typekind) // determines whether the component interface is return ptypeattr-> guid; ptypeinfo-> releasetypeattr (ptypeattr); ptypeinfo-> release ();} ptypelib-> release (); ptypelib = NULL; throw runtime_error ("the input file does not contain the relevant COM Component Interface"); Return GUID ();} void cleanup () {If (ptypeattr) ptypeinfo-> releasetypeattr (ptypeattr); If (ptypeinfo) ptypeinfo-> release (); ptypeattr = NULL; ptypeinfo = NULL ;}
When used, The TLB, ocx, and DLL file paths are directly transferred to the getclsidfromfile () function. The return value of the function is the CLSID of the COM component;
After obtaining the CLSID of the component, you can access the corresponding interface of the component according to the conventional method.
If you know the progid of the corresponding component, you can use
HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID, //Pointer to the ProgID LPCLSID pclsid //Pointer to the CLSID);
Input the corresponding progid string and return the corresponding CLSID;
Reference: http://support.microsoft.com/kb/286340/zh-cn