C#从COM对象获取progID [英] C# Get progID from COM object
问题描述
我想知道是否有一种方法来获取c#中com对象的progId。例如 - 我有一个webBrowser对象暴露一个文档对象是COM。有没有办法找出该文档对象的progID是什么?
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 to figure out what the progID of that document object is?
我知道你可以从progID获取对象,只是不知道如何做的其他方式
I know you can get the object from progID, just not sure how to do the other way around.
推荐答案
您可以查询 IPersist
和 GetClassID 。
这会得到 CLSID
。然后调用 ProgIDFromCLSID :
The pinvoke declaration is here.
这将获得您的ProgID。
That gets you the ProgID.
编辑:
要查询接口,只需在C#中进行转换:
To query for an interface, you just do a cast in C#:
IPersist p = myObj as IPersist;
if (p != null)
{
// phew, it worked...
}
在后台,这是实际发生的事情,如C ++中所示:
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...
}
(但是没有人麻烦地用手写这些东西,因为聪明的指针可以模拟C#体验。)
(But no one bothers with writing that stuff by hand these days, as a smart pointer can pretty much simulate the C# experience.)
这篇关于C#从COM对象获取progID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!