How to get CLSID for the underlying object.
-
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
-
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
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
-
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
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