简体   繁体   English

将 C++ COM object 更新为 C#

[英]Update C++ COM object to C#

We have a COM interface declared and implemented in unmanaged C++.我们有一个在非托管 C++ 中声明和实现的 COM 接口。 We no longer wish to maintain this assembly, but do need to make some changes, and still be backward compatible.我们不再希望维护此程序集,但确实需要进行一些更改,并且仍然向后兼容。

What I want to do, is to write all the new code in C#, and make it ComVisible.我想做的,就是把所有的新代码都写在C#中,并使其ComVisible。 From this thread I see a reference to TreatAs to replace the old interface.这个线程中,我看到了对 TreatAs 的引用来替换旧界面。

I was thinking along the same path as Jonathan Peppers regarding ProgId, Guid, etc. I will implement the exact interface as the old version as a wrapper to the new implementation.在 ProgId、Guid 等方面,我的想法与 Jonathan Peppers 相同。我将实现与旧版本完全相同的接口,作为新实现的包装器。 I am also thinking about adding MarshalAs attributes exactly as in the generated interop wrapper to be sure the data types will be the same, if possible.我也在考虑添加 MarshalAs 属性,就像在生成的互操作包装器中一样,以确保数据类型相同,如果可能的话。

Are there anything else I should consider while working with this?在处理这个问题时我还有什么需要考虑的吗? Anyone with experience doing this conversion?有做过这种转换经验的人吗?

EDIT: I do have the IDL file for the server.编辑:我确实有服务器的 IDL 文件。 I am not sure if there is a way I can auto generate the code based on this.我不确定是否有一种方法可以基于此自动生成代码。 COM is not something I'm very familiar with. COM 不是我很熟悉的东西。

EDIT Q: How should I deal with HRESULT used by existing clients?编辑问:我应该如何处理现有客户使用的 HRESULT?

ADDED: Figured I should point other readers to a different fix , which is not available for my scenario as I can't recompile all the .NET applications using the existing com:补充:认为我应该将其他读者指向不同的修复程序,这不适用于我的场景,因为我无法使用现有的 com 重新编译所有 .NET 应用程序:

Bjørnar Sundsbø比约纳尔·松兹伯

One quick way to get started:一种快速入门的方法:

  • Import your existing COM object into C# with tlbimp (or Visual Studio)使用tlbimp (或 Visual Studio)将您现有的 COM object 导入 C#
  • Load up .Net Reflector and get the generated interface to put in your new project加载.Net Reflector并获取生成的界面以放入您的新项目中
  • Implement the generated interface in C#在C#中实现生成的接口
  • Switch any import declarations to export instead将任何进口声明改为出口

This should get all your types and method signatures correct from the start.这应该从一开始就使您的所有类型和方法签名正确。 It is a great starting point for porting an existing COM interface to C#.这是将现有 COM 接口移植到 C# 的一个很好的起点。

It seems to be by design to prevent managed COM object from being used from a managed client.似乎为了防止从托管客户端使用托管 COM object。

Here are some links to the same problem , as well as this thread , providing some solutions.这里有一些相同问题的链接,以及这个线程,提供了一些解决方案。 Also take a look at the answer Jonathan Peppers provided to see how to start out if you only need to use it from unmanaged applications.另请查看 Jonathan Peppers 提供的答案,了解如果您只需要从非托管应用程序中使用它,如何开始。

The only way I figure I could work around this, is a messy solution where I create the new code in C#, add a COM layer on top of that.我认为我可以解决这个问题的唯一方法是一个混乱的解决方案,我在 C# 中创建新代码,在其之上添加一个 COM 层。 Then create an additional COM layer as unmanaged C++ which accesses the first layer so, that my .NET applications can access it.然后创建一个额外的 COM 层作为非托管 C++ 访问第一层,以便我的 .NET 应用程序可以访问它。 {Unmanaged COM exposing the original interface} => {Managed COM with rewrite of the original logic of the COM}. {非托管 COM 暴露原始接口} => {托管 COM 重写 COM 的原始逻辑}。 All existing applications will then access the "{Unmanaged COM...}".然后,所有现有应用程序都将访问“{Unmanaged COM...}”。 If I get desperate enough, that might be a way.如果我足够绝望,那可能是一种方法。 For now I'm abandoning this approach and looking for other solutions.现在我正在放弃这种方法并寻找其他解决方案。

If you can, recompile your managed application to use the new assembly, do so.如果可以,请重新编译托管应用程序以使用新程序集,这样做。 You can still use that managed COM from VB6, unmanaged C++, etc. If you still need to reference as a COM from managed, you might be able to create new instances using the approach specified in one of the referenced posts , as long as you don't need to create an interop wrapper. You can still use that managed COM from VB6, unmanaged C++, etc. If you still need to reference as a COM from managed, you might be able to create new instances using the approach specified in one of the referenced posts , as long as you不需要创建互操作包装器。

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

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