简体   繁体   English

在C中使用C#dll-托管CLR或注册COM互操作,Visual Studio 2010

[英]Using C# dll in C - host CLR or register for COM interop, Visual Studio 2010

As it is described here I'd like to use a DLL in my C program. 如此处所述我想在我的C程序中使用DLL。 The first two options I am considering are: 我正在考虑的前两个选项是:

1. Host CLR 1.主机CLR

My problem here would be the note from this article: 我的问题是这篇文章的注释:

Hosting methods provided by versions of the .NET Framework before the .NET Framework 4 are deprecated. 不推荐使用.NET Framework 4之前的.NET Framework版本提供的托管方法。 We recommend that you use the interfaces introduced by the .NET Framework 4 and discussed in this topic. 我们建议您使用.NET Framework 4引入并在本主题中讨论过的接口。

In my case the DLL files are working under the .NET Framework 2.0 so this could be a problem in the first place. 在我的情况下,DLL文件在.NET Framework 2.0下工作,因此这首先可能是个问题。

  • Does anybody know if I can do this anyway somehow? 有人知道我是否可以这样做吗?

2. Register for COM interop 2.注册COM互操作

The other option is using COM. 另一个选择是使用COM。 In this case I would need to know what exactly does the option Register for COM interop in Visual Studio 2010. The DLL file is part of a library that our company also gives to customers. 在这种情况下,我需要知道在Visual Studio 2010中注册COM互操作选项的确切含义。DLL文件是我们公司还提供给客户的库的一部分。 So what I need to know is: 所以我需要知道的是:

  • Which additional information are stored inside the DLL? DLL中存储了哪些其他信息?
  • Could there be a reason why a company would not want to register its DLLs for COM interop? 公司可能不想为COM互操作注册其DLL的原因吗?

Thanks for any information! 感谢您提供任何信息!

Using Regasm.exe (aka "Register for COM interop") does two things: 使用Regasm.exe(又名“注册COM互操作”)可以完成两件事:

  • it records where the DLL is located so it can be automatically found. 它记录DLL的位置,以便可以自动找到它。 You just use a number in your own code, a GUID. 您只需在自己的代码中使用一个数字,即GUID。
  • it records which CLR version is required to run the code. 它记录了运行该代码所需的CLR版本。 You automatically get the right one. 您会自动获得正确的一个。

So it solves the exact problem you are trying to solve :) No coincidence of course. 因此,它可以解决您要解决的确切问题:)当然,这并非巧合。 Hosting the CLR yourself has the advantage that you can call any .NET code, not just the kind that's [ComVisible]. 自己托管CLR的好处是,您可以调用任何.NET代码,而不仅仅是[ComVisible]类型。 It isn't otherwise a way to avoid having to learn how to write COM code, the hosting interfaces and the primary .NET interfaces you need to call .NET code (like _Assembly) are also COM based. 否则,这不是避免不必学习如何编写COM代码的方法,您需要调用.NET代码(例如_Assembly)的宿主接口和主要.NET接口也是基于COM的。 Using C instead of C++ is cruel and unusual punishment, best avoided. 最好避免使用C而不是C ++进行残酷和异常的惩罚。

To answer your question: 1. You definitely can host a 2.0 in your process. 要回答您的问题: 1.您绝对可以在过程中托管2.0。 Jeffrey Richter recommends using CorBindToRuntimeEx Function in the 2.0 edition of his CLR via C#. Jeffrey Richter建议在他的CLR 2.0版中通过C#使用CorBindToRuntimeEx Function This got deprecated in the latest versions of the framework, hence the confusion. 在最新版本的框架中不赞成使用此方法,因此造成了混乱。 I would recommend going this way. 我建议这样做。

2. COM interop - to make it work properly you actually need to change your library, add some attributes, then register the resulting COM object, etc. I am pretty sure it adds references to some marshallers and other COM related stuff. 2. COM互操作-要使其正常工作,您实际上需要更改您的库,添加一些属性,然后注册生成的COM对象,等等。我很确定它为一些编组器和其他与COM相关的东西添加了引用。 I would definitely stay as far as possible from COM. 我一定会尽量远离COM。 The shortcomings of this technology was one of the reasons to come up with .Net in the first place. 这项技术的缺点是首先提出.Net的原因之一。 And even more so if you ship these dll's to anyone. 甚至更多,如果您将这些dll交付给任何人。

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

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