vb6 to vb.net dll compatibility version

旧城冷巷雨未停 提交于 2021-01-27 13:24:10

问题


I explain my problem :

I have some program (5-6) in vb6, and a lot of DLL in vb6. We will perform a migration of these DLL and program in Vb.Net.

We want to start with DLL, and each program references 2 or 3 DLL.

I want to perform the migration of the DLL in vb.net (no problem for that) but i want to keep the (binary) compatibility between my old vb6 DLL and the new one in vb.net Because without that i will have to re build each application using this DLL because the compatibility is broken.

I searched a lot and i tried to take the GUID of the vb6 class and forced it in VB.NET with something like :

<Guid("B3F3EF24-F6E8-4XF9-A686-824DXXX2DF0")>
<ProgId("Project.ClsObj")>
 Public Class ClsObj
 ....

And put the option : Make assembly COM-Visible

but even with that if i deploy the vb.net DLL (with the same name of the vb6 DLL) and register it with regasm : the program doesn't work.

If you have some idea, i'll appreciate i don't know if what i wanna do is really possible

Thks


回答1:


Yes, this is not enough. Best way to get ahead with this is to use the OleView.exe utility, run it from the Visual Studio Command Prompt. Use File > View TypeLib and select your VB6 DLL first. You'll see the content of the type library in the right pane. Copy/paste it into a text editor, this is your "master" that your .NET version needs to match exactly. Use File > View Typelib again, this time selecting the .NET .tlb file to compare.

First thing you need to pay attention to are the interfaces, a detail that's completely invisible in VB6. The [uuid] of the interface must match, right now it does not. You can keep the programming style you have now instead of declaring the interface explicitly but then you must use the Microsoft.VisualBasic.ComClassAttribute. It lets you specify the [Guid] of the class, which you already have, but also the [Guid] of the interfaces. One for the [default] interface, another for the [source] interface (if any).

Next detail is the uuid and version number for the type library, also visible from OleView on top of the [library] block. Use Project > Properties > Application > Assembly Information button. Copy the guid and set the first two numbers of the AssemblyVersion to match.


Next you'll need to carefully compare the VB6 and .NET output of OleView and verify that the order and arguments of the interface members exactly match. Small mistakes cause very hard to diagnose runtime failure. Accidentally omitting or swapping two members causes VB6 to call the completely wrong .NET method. Getting an argument wrong causes stack imbalance.

There's a much better way to ensure that this can't go wrong, you can actually use the interface declarations in the VB6 type library in your VB.NET project. Project > Add New Reference > Browse button/tab > select the VB6 executable. Your class should then resemble:

<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
<Guid("B3F3EF24-F6E8-4XF9-A686-824DXXX2DF0")> _
<ProgId("Project.ClsObj")> _
Public Class ClsObj
    Implements _ClsObj

    '' etc..
End Class

Where _ClsObj is the name of the interface you found back in the OleView output. The big, big advantage of doing it this way is that you can be completely sure that your class can always be safely used from existing VB6 code. Which of course matters a lot when you do this incrementally, replacing one VB6 executable at a time.

The disadvantage is that you keep a dependency on the VB6 type library. Be sure to check it into source control. You can eventually get rid of it with a decompiler like Reflector or ILSpy. Ask it to decompile the interface type and copy/paste it into your VB.NET source file. After which you can remove the assembly reference.



来源:https://stackoverflow.com/questions/31246808/vb6-to-vb-net-dll-compatibility-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!