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