65.9K
CodeProject is changing. Read more.
Home

How to find the name for interfaces implemented by a COM object

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.29/5 (8 votes)

Apr 3, 2002

viewsIcon

100491

The article descibes how can you find the interfaces implemented by a COM object

Introduction

This is a class that helps you to find the interfaces implemented by a COM object. Here's how you can use that MsgBox method

QI::MsgBox( yourCOMObject ); 

Code Listing

#include <atlbase.h>
#include <string>
#include <sstream>
class QI : protected CRegKey,
#ifdef _UNICODE
    public std::wstringstream
#else
    public std::stringstream
#endif
{
protected:
    virtual tstring Query( IUnknown* pUnk )
    {
        if ( pUnk != NULL && ( ERROR_SUCCESS == 
            Open( HKEY_CLASSES_ROOT, _T("Interface") ) ) )
        {
            TCHAR szKeyName[1024] = _T("");
            for ( int i = 0; ERROR_SUCCESS == 
                RegEnumKey( m_hKey, i, szKeyName, 1024 ); i++ )
            {
                USES_CONVERSION;
                CLSID clsID = CLSID_NULL;
                if ( SUCCEEDED( CLSIDFromString( T2OLE(szKeyName), 
                    &clsID ) ) )
                {
                    CComPtr spObject = NULL;
                    if ( SUCCEEDED( pUnk->QueryInterface( clsID, 
                        (void**)&spObject ) ) )
                    {
                        TCHAR szValue[1024] = _T(""); 
                        LONG ncbValue = 1024;
                        if ( ERROR_SUCCESS == RegQueryValue( m_hKey, 
                            szKeyName, szValue, &ncbValue ) )
                            *this << tstring(szValue) + _T("\r\n");
                    }
                }
            }
            Close();
        }
        return str();
    }

public:
    static void MsgBox( IUnknown* pUnk )
    {
        QI objQI;
        MessageBox( NULL, objQI.Query( pUnk ).c_str(),
            _T("The object implements:"), MB_ICONINFORMATION );
    }
};