C# Get progID from COM object

前端 未结 1 1154
慢半拍i
慢半拍i 2020-12-10 15:19

i would like to know if there is a way to get the progId of a com object in c#. eg - i have a webBrowser object that exposes a document object which is COM. is there a way t

相关标签:
1条回答
  • 2020-12-10 15:56

    You could query for IPersist, and GetClassID on it.

    That gets you the CLSID. Then call ProgIDFromCLSID:

    The pinvoke declaration is here.

    That gets you the ProgID.

    EDIT:

    To query for an interface, you just do a cast in C#:

    IPersist p = myObj as IPersist;
    if (p != null)
    {
        // phew, it worked...
    }
    

    Behind the scenes, this is what is actually happening, as shown here in C++:

    IUnknown *pUnk = // ... get object from somewhere
    
    IPersist *pPersist = 0;
    if (SUCCEEDED(pUnk->QueryInterface(IID_IPersist, (void **)&pPersist)))
    {
        // phew, it worked...
    }
    

    (But no one bothers with writing that stuff by hand these days, as a smart pointer can pretty much simulate the C# experience.)

    0 讨论(0)
提交回复
热议问题