How To Get the CLSID and ProgID of a CoClass from the Type Library in OCX, DLL, and EXE Files (286340)



The information in this article applies to:

  • Microsoft COM

This article was previously published under Q286340

SUMMARY

When you use the ITypeLib and ITypeInfo interfaces, you can read the type information that is sent to a type library. The sample code in this article illustrates how to use ITypeInfo to find out the CLSID and ProgID of component object classes (CoClasses) that are sent to the type library.

MORE INFORMATION

Sample Code

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <comdef.h>
void DisplayError(HRESULT hr);
void Cleanup();
ITypeLib *pTypeLib = NULL;
ITypeInfo *pTypeInfo = NULL;
TYPEATTR *pTypeAttr = NULL;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	_bstr_t str;
	HRESULT hr;
	WCHAR* pwszProgID = NULL;
        // This assumes that the .ocx file contains the type library information.
        // If it does not, we can replace the .ocx file with the .tlb file.
	hr = LoadTypeLib(L"f:\\winnt\\system32\\comdlg32.ocx",&pTypeLib);
	if ( FAILED(hr)) 
	{
           Cleanup(); // Release the Interface pointers.
	   DisplayError(hr); // Display the error that corresponds to the HRESULT.
           return 0;
	}
	UINT iCount = pTypeLib->GetTypeInfoCount(); 
	// There may be multiple types that are defined in a type library. 
         // Let us iterate through them to find the CoClasses that are defined.
	for (int i=0; i < iCount ; i++) 
            {
	       // Obtain ITypeInfo interface.
               if (FAILED(hr = (pTypeLib->GetTypeInfo(i, &pTypeInfo))))
                 {
                    DisplayError(hr);
                    Cleanup();
                    return 0;
                 }
	       // Obtain TYPEATTR structure.
               if (FAILED(hr = (pTypeInfo->GetTypeAttr(&pTypeAttr))))
                 {
                    DisplayError(hr);
                    Cleanup();
                    return 0;
                 }
	        // Check if this is a CoClass.
		if (TKIND_COCLASS == pTypeAttr->typekind)
		  // pTypeAttr->guid contains the CLSID of the CoClass.
		  if (!FAILED(hr =  (ProgIDFromCLSID(pTypeAttr->guid,&pwszProgID))))
		     {
			str = pwszProgID;
			// Display the ProgID.
			::MessageBox(NULL,str,str,MB_OK);
			CoTaskMemFree(pwszProgID);
		     }
		pTypeInfo->ReleaseTypeAttr(pTypeAttr);
		pTypeInfo->Release();
			
             }

	pTypeLib->Release();
	pTypeLib = NULL;
	return 0;
}

void DisplayError(HRESULT hr)
{
      LPVOID lpMsgBuf;
      FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		  FORMAT_MESSAGE_FROM_SYSTEM    		  |
		  FORMAT_MESSAGE_IGNORE_INSERTS		  ,
		  NULL,hr, 
                  // Default language.
		  MAKELANGID(LANG_NEUTRAL,  SUBLANG_DEFAULT), 
                  // Process any inserts in lpMsgBuf.
                 (LPTSTR) &lpMsgBuf, 0, NULL );
      char szTemp[100];
      wsprintf(szTemp, "%s\n", lpMsgBuf);
}
void Cleanup()
{
      if (pTypeAttr)
          pTypeInfo->ReleaseTypeAttr(pTypeAttr);

      if (pTypeInfo)
          pTypeInfo->Release();
      pTypeAttr = NULL;
      pTypeInfo = NULL;
}
				

Modification Type:MinorLast Reviewed:10/6/2004
Keywords:kbcode kbhowto KB286340