Progid from Interface ID

Author: Dave
Date: 06.21.14 - 7:25am

Here is a hairy bit of code for you..

Lets say you have an IDispatch interface, you want to know what the publicly creatable CLSID and ProgID are for its class. How do you find it?

The TYPEATTR of the actual object you have ahold of, is an interface id. So you have to get the parent type library, enumerate its coclasses, enumate each coclasses implemented interfaces scanning for the IID you already have. Once found, then you use parent CoClass info to turn its TYPEATTR structures guid attribute into the ProgID and CLSID.

Much thanks to Igor Tandetnik news group post back from 5/30/07 on enumerating the steps to pull this off.

Below is some code for the brave of heart..

HRESULT FindCoClass(ITypeInfo* pti, GUID target, std::string &progid, std::string &clsid, std::string &version)
   HRESULT hr = S_OK;
   UINT count = 0;
   char buf[100];
   std::string tmp;
   WCHAR* pwszProgID = NULL;

   UINT index=0;
   CComPtr<ITypeLib> spTypeLib = NULL;
    hr = pti->GetContainingTypeLib(&spTypeLib, &index);

   if(SUCCEEDED(hr) && spTypeLib){
      UINT cnt = spTypeLib->GetTypeInfoCount();
      for(int i=0; i < cnt; i++){
         TYPEKIND tk;
         CComPtr<ITypeInfo> spTypeInfo = NULL;
         hr = spTypeLib->GetTypeInfoType(i, &tk);
            if(tk == TKIND_COCLASS){
               hr = spTypeLib->GetTypeInfo(i, &spTypeInfo);
               if(SUCCEEDED(hr) && spTypeLib){
                  TYPEATTR *ta = NULL;
                  hr = spTypeInfo->GetTypeAttr(&ta);
                  if(SUCCEEDED(hr) && ta){
                     for(int j=0; j< ta->cImplTypes; j++){
                        HREFTYPE ht;
                        TYPEATTR *ta2 = NULL;
                        CComPtr<ITypeInfo> ti2 = NULL;

                        hr = spTypeInfo->GetRefTypeInfo(ht, &ti2);
                        if(SUCCEEDED(hr) && ti2){
                           hr = ti2->GetTypeAttr(&ta2);
                           if(SUCCEEDED(hr) && ta2){
                              if(ta2->guid == target){ //we found our match..
                                 sprintf(buf, "%d.%d", ta->wMajorVerNum, ta->wMinorVerNum);
                                 version = buf;
                                 if (!FAILED(hr = (ProgIDFromCLSID(ta->guid,&pwszProgID))))
                                    progid = W2A(pwszProgID);
                                 if (!FAILED(hr = (StringFromCLSID(ta->guid,&pwszProgID))))
                                    clsid = W2A(pwszProgID);
                                 return S_OK;
     return 0;

And example usage:

HRESULT GetIDispatchMethods(IDispatch* pDisp, std::list &methods)
    HRESULT hr = S_OK;
    UINT count = 0;
    char buf[100];
    std::string tmp;
    std::string progid;
    std::string clsid;
    std::string version;


    CComPtr<IDispatch> spDisp(pDisp);
        return E_INVALIDARG;

    CComPtr<ITypeInfo> spTypeInfo = NULL;
    hr = spDisp->GetTypeInfo(0, 0, &spTypeInfo);

    if(SUCCEEDED(hr) && spTypeInfo)
        TYPEATTR *pTatt = nullptr;
        hr = spTypeInfo->GetTypeAttr(&pTatt);
        if(SUCCEEDED(hr) && pTatt)
           hr = FindCoClass(spTypeInfo,pTatt->guid, progid, clsid, version);
           if(progid.length() > 0)methods.push_back("ProgID: " + progid); 
           if(clsid.length() > 0)methods.push_back("CLSID: " + clsid);
           if(version != "0.0") methods.push_back("Version: " + version);


Comments: (1)

On 06.21.14 - 7:30am Dave wrote:
You can find a working example in the demo

I should also mention the context of this is for dealing with public ActiveX control interfaces that support ITypeInfo. VB6 Form objects for example, while they are COM objects and do support IDispatch and are scriptable, do not support a full ITypeInfo because the interface is not public and they do not have type library entries. Basically it seems like they support just enough so that the VB6 TypeName() function always works.

Actually sometimes VB6 form objects do have a full implementation of ITypeInfo functions, but only while running in the IDE. I believe this is related to the internal workings of the IDE intellisense.

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 65 + 74 = ? followed by the letter: Q 

About Me
More Blogs
Main Site
Console tricks
FireFox temp dir
OCX License
Extract substring
VB6 Console Apps
VB6 Debugger View As Hex tooltips
VB6 - C Share registry data
VB6 Addin Missing Menus
VB6 Class Init Params
VB6 isIn function
Python and VB6
Python pros and cons
download web Dir
vc rand in python
VB6 Language Enhancement
Register .NET as COM
UDT Tricks pt2
Remote Data Extraction
Collection Extender
VB6 FindResource
DirList Single Click
Reset CheckPoint VPN Policy
VB6 BSTR Oddities Explained
SafeArrays in C
BSTR and Variant in C++
Property let optional args
Misc Libs
Enum Named Pipes
Vb6 Collection in C++
VB6 Overloaded Methods
VB6 Syncronous Socket
Simple IPC
VB6 Auto Resize Form Elements
Mach3 Automation
Exit For in While
C# self register ocx
VB6 Class Method Pointers
JS Debugger
Duktape Debug Protocol
QtScript 4 VB
Vb6 Named Args
vb6 Addin Part 2
VB6 Addin vrs Toolbars
OpenFile Dialog MultiSelect
Duktape Example
DukTape JS
VB6 Unsigned
.Net version
TitleBar Height
.NET again
VB6 Self Register OCXs
Query Last 12 Mos
Progid from Interface ID
VB6 to C Array Examples
Human Readable Variant Type
ScriptBasic COM Integration
CodeView Addin
ScriptBasic - Part 2
Script Env
printf override
History Combo
Disable IE
API Hooking in VB6
Addin Hook Events
FastBuild Addin
VB6 MemoryWindow
Link C Obj Files into VB6
Vb6 Standard Dlls
CStr for Pascal
Lazarus Review
asprintf for VS
VB6 GlobalMultiUse
Scintilla in VB6
Dynamic Highlight
WinVerifyTrust, CryptMsgGetParam VB6
MS GLEE Graphing
printf for VB6
C# App Config
Tero DES C# Test
VC 2008 Bit Fields
Speed trap
C# Db Class Generator
VB6 vrs .NET (again)
FireFox Whois Extension
git and vb6
Code Additions
Compiled date to string
C# ListView Sorter
VB6 Wish List
C# Process Injection
C# PE Offset Calculator
VB6 Async Download
Show Desktop
coding philosophy
Code release
Dll Not Found in IDE
Advanced MSScript Control
random tip
Clipart / Vector Art
VB6 Callback from C#
Binary data from VB6 to C#
CSharp and MsScriptControl
HexDumper functions
Js Beautify From VB6 or C#
vb6 FormPos
Inline Asm w VB6
The .NET Fiasco
One rub on computers
Universal extractor