简体   繁体   English

COM 与 .NET 互操作 6

[英]COM Interop with .NET 6

We have a .NET class library written in C# that we're trying to upgrade to .NET 6 from .NET Framework v2.0.我们有一个用 C# 编写的 .NET class 库,我们正在尝试从 .NET Framework v2.0 升级到 .NET 6。 With .NET 2.0, the library is instantiated/consumed via C++ code which imports the library's.TLB.使用 .NET 2.0,库通过导入库的 .TLB 的 C++ 代码实例化/使用。

In order to consume .NET 6 libraries using COM, you now have to add attributes to your C# interfaces/classes yourself, as well as the.csproj, so that an extra DLL called.comhost.dll is created at compile time, which you then register using regsvr32 as per https://learn.microsoft.com/en-us/do.net/core/native-interop/expose-components-to-com .为了使用 COM 使用 .NET 6 个库,您现在必须自己向 C# 接口/类以及 .csproj 添加属性,以便在编译时创建一个名为 .comhost.dll 的额外 DLL,然后您根据https://learn.microsoft.com/en-us/do.net/core/native-interop/expose-components-to-com使用 regsvr32 注册。 Using type libraries is no longer supported out of the box but I tried generating a TLB using the "dscom" utility here https://github.com/dspace-group/dscom .开箱即用不再支持使用类型库,但我尝试使用此处的“dscom”实用程序生成 TLB https://github.com/dspace-group/dscom Unfortunately it kept failing with an error saying System.Windows.Forms could not be found and having spent a couple of days trying to get it work, I've given up.不幸的是,它一直失败,并出现错误提示 System.Windows.Forms 找不到,我花了几天时间试图让它工作,但我放弃了。

Microsoft's tutorial doesn't suggest the best way of instantiating the newly created COM object, so I'm wondering if it's a case of going back to the old methods of calling CoCreateInstance or LoadLibrary, or if there's a better way?微软的教程没有建议实例化新创建的COM object的最佳方法,所以我想知道是否回到调用CoCreateInstance或LoadLibrary的旧方法,或者是否有更好的方法? It would've been nice if the TLB-based approach still worked.如果基于 TLB 的方法仍然有效,那就太好了。

Just an update on this.只是对此的更新。 I managed to generate a type library using the dscom utility.我设法使用 dscom 实用程序生成了一个类型库。 I had to provide an additional argument called "--asmpath" to point to the location of the .NET runtime assemblies (in this case C:\Program Files\do.net\shared\Microsoft.WindowsDesktop.App\6.0.0) that my assembly depended on, and then it worked.我必须提供一个名为“--asmpath”的附加参数来指向 .NET 运行时程序集的位置(在本例中为 C:\Program Files\do.net\shared\Microsoft.WindowsDesktop.App\6.0.0)我的程序集依赖于它,然后它起作用了。

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

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