如何将 VB6 代码转换为 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/179605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert VB6 code to C#?
提问by basl
Does anyone know a way to convert from VB6 code to C#?
有谁知道从 VB6 代码转换为 C# 的方法?
Is there a tool that can do this for me?
有没有工具可以为我做到这一点?
回答by theraccoonbear
VisualStudio offers (or at least offered) a wizard to do a conversion from VB6 to VB.NET (which could then be converted to C# with a bit of work, possibly helped by #develop's VB.NET <-> C# converter), but when last I used it, for anything non-trivial there was a lot of manual work needing to be done so I suspect you're probably better rewriting or porting by hand if this is a large and/or important application.
VisualStudio 提供(或至少提供)一个向导来进行从 VB6 到 VB.NET 的转换(然后可以通过一些工作将其转换为 C#,可能在#develop 的 VB.NET <-> C# 转换器的帮助下),但是当我最后一次使用它时,对于任何重要的事情都需要做很多手动工作,所以我怀疑如果这是一个大型和/或重要的应用程序,您可能最好手动重写或移植。
回答by sebagomez
Open your project with a new version of Visual Studio, convert your code to VB.Net and then download .Net Reflectorto help you with the C# transformation.
使用新版本的 Visual Studio 打开您的项目,将您的代码转换为 VB.Net,然后下载.Net Reflector以帮助您进行 C# 转换。
Good luck!
祝你好运!
回答by Esteban Brenes
Artinsoft(Now renamed to Mobilize.Net) does just this, especifically the Visual Basic Upgrade Companion.
Artinsoft(现在更名为Mobilize.Net)就是这样做的,尤其是Visual Basic Upgrade Companion。
However, even after using the VBUC there's still some parts that of the system that needs to be migrated/proofed by hand. But it's usually a much smaller set of the original problem. And some of the migration issues have been resolved thanks to experience with past migrations.
但是,即使在使用 VBUC 之后,系统的某些部分仍然需要手动迁移/校对。但它通常是原始问题的一个小得多的集合。由于过去的迁移经验,一些迁移问题已经得到解决。
Artinsoft is the same company that built the wizard that ships with Visual Studio, mentioned in theraccoonbear's post. However, if I'm not mistake the wizard only migrates VB6 to VB.Net.
Theraccoonbear 的帖子中提到,Artinsoft 是构建 Visual Studio 附带的向导的同一家公司。但是,如果我没记错的话,向导只会将 VB6 迁移到 VB.Net。
Full disclosure: I work for Artinsoft
完全披露:我为 Artinsoft 工作
回答by Derek Smyth
It might come across as a little bit cheeky but your brain might be the best tool to use. Could be worth the re-write.
这可能会让人觉得有点厚颜无耻,但你的大脑可能是最好的工具。可能值得重写。
Maybe you don't need to porting it. Could the VB6 be turned into a COM component? Port to COM, call from C#, home in time for Judge Judy.
也许你不需要移植它。VB6 可以变成 COM 组件吗?端口到 COM,从 C# 调用,及时返回 Judy 法官。
回答by Scott and the Dev Team
回答by RS Conley
The short answer is that VB6 and VB.NET (and consequently C#) are separate language but related languages. There are many subtle (Integer being Int32 instead of Int16) and gross differences (graphics, form, and printing engines) between the two platforms. You need to treat this as if you are converting to a completely different platform.
简短的回答是 VB6 和 VB.NET(以及 C#)是独立的语言但相关的语言。两个平台之间存在许多细微的(整数是 Int32 而不是 Int16)和明显的差异(图形、表单和打印引擎)。您需要将此视为您正在转换到一个完全不同的平台。
回答by Kris Erickson
As someone who has done this a bunch of times, this is not an easy process. You can use the VB6 to VB.Net tool as stated in this answer, and then use either Reflector or SharpDevelop to convert to C#. With the SharpDevelop conversion, a few caveats. It screws up all the array references and thinks that they are function calls, and all the logical operators are converted to bitwise logical operators (And becomes & not &&). With Reflector you lose a bunch of stuff. Also the Visual Studio converter fails on a lot of large projects, just hangs and never completes.
作为已经这样做了很多次的人,这不是一个容易的过程。您可以使用本答案中所述的 VB6 to VB.Net 工具,然后使用 Reflector 或 SharpDevelop 转换为 C#。对于 SharpDevelop 转换,有一些注意事项。它搞砸了所有的数组引用并认为它们是函数调用,并将所有逻辑运算符转换为按位逻辑运算符(And 变为 & 非 &&)。使用 Reflector,您会丢失很多东西。此外,Visual Studio 转换器在许多大型项目中失败,只是挂起并且永远不会完成。
Once you have got your code converted into C#, you have to start the real work. The conversion gets you at best 50% of the way there, you have to fix a ton of stuff (you will see your code littered with TODO's), refactor a ton of stuff, and at the end you are left with C# that is a representation of your VB6 -- unless you have very nice VB6 code not a place you really want to be. Also all of your code with be littered with the VB helpers rather than using proper DotNet functions (all the string functions are helpers rather than class objects, for examples0. If you used Variants at all those all have to be rewritten. If you used a lot of API calls, they tend to need rewritting.
一旦将代码转换为 C#,就必须开始真正的工作。转换最多只能让你完成 50% 的工作,你必须修复大量的东西(你会看到你的代码里堆满了 TODO),重构了大量的东西,最后你只剩下 C#,这是一个你的 VB6 的表示——除非你有非常好的 VB6 代码而不是你真正想要的地方。此外,您的所有代码都充斥着 VB 帮助程序,而不是使用正确的 DotNet 函数(所有字符串函数都是帮助程序而不是类对象,例如example0。如果您使用了所有变体,则必须重写所有这些。如果您使用了很多 API 调用,它们往往需要重写。
In the end you will get a base, but converting a large project (20-30 forms, 30 classes, 30 modules) can take several man months. Rewritting from scratch, however, may take twice as long and you lose all of your business logic. So, it can be done (I have done it with 3 or 4 large projects), but there is no panacea, no silver bullet, and any tool that says it will do it for you alone, is lying.
最终您将获得一个基础,但转换一个大型项目(20-30 个表单、30 个类、30 个模块)可能需要几个月的时间。但是,从头开始重写可能需要两倍的时间,并且您会丢失所有业务逻辑。所以,它是可以做到的(我已经用 3 或 4 个大型项目做到了),但是没有万能药,没有灵丹妙药,任何说它可以单独为您完成的工具都是在撒谎。
回答by MarkJ
One question to ask yourself is, how important is it to have C# rather than VB.NET? If you're converting a large application, perhaps you should just upgrade to VB.NET, which will be cheaper.
要问自己的一个问题是,拥有 C# 而不是 VB.NET 有多重要?如果您要转换大型应用程序,也许您应该升级到 VB.NET,这会更便宜。
There are some features in VB6 with no exact equivalent in C# (the error handling, late binding etc). Ask yourself why it's worth the manual effort to convert those, when you could go to VB.NET which does support them.
VB6 中有一些功能在 C# 中没有完全等效的功能(错误处理、后期绑定等)。问问自己为什么值得手动转换它们,当您可以转到支持它们的 VB.NET 时。
And if you don't believe me (and no-one's voted for my answer, sob sob!) maybe you'll believe Francesco Balenaand Dan Appleman (in a book Moving to VB.NET) who agree with me.
如果你不相信我(没有人投票支持我的答案,呜咽!)也许你会相信Francesco Balena和 Dan Appleman(在《Moving to VB.NET》一书中)同意我的观点。
回答by mark
At GreatMigrations, we do this by hand -- we use our hands to develop a VB6/COM compiler. The compiler reads a VBP (or group of related VBPs) and referenced COM libraries and constructs a "semantic model". This model is an extremely detailed set of symbol tables and opcode streams that fully and accurately describe all information structures defined and operations performed by the source system.
在 GreatMigrations,我们是手工完成的——我们用手开发 VB6/COM 编译器。编译器读取一个 VBP(或一组相关的 VBP)并引用 COM 库并构建一个“语义模型”。该模型是一组极其详细的符号表和操作码流,可以完整准确地描述源系统定义的所有信息结构和执行的操作。
The tool then applies a series of algorithms that modify and restructure the model to make it more compatible with .NET and optionally applies custom transformations; for example to replace COM components with .NET classes. The transformation rules are designed by people and created "by hand" to meet their unique requirements.
然后,该工具应用一系列算法来修改和重构模型,使其与 .NET 更兼容,并可选择应用自定义转换;例如用 .NET 类替换 COM 组件。转换规则由人们设计并“手工”创建,以满足他们的独特要求。
Finally, the model is "executed" but instead of allocating the data model and executing the operations, we author the file structures, data model, and operations in the notation of the target platform. We can author both VB.NET and C# using this approach. We also use this process to migrate ASP sites to ASPX, (we also have a multi-dialect FORTRAN-to-C product)
最后,模型被“执行”,但不是分配数据模型和执行操作,而是用目标平台的符号编写文件结构、数据模型和操作。我们可以使用这种方法编写 VB.NET 和 C#。我们也使用这个过程将 ASP 站点迁移到 ASPX,(我们还有一个多方言 FORTRAN-to-C 产品)
The approach does not produce production ready software on the first try, but it can be improved and made to produce "better" software by modifying the configuration of the tool and re-running the translation process. The advantage of our tool is its repeatability, speed, and flexibility which facilitates an agile tool-assisted-rewrite methodology. IMO, migration teams that leverage our approach will benefit from allocating a lot more resources to evaluating alternatives for redesign, and development process improvements rather than to re-gathering functional requirements, hand coding, and testing. They will ultimately enjoy a more maintainable system and a much more faithful reproduction of the original application functionality after the migration as well as greater control and visibility during the migration.
该方法不会在第一次尝试时生成生产就绪软件,但可以通过修改工具的配置并重新运行翻译过程来改进并使其生成“更好”的软件。我们工具的优势在于其可重复性、速度和灵活性,这有助于实现敏捷的工具辅助重写方法。IMO,利用我们方法的迁移团队将受益于分配更多资源来评估重新设计和开发过程改进的替代方案,而不是重新收集功能需求、手动编码和测试。他们最终将在迁移后享受更易于维护的系统和更忠实地再现原始应用程序功能以及在迁移过程中更好的控制和可见性。
A trial edition of our tool is available here gmStudio Trail Request Download. It will allow you to experiment with ASP/VB codes up to 10K LOC and runs for up to 30 sessions.
我们工具的试用版可在此处获得gmStudio Trail 请求下载。它将允许您试验高达 10K LOC 的 ASP/VB 代码并运行多达 30 个会话。
回答by Konstantin Spirin
Microsoft recommends ArtinSoft's Visual Basic Upgrade Companion (VBUC).
Microsoft 推荐ArtinSoft 的 Visual Basic Upgrade Companion (VBUC)。
Microsoft and ArtinSoft have partnered to offer a free VBUC license to you. The discount code below can be used to purchase a license that will allow you to migrate an application of up to 10,000 lines of VB6 code to VB.NET or C# for free. It can also be applied when purchasing a larger license.
Microsoft 和 ArtinSoft 已合作为您提供免费的 VBUC 许可证。下面的折扣代码可用于购买许可证,允许您将最多 10,000 行 VB6 代码的应用程序免费迁移到 VB.NET 或 C#。它也可以在购买更大的许可证时应用。
It's not perfect, resulting code still requires lots of manual fixes until it even compiles but still this tool is worth giving a shot.
它并不完美,生成的代码在编译之前仍然需要大量手动修复,但这个工具仍然值得一试。