什么是互操作 dll? [英] What is the interop dll?

查看:13
本文介绍了什么是互操作 dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要澄清一下.我有一个使用 Crystal Reports 的 Reportwriter dll.它是用VB6编写的.我必须将此 dll 添加到我的 asp.net 项目中,它会在其中创建一个互操作 dll.

I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It is written in VB6. I have to add this dll to my asp.net project, where it creates an interop dll.

据我了解,interop dll 作为中介存在,因此我的 .net 代码可以与 Reportwriter dll 对话.

To my understanding, the interop dll is there as an intermediary so that my .net code can speak to the Reportwriter dll.

那么我是注册互操作 dll 还是注册原始 dll?

So do I register the interop dll or do I register the original dll?

推荐答案

在VB6中编写代码时,编译结果是一个COM组件.COM 组件提供接口、协同类、结构和枚举,这些通常使用 COM 类型库来描述.但是,要在 .NET 中使用该 COM 组件,您需要采用 .NET 可以理解的格式的类型描述 - 即 .NET 程序集(因为它不能直接与类型库一起使用).因此,互操作程序集只是一个转换后的"COM 类型库,从某种意义上说,它包含与类型库中相同事物相对应的接口、结构等的描述.

When you write code in VB6, the compiled result is a COM component. COM components provide interfaces, coclasses, structs and enums, which are normally described using a COM type library. However, to consume that COM component in .NET, you need type description in a format that .NET understands - that is, a .NET assembly (since it cannot work with type libraries directly). An interop assembly is therefore just a "converted" COM type library, in a sense that it contains descriptions of interfaces, structs etc that correspond to the same things in a type library.

(以上内容有所简化,因为互操作程序集必须从类型库生成 - 例如,如果需要,您可以手动编写代码.)

(The above is somewhat simplified, as interop assembly doesn't have to be produced from a type library - you can hand-code one if you want, for example.)

与常说的相反,互操作程序集不包含任何可执行代码,也不进行任何编组.它只包含类型定义,它唯一可以有方法的地方是在接口中,而接口中的方法没有实现.将 .NET 调用编组到 COM 调用实际上是由 CLR 本身根据从互操作程序集加载的类型描述完成的 - 它动态生成所有必要的代码.

Contrary to what is often said, an interop assembly doesn't contain any executable code, and it doesn't do any marshalling. It only contains type definitions, and the only place where it can have methods is in interfaces, and methods in interfaces don't have an implementation. Marshaling .NET calls to COM ones is actually done by CLR itself based on type descriptions loaded from interop assemblies - it generates all necessary code on the fly.

现在关于你的问题.您需要注册您的 COM DLL(VB6 的输出)项目 - 例如,使用 regsvr32.exe.您不应该(事实上,您不能)以这种方式注册互操作程序集,因为它不是 COM 组件 - 它只是一个普通的 .NET 程序集,因此您可以将它与您的 .exe/.dll 放在同一个文件夹中,或者像往常一样将其放入 GAC.

Now as to your question. You need to register your COM DLL (the output of your VB6) project - for example, using regsvr32.exe. You shouldn't (in fact, you cannot) register an interop assembly that way, because it's not a COM component - it's just a plain .NET assembly, so you can either put it in the same folder with your .exe/.dll, or put it into GAC, as usual.

这篇关于什么是互操作 dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆