如何在VB6和C#之间共享接口? [英] How can I share an interface between VB6 and C#?

查看:74
本文介绍了如何在VB6和C#之间共享接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够编写一个可以在C#和VB6类中实现的类接口,以便可以在VB6代码中以相同的方式处理这些类,但是我无法完成这项工作.

I would like to be able to code a class interface which I can implement in C# and VB6 classes so that these classes can be handled in the same way within VB6 code but I can't make this work.

在VB6中,我希望使用Implements关键字实现VB6Class类,以实现某些接口ISharedInterface.

In VB6 I want to have come class VB6Class using the Implements key word to implement some interface ISharedInterface.

在C#中,我想拥有其他一些C#Class类,可以在实现ISharedInterface的同时向COM公开它.

In C# I want to have some other class C#Class which I can expose to COM as also implementing ISharedInterface.

目标是使VB6代码将能够通过ISharedInterface在VB6Class和C#Class上进行操作,而无需关心类所使用的语言.

The goal is that VB6 code will then be able to operate on VB6Class and C#Class via ISharedInterface without caring which language the classes were built in.

我想通过连续重写VB6端的所有内容来使用该技术作为从VB6迁移的方法,如果我可以在C#中实现VB6中已有的接口,那将是理想的选择.但是失败了,即使我不得不用C#重写一个接口以共享回VB6,这仍然会有用(即,我什至不在乎该接口是用C#编写,暴露给COM还是用COM编写并由用户使用).C#,只要语言障碍的双方都可以引用相同的接口即可.

I want to use this technique as a way to migrate away from VB6 by successively rewriting everything at the VB6 end and if I can implement interfaces I already have in VB6 within C# this would be ideal. But failing that, even if I have to rewrite an interface in C# to share back to VB6 that would still be useful (i.e. I don't even care whether the interface is written in C# and exposed to COM or written in COM and consumed by C#, so long as both sides of the language barrier can reference the same interface).

我发现这很难做到.我可以引用来自COM的C#接口,但不能将其作为COM可见接口导出回COM.如果作为替代方案,我尝试在C#本身中创建一个接口,但我没有找到直接在COM上查看该接口的方法,并且我尝试了间接使用它的各种解决方法,例如创建一个stub类来实现该接口并公开当我尝试实现公开的存根类(即使它们已编译)时,作为COM可见,这只会在VB6中引发运行时错误.

I'm finding this surprisingly hard. I can reference an interface in C# that comes from COM but I can't export it back to COM as a COM visible interface. If, as an alternative, I try to create an interface in C# itself I haven't found a way to see it directly over COM and various workarounds I have tried to use it indirectly, like creating a stub class to implement the interface and exposing that as COM visible just raise run time errors in VB6 when I try to implement the exposed stub class (even though they compile).

我目前对此没有很好的解决方案,只有一个非常笨拙的工作,即在C#和VB6中实现单独的接口,将C#方法直接公开给COM,并在VB6中创建一个包装器类,该包装器仅将接口方法重定向到基本的真实方法.

I currently have no good solution for this and only a horribly clunky work around which is to implement separate interfaces in C# and VB6, expose the C# methods directly to COM and create a wrapper class in VB6 that simply redirects the interface methods to the underlying true methods.

创建单个界面(在VB6或C#中)并且在无需重复界面定义的情况下两种语言都可以引用的最佳方法是什么?

What is the best way to create a single interface (either in VB6 or C#) which both languages can reference without me having to duplicate the interface definition?

推荐答案

创建单个界面(在VB6或C#中)并且在无需重复界面定义的情况下两种语言都可以引用的最佳方法是什么?

What is the best way to create a single interface (either in VB6 or C#) which both languages can reference without me having to duplicate the interface definition?

在IDL中编写接口,并将其编译为VB中引用的类型lib,然后将( tlbimp )导入.NET.(如果要使用VB6定义接口,则需要避免VB6会重新生成IID.)

Write the interface in IDL and compile into a type lib which is referenced in VB and imported (tlbimp) into .NET. (You need to avoid the regeneration of IIDs that VB6 will do if you use it to define the interface.)

您可以在.NET中定义接口,但这将涉及更多步骤.

You could define the interface in .NET, but that would involve more steps.

这篇关于如何在VB6和C#之间共享接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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