Microsoft KB Archive/286340

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

Article ID: 286340

Article Last Modified on 10/6/2004

-

APPLIES TO


 * Microsoft COM+ 2.0 Standard Edition

-



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.



Sample Code
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&quot;f:\\winnt\\system32\\comdlg32.ocx&quot;,&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; }
 * 1) include &quot;stdafx.h&quot;
 * 2) include 
 * 3) include 
 * 4) include 

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, &quot;%s\n&quot;, lpMsgBuf); } void Cleanup {     if (pTypeAttr) pTypeInfo->ReleaseTypeAttr(pTypeAttr);

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

Keywords: kbhowto kbcode KB286340

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.