Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. How to get CLSID for the underlying object.

How to get CLSID for the underlying object.

Scheduled Pinned Locked Moved COM
comtutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cagey
    wrote on last edited by
    #1

    I have a COM interface and would like the CLSID for the underlying implementing object. The interface I have decends from IDispatch, and I have played with ITypeInfo. I am able to get the IID of the interface, but not the CLSID of the object. Is there an easy way to do this? Thanks, cagey

    S 1 Reply Last reply
    0
    • C cagey

      I have a COM interface and would like the CLSID for the underlying implementing object. The interface I have decends from IDispatch, and I have played with ITypeInfo. I am able to get the IID of the interface, but not the CLSID of the object. Is there an easy way to do this? Thanks, cagey

      S Offline
      S Offline
      soptest
      wrote on last edited by
      #2

      In order to get CLSID of running COM instance that object must have at least IPersist to be implemented. --or-- To read CLSID from TypeLib (If you TypeLib has more then one coclass and some of those coclasses have implemented the same interface you can not say which coclass is instantiated):

      #include #include #include "comdef.h"
      #include "atlbase.h"

      void main()
      {
      CoInitialize(0);
      {
      CComPtr p;
      HRESULT hr;

      	hr = p.CoCreateInstance(L"TestObj.Test");
      	if(S\_OK == hr)
      	{
      		
      		::ITypeInfo \*pti;
      		::ITypeLib \*ptl;
      		UINT ui;
      
      		hr = p->GetTypeInfo(0, LOCALE\_SYSTEM\_DEFAULT,&pti);
      		
      		if(S\_OK == hr)
      		{
      			hr = pti->GetContainingTypeLib( &ptl, &ui);
      			pti->Release();
      			if(S\_OK == hr)
      			{
      				ui = ptl->GetTypeInfoCount();
      				
      				for(UINT i = 0; i < ui; ++i)
      				{
      					hr = ptl->GetTypeInfo(i, &pti);
      					
      					
      					if(S\_OK == hr)
      					{
      						
      						::TYPEATTR \*patr = 0;
      						hr = pti->GetTypeAttr( &patr);
      						if(patr->typekind == TKIND\_COCLASS)
      						{
      							printf("OK - TKIND\_COCLASS\\n");
      							OLECHAR \*wsz;
      							hr = ::StringFromCLSID(patr->guid, &wsz);
      							
      							if(S\_OK == hr)
      							{
      								printf("CLSID = %s\\n",(char\*) \_bstr\_t(wsz));
      								::CoTaskMemFree(wsz);
      							}
      						}
      						pti->Release();
      					}
      				}
      				ptl->Release();
      				
      			}
      		}
      	}
      }
      CoUninitialize();
      

      }

      soptest

      G 1 Reply Last reply
      0
      • S soptest

        In order to get CLSID of running COM instance that object must have at least IPersist to be implemented. --or-- To read CLSID from TypeLib (If you TypeLib has more then one coclass and some of those coclasses have implemented the same interface you can not say which coclass is instantiated):

        #include #include #include "comdef.h"
        #include "atlbase.h"

        void main()
        {
        CoInitialize(0);
        {
        CComPtr p;
        HRESULT hr;

        	hr = p.CoCreateInstance(L"TestObj.Test");
        	if(S\_OK == hr)
        	{
        		
        		::ITypeInfo \*pti;
        		::ITypeLib \*ptl;
        		UINT ui;
        
        		hr = p->GetTypeInfo(0, LOCALE\_SYSTEM\_DEFAULT,&pti);
        		
        		if(S\_OK == hr)
        		{
        			hr = pti->GetContainingTypeLib( &ptl, &ui);
        			pti->Release();
        			if(S\_OK == hr)
        			{
        				ui = ptl->GetTypeInfoCount();
        				
        				for(UINT i = 0; i < ui; ++i)
        				{
        					hr = ptl->GetTypeInfo(i, &pti);
        					
        					
        					if(S\_OK == hr)
        					{
        						
        						::TYPEATTR \*patr = 0;
        						hr = pti->GetTypeAttr( &patr);
        						if(patr->typekind == TKIND\_COCLASS)
        						{
        							printf("OK - TKIND\_COCLASS\\n");
        							OLECHAR \*wsz;
        							hr = ::StringFromCLSID(patr->guid, &wsz);
        							
        							if(S\_OK == hr)
        							{
        								printf("CLSID = %s\\n",(char\*) \_bstr\_t(wsz));
        								::CoTaskMemFree(wsz);
        							}
        						}
        						pti->Release();
        					}
        				}
        				ptl->Release();
        				
        			}
        		}
        	}
        }
        CoUninitialize();
        

        }

        soptest

        G Offline
        G Offline
        Gertjan Schuurmans
        wrote on last edited by
        #3

        soptest's code should be working in most cases, but first check if the object supports IPersist, IProvideClassInfo or IProvideClassInfo2. You can then obtain the class ID in a very simple way through: IPersist::GetClassID() IProvideClassInfo2::GetGUID() gertjan

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups