Extract CLSID of COM component from TLB, ocx, and dll library

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.