简体   繁体   English

构建DLL时,COM如何补充.NET Framework?

[英]How does COM complement the .NET Framework when building DLLs?

Over the past couple of weeks I was working on building a custom DLL extension for Excel that I wrote in C# and built as a COM object with COM interop enabled through Visual Studio. 在过去的几周中,我一直在为Excel创建自定义DLL扩展,该扩展是用C#编写的,并通过Visual Studio启用了COM互操作,从而构建为COM对象。 The DLL itself works fine but I want to understand the technology behind it. DLL本身工作正常,但我想了解其背后的技术。

After reading a few articles and posts I got confused quite a bit and can't seem to find the information that explains exactly how COM and the .NET Framework work together to allow us to build these DLLs and why we need both of them. 在阅读了几篇文章和帖子后,我感到相当困惑,并且似乎找不到确切说明COM和.NET Framework如何一起工作以允许我们构建这些DLL的信息,以及为什么同时需要它们。

My current understanding: 我目前的理解:

COM - a way to create binary objects that are language-independent and can be used in different environments. COM-一种创建与语言无关的二进制对象的方法,可以在不同的环境中使用。 (For example you write a C# object, build it as a COM object, and then you can use it in your VB Script in Excel) (例如,编写一个C#对象,将其构建为COM对象,然后可以在Excel的VB脚本中使用它)

.NET Framework - a framework that provides a common run-time environment for all supported languages and allows language interoperability between them. .NET Framework-一个框架,为所有受支持的语言提供通用的运行时环境,并允许它们之间的语言互操作性。 (In other words a C# object can be used by a VB Script due to the CLR) (换句话说,由于CLR,VB脚本可以使用C#对象)

The Confusion: 混乱:

In one of the articles COM was presented as a predecessor to the .NET Framework that requires a lot of extra logic from developers in order to manage their code(COM => unmanaged code). 在一篇文章中,COM是.NET Framework的前身,它要求开发人员提供很多额外的逻辑来管理其代码(COM =>非托管代码)。 The .NET Framework takes care of that now and has a way to deal with unmanaged code as if it were managed code under the .NET Framework. .NET Framework现在已经解决了这一问题,并且具有一种处理非托管代码的方式,就好像它是.NET Framework下的托管代码一样。

If COM and .NET Framework objects are technically cross-language compatible, why can't we simply use Visual Studio to build a C# DLL, build it and reference it in Excel as an add-in? 如果COM和.NET Framework对象在技术上是跨语言兼容的,为什么我们不能简单地使用Visual Studio来构建C#DLL,进行构建并在Excel中将其作为加载项引用呢? Why do we need to register the assembly as COM object and enable it for COM interop if the .NET framework should already provide this language interoperability and all the code management features? 如果.NET框架应该已经提供了这种语言的互操作性和所有代码管理功能,为什么我们需要将程序集注册为COM对象并为COM互操作启用它?

Perhaps it would be best if you can really explain the relationship between the 2 technologies and how exactly the "make assembly COM visible" and "register for COM interop" settings in Visual Studio tie in together with them. 如果您能真正解释这两种技术之间的关系,以及Visual Studio中“使程序集COM可见”和“注册COM互操作”设置如何与它们紧密结合在一起,也许是最好的选择。

Thanks, Dimitar 谢谢,迪米塔尔

EDIT: 编辑:

Update 04/22/19: 更新04/22/19:

After reading your feedback below I get the following: 阅读以下您的反馈后,我得到以下信息:

  1. COM allows DLLs to expose their components by implementing some specific interfaces/methods COM允许DLL通过实现一些特定的接口/方法来公开其组件

  2. Excel only supports COM, and therefore only works with COM objects although it's a .NET Application. Excel仅支持COM,因此尽管是.NET应用程序,但它仅适用于COM对象。

  3. .NET provides COM interop for applications such as Excel that cannot work with .NET components directly .NET为无法直接与.NET组件一起使用的Excel等应用程序提供COM互操作

  4. The COM visible setting tells COM which parts of your object you want to be available for COM use. COM可见设置告诉COM您希望对象可用于COM的哪些部分。 The COM interop decorates the C# object with the necessary interfaces/methods in order to make it usable by COM. COM互操作使用必要的接口/方法来装饰C#对象,以使其可被COM使用。

Things I still need clarified: 我仍然需要澄清的事情:

1.Is my C# object in Visual Studio considered a .NET component if not COM enabled? 1.如果未启用COM,Visual Studio中的C#对象是否被视为.NET组件? I assume yes. 我想是的。

2.Is COM interop the main thing that turns Excel into a .NET application? 2.COM interop是将Excel变成.NET应用程序的主要功能吗?

  1. Does the .COM interop also allow .NET applications that do not rely on COM, unlike Excel, to also use COM objects? 与Excel不同,.COM互操作是否还允许不依赖COM的.NET应用程序也使用COM对象?

  2. What exactly does the language neutralization? 语言中和到底是什么? COM or .NET? COM或.NET? How? 怎么样?

COM is an older technology and existed much before .NET , Component Object Model was created by MS and used to allow DLLs to expose their components and callers to simply query if a loaded component would implement the specific interface the caller was looking for. COM是一种较老的技术,在.NET之前存在很久,它是由MS创建的,用于允许DLL公开其组件,而调用者则可以简单地查询加载的组件是否实现了调用者正在寻找的特定接口。

Every COM library or object is implementing at least one interface called IUnknowk and a method called QueryInterface could be used to check if that object implemented a specific interface. 每个COM库或对象都至少实现一个称为IUnknowk接口,可以使用一种称为QueryInterface的方法来检查该对象是否实现了特定的接口。

When MS introduced .NET Framework in 2001, it has decided to support, by design, from day one and natively a paradigm called COM / .NET interoperability, this means that from .NET you can consume COM libraries and also that by selecting the COM Interop flag in project properties you get the .NET compiler to decorate your assembly with such extra parts required by COM; 当MS在2001年推出.NET Framework时,它决定从设计开始就支持从第一天开始,并且原生支持称为COM / .NET互操作性的范例,这意味着从.NET您可以使用COM库,也可以通过选择COM来支持项目属性中的Interop标志使您可以使用.NET编译器用COM所需的额外零件来装饰程序集; like the IUnknown and QueryInterface attributes. 例如IUnknown和QueryInterface属性。

Excel and any other COM consumer can then use that approach to find objects and methods in your COM enabled .NET assembly. 然后,Excel和任何其他COM使用者可以使用该方法在启用COM的.NET程序集中查找对象和方法。

As Excel COM loader ( or VBA or VB Script languages ) are older than .NET, these tools cannot find .NET objects natively as those were designed to consume COM only and so look for iUnknown interface etc. 由于Excel COM加载程序(或VBA或VB脚本语言)早于.NET,因此这些工具无法原生找到.NET对象,因为这些对象仅设计为使用COM,因此需要查找iUnknown接口等。

hope it helps, there is lots of documentation online about this matter, for instance here 希望对您有帮助,在线有很多有关此问题的文档,例如此处

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/com-interop/index https://docs.microsoft.com/zh-cn/dotnet/visual-basic/programming-guide/com-interop/index

but also much much more depends on which aspect i particular you are interested about 而且还取决于我对您感兴趣的方面

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

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