简体   繁体   English

COM 接口问题 - .NET

[英]COM Interface question - .NET

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.为了使 .NET 程序集对 COM 可见,我需要使用 tlbexp 工具和/或 regasm 工具。 To make COM visible to .NET assebly I have to use tlbimp tool - all that is clear for me.为了使 COM 对 .NET 可见,我必须使用 tlbimp 工具——这对我来说很清楚。

  1. Apart from that, I've seen on the internet a lot of code dealing with COM interfaces with guid attributes, IUnknown and IDispach.除此之外,我在互联网上看到了很多处理带有 guid 属性、IUnknown 和 IDispach 的 COM 接口的代码。

My question is how all these relate to COM - do I need to use that?我的问题是所有这些与 COM 有何关系——我需要使用它吗? I am preparing to certification but they didn't say a word about interfaces, guids, IUnknown etc...我正在准备认证,但他们没有说关于接口、guids、IUnknown 等...

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).我还有一个关于从 COM(tlbimp 工具)导入的类型库的问题。 Does the type library imported from COM still require COM object registred in the system?从 COM 导入的类型库是否还需要在系统中注册 COM 对象?

Kind regards PK亲切的问候 PK

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.您似乎在问 GUID、IUnknown 和 IDispatch 与 COM 有什么关系。 I will try to give a brief survey.我将尝试做一个简短的调查。

Every COM component exposes a common interface, IUnknown.每个 COM 组件都公开一个公共接口 IUnknown。 IUnknown has the methods AddRef(), Release() and QueryInterface(). IUnknown 具有 AddRef()、Release() 和 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. AddRef() 和 Release() 用于支持引用计数,因此当对对象的每个引用都被释放时,该对象将被销毁。 QueryInterface() is kind of like COM's version of the dynamic_cast<> operator in C++. QueryInterface() 有点像 C++ 中的 dynamic_cast<> 运算符的 COM 版本。 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.客户端代码使用它来查看他们拥有的 IUnknow 指针是否实际指向另一种类型的对象,如 IDog、ICat 或其他类型的对象。

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;所以每个 COM 组件都必须实现 IUnknown,但大多数 COM 库会自动为您实现这一点,因此当您编写 COM 代码时,您通常不必编写任何代码来获取 IUnknown; you get it for free.你免费得到它。

GUIDs are kind of like fingerprints. GUID 有点像指纹。 Just like in real life two people can have exactly the same name, in COM two COM components can also have the same name.就像在现实生活中两个人可以拥有完全相同的名称一样,在 COM 中两个 COM 组件也可以具有相同的名称。 For example, you can have 2 libraries that both implement the IDog interface, but they may do totally different things.例如,您可以有 2 个都实现 IDog 接口的库,但它们可能做完全不同的事情。 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.但是,您仍然应该能够在您的机器上安装这两个库,并且区分这两个库是 GUID 的用途。 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. GUID 是一个全球唯一标识符,这意味着当您生成一个时,理论上地球上的任何其他人在任何其他时间点都无法创建相同的 GUID。 So in addition to a name, every COM object (and coclass, and library, etc) has a GUID.因此,除了名称之外,每个 COM 对象(以及协同类和库等)都有一个 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. IDispatch 是另一个基本接口,很像 IUnknown,但与每个 COM 对象都需要的 IUnknown 不同,IDispatch 是可选的,并提供许多但并非所有 COM 对象都支持的特殊功能。 Things like supporting certian language features and making your object easier to use by clients.诸如支持特定语言功能并使您的对象更易于客户使用之类的事情。 Most (probably) COM object expose this interface.大多数(可能)COM 对象公开此接口。

Regarding COM libraries;关于 COM 库; yes, they must be registered in Windows.是的,它们必须在 Windows 中注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM