5

I would like to call a method which is written in visual basic 6.0 from c# (visual studio 2008). Is it possible? How would I do it?

Rap
  • 6,851
  • 3
  • 50
  • 88
ratty
  • 13,216
  • 29
  • 75
  • 108

5 Answers5

12

Easiest way to do it is to just compile the VB6 code as an ActiveX DLL. Then you can reference the DLL in your .net project. (Visual studio can reference ActiveX DLLs properly.)

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • 2
    +1. Step-by-step instructions on creating an ActiveX DLL in VB6 are [here](http://stackoverflow.com/questions/3477265/com-dll-in-vb6/3477344#3477344) and there's a [good tutorial in the VB6 manual](http://msdn.microsoft.com/en-us/library/aa229332%28v=VS.60%29.aspx). Then just reference the VB6 ActiveX DLL in your C# project - it will be listed on the COM tab of the Add Reference dialog box. – MarkJ Dec 29 '10 at 14:28
6
  1. Compile your VB6 DLL as activex dll

  2. Register it using -> regsvr32 "Full Name And Path of newly compiled vb6 dll".(use Run Dialog or Command Prompt to register)

  3. In .net Add refrence - select com tab and search this newly registered dll

  4. Now you can use this dll.

Note:

Whenever you do any changes in vb6 code, you have to follow above steps again.

To unregister vb6 dll use regsvr32 "Name and path" /u

welcome to (dll) hell

indiPy
  • 7,844
  • 3
  • 28
  • 39
2

Yes. It is possible. You call it just like you call a method which has been written in Visual Basic. You need a reference to the assembly and then you just call it with the right namespace.

Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
  • 1
    VB6 doesn't create assemblies and doesn't emit .NET metadata. – Ben Voigt Dec 25 '10 at 06:07
  • @Saeed: Seems the question wasn't specific about the version of VB originally. – Ben Voigt Dec 25 '10 at 06:08
  • @Ben Voigt, As I can see It's specific to "visual basic6.0.", other versions is easy, and OP can test it simply :) – Saeed Amiri Dec 25 '10 at 06:09
  • @Saeed: See that "edited 12 minutes ago" (or similar) under the question? See the date on this answer, almost a month ago? – Ben Voigt Dec 25 '10 at 06:10
  • @Ben Voigt, yes It's edited currently, but what you wanna to say to me? my note is true right now till the Steve fix his answer or remove it. – Saeed Amiri Dec 25 '10 at 06:22
  • @Saeed: I'm just saying, I don't think Steve is "sure" that his answer applies to VB6 because I don't think he meant it to apply to VB6. The answer is correct... but it's a correct answer to a different question. – Ben Voigt Dec 25 '10 at 06:24
2

It's possible if and only if the VB6 code is compiled as a COM server.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Is there an option in VB6 to compile it as COM server?? I didn't have VB6.0 but I think it's not as easy make it COM as you think. – Saeed Amiri Dec 25 '10 at 06:24
  • @Saeed: Actually it is pretty simple. There's a checkbox in project options, then you have to make sure the function in question is a method in a class module, not in a code module (and make sure the class module isn't private). I can't be absolutely sure because it's been a long time, but I think that's all that's needed. – Ben Voigt Dec 25 '10 at 06:26
  • How can get COM server for compile the VB6 COde – ratty Dec 25 '10 at 07:11
  • @ratty: Do you have the source code for the VB6 program? Do you have VB6 installed? – Ben Voigt Dec 25 '10 at 16:45
  • @ratty: Ok, did you follow the advice in my earlier comment? Is the function you are trying to call part of a class module (not a code module)? Have you set the project settings to build a COM (OLE) server? – Ben Voigt Dec 27 '10 at 04:06
  • If the VB6 function is inside a code module rather than a class module, how can it be called from .NET? The .NET IntelliSense does not even show it. – AllSolutions May 25 '16 at 23:47
  • @AllSolutions: Was my previous comment unclear? It has to be in a class module to be seen by .NET. The class module can use the code module's procedures if it wants. – Ben Voigt May 25 '16 at 23:51
  • Ok. But I need it in a Code Module as the function has to return a UDT, and this VB6 function needs to be called from C#. If I define it in a class module, then I get run time error "Insufficient memory to continue the execution of the program". Looking at various threads, it looks like if the function is in a Code Module, it might be possible . but then the question is how to call this function from C# – AllSolutions May 25 '16 at 23:58
  • @Ben Voigt, can you please look at this question: http://stackoverflow.com/questions/37261070/calling-a-vb6-dll-function-with-a-complex-user-defined-type-udt-from-c-sharp – AllSolutions May 25 '16 at 23:58
  • So are you saying that a VB6 function in Code Module can NOT be called from C#? – AllSolutions May 25 '16 at 23:59
  • @AllSolutions: Not directly. You can call a function in a VB6 class and that VB6 class can call a code module. I think you need to run the whole combination in the VB6 debugger in order to find out where your invalid memory access is occurring. – Ben Voigt May 26 '16 at 00:02
  • Calling the class function is ok but the class function is not able to return the UDT is the problem. Appreciate if you can take a look at the question link I posted above. – AllSolutions May 26 '16 at 00:08
2

.NET can use your VB6 DLL like any COM DLL.

Just click to "Add reference", then choose the "COM" Tab if your DLL is already registered, or just click the "Browse" Tab in order to select the file directly.

If COM compatible, Visual Studio will automaticly create a COM Interop Assembly that will act as a .NET wrapper to your VB6 DLL.

You will have to deploy your VB6 dll and the Interop assembly with your program.

SeeSoft
  • 91
  • 3