COM 接口问题 - .NET

发布于 2024-07-17 12:48:54 字数 493 浏览 6 评论 0原文

最近我正在阅读有关互操作编组的内容,但我仍然不明白一件事。 为了使 .NET 程序集对 COM 可见,我需要使用 tlbexp 工具和/或 regasm 工具。 为了使 COM 对 .NET 程序集可见,我必须使用 tlbimp 工具 - 这一切对我来说都很清楚。

  1. 除此之外,我在互联网上看到了很多处理带有 guid 属性、IUnknown 和 IDispach 的 COM 接口的代码。

    除此之外

我的问题是所有这些与 COM 有什么关系 - 我需要使用它吗? 我正在准备认证,但他们没有说任何有关接口、指南、IUnknown 等的信息...

我真的不喜欢留下不清楚的东西,所以如果有人可以向我解释一下,我真的会很高兴感激的。

  1. 我还有一个关于从 COM(tlbimp 工具)导入的类型库的问题。 从COM导入的类型库还需要在系统中注册COM对象吗?

亲切的问候 PK

Recently I was reading about interop marshalling but I still don't understand one thing. To make .NET assembly visible to COM I need to use tlbexp tool and/or regasm tool. To make COM visible to .NET assebly I have to use tlbimp tool - all that is clear for me.

  1. Apart from that, I've seen on the internet a lot of code dealing with COM interfaces with guid attributes, IUnknown and IDispach.

My question is how all these relate to COM - do I need to use that? I am preparing to certification but they didn't say a word about interfaces, guids, IUnknown etc...

I don't really like to leave unclear things behind me, so If someone could please explain it to me, I would be really grateful.

  1. I also have one question regarding type library imported from COM (tlbimp tool). Does the type library imported from COM still require COM object registred in the system?

Kind regards
PK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-07-24 12:48:55

我不知道为什么有人对这个问题投了反对票。 对我来说,这似乎是一个完全正确的问题; 也许他们只是今天早上还没有喝咖啡。

您似乎在问 GUID、IUnknown 和 IDispatch 与 COM 有何关系。 我将尝试做一个简短的调查。

每个 COM 组件都公开一个公共接口 IUnknown。 IUnknown 具有 AddRef()、Release() 和 QueryInterface() 方法。 AddRef() 和 Release() 用于支持引用计数,因此当对对象的每个引用被释放时,该对象将被销毁。 QueryInterface() 有点像 COM 版本的dynamic_cast<>。 C++ 中的运算符。 客户端代码使用它来查看它们所拥有的 IUnknow 指针是否实际上指向另一种类型的对象,例如 IDog、ICat 或其他类型。

因此,每个 COM 组件都必须实现 IUnknown,但大多数 COM 库都会自动为您实现这一点,因此当您编写 COM 代码时,通常不必编写任何代码来获取 IUnknown; 你可以免费得到它。

GUID 有点像指纹。 就像现实生活中两个人可以具有完全相同的名称一样,在 COM 中两个 COM 组件也可以具有相同的名称。 例如,您可以拥有两个都实现 IDog 接口的库,但它们可能会执行完全不同的操作。 然而,您仍然应该能够在您的计算机上安装这两个库,并且区分这两个库是 GUID 的用途。 GUID 是一种全球唯一标识符,这意味着当您生成一个 GUID 时,理论上地球上任何其他人在任何其他时间点都无法创建相同的 GUID。 因此,除了名称之外,每个 COM 对象(以及组件类、库等)都有一个 GUID。

IDispatch 是另一个基本接口,与 IUnknown 非常相似,但与每个 COM 对象都需要的 IUnknown 不同,IDispatch 是可选的,并提供许多(但并非所有)COM 对象支持的特殊功能。 例如支持某些语言功能并使您的对象更易于客户使用。 大多数(可能)COM 对象都公开此接口。

关于 COM 库; 是的,它们必须在 Windows 中注册。

I have no idea why someone downvoted this question. It seems a perfectly valid question to me; maybe they just haven't had their coffee yet this morning.

You seem to be asking what GUIDs, IUnknown and IDispatch have to do with COM. I will try to give a brief survey.

Every COM component exposes a common interface, IUnknown. IUnknown has the methods AddRef(), Release() and QueryInterface(). AddRef() and Release() are used to support reference counting, so that when every reference to an object has been released the object will be destroyed. QueryInterface() is kind of like COM's version of the dynamic_cast<> operator in C++. It is used by client code to see if the IUnknow pointer they have actually points to an object of another type, like IDog, ICat, or whatever.

So every COM component must implement IUnknown, but most COM libraries will implement this for you automatically so when you are writing COM code you typically don't have to write any code to get IUnknown; you get it for free.

GUIDs are kind of like fingerprints. Just like in real life two people can have exactly the same name, in COM two COM components can also have the same name. For example, you can have 2 libraries that both implement the IDog interface, but they may do totally different things. You should still be able to have both libraries installed on your machine however, and distinguishing between the two is what GUIDs are used for. A GUID is a Globally Unique IDentifier, meaning that when you generate one, in theory no other person on Earth at any othher point in time will ever be able to create the same GUID. So in addition to a name, every COM object (and coclass, and library, etc) has a GUID.

IDispatch is another base interface much like IUnknown, but unlike IUnknown which is required for every COM object, IDispatch is optional, and provides special functionality that many but not all COM objects support. Things like supporting certian language features and making your object easier to use by clients. Most (probably) COM object expose this interface.

Regarding COM libraries; yes, they must be registered in Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文