问题描述
我想知道是否有办法在 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 就可以了.
You could query for IPersist
, and GetClassID on it.
这将为您提供 CLSID
.然后调用ProgIDFromCLSID:
That gets you the CLSID
. Then call ProgIDFromCLSID:
这会让你得到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!