8
votes

I have been stump in this problem for a few hours now. I hope someone has had a similar problem to this.

We have developed a prototype .Net(C#) dll using VS2010, and would like to be able to call this dll in a both C# applications and VB6 application.

My question is:

Is it possible to debug a VB6 application that is calling a .Net dll? I get an error message "Automation Error The system cannot find the file specified"

The error message suggests that there is something missing for my VB6 app to find the .Net dll.

I am aware that if the VB6 application has been compiled, and the .exe has been created, the VB6 will successfully call the .Net dll functionality when using the .exe

However it is important that we can debug through our VB6 application. Unfortunately debugging does not allow you to step over the line of code instantiating the .Net DLL's class object. I can't seem to do this.

NOTE: I have looked around forums and MSDN documentation and I mostly find solution for calling a VB6 dll in .NET; which is unfortunately the opposite of what we need to do.

NOTE: I have already registered the compiled .Net(C#) assembly, and referenced it in the VB6 project.

I have however found these two pages, which seemed to be what we need, but its a solution for calling a .NET(c#) dll generated using VS2005. This doesnt seem to work when the .NET(C#) dll was generated using VS2010.

site1 site2

If someone could give any suggestions or direct me somewhere I can get one, that would be great.

Thanks

SOLUTION Thanks to @HansPassant, I have found the solution. To debug a VB6 project that contains a C# .NET assembly, you need to register the .NET dll through both "regasm" and "gacutil", then make sure to close and reopen the VB6 application before you start debugging.

3
If I understand your question correctly, you want to do the debugging from the VB 6 side (i.e., from the VB 6 IDE)? That's not going to be possible—the VB 6 debugger works only with VB 6 code. It can't handle native code at all, and it certainly can't handle MSIL (.NET) code since that hadn't even been invented yet when VB 6 came out.Cody Gray♦
I guess it depends on the .Net framework you use to compile it. The examples you've found are the opposite since the visual studio versions try to keep the backwards compatibilityluchosrock
wow good point @CodyGray. I didn't figure it that out, you're rightluchosrock
Redeclare your .NET object as "As Object" (temporarily), should work much better.Arvo
@Arvo: Why? What do you think it's declared as now? Why would changing it to As Object make it "work much better"? How is that going to allow you to step into native/MSIL code from VB 6?Cody Gray♦

3 Answers

12
votes

This is not a problem, VB6 uses its own debugger that doesn't get in the way of any other debugger, including the managed one for C# code.

You start from your C# class library project, ensure it is selected as the start project. Project + Properties, Debug tab. Select the "Start external program" option and enter the path to the VB6 IDE. Typically c:\program files\microsoft visual studio\vb98\vb6.exe. Set a breakpoint on the method you want to debug.

Press F5 and the VB6 ide will start running. Load or create your vb6 project. Note how you can add the path to the .vbp project in the previous step so it will automatically load your project.

Start debugging your vb6 project as usual. As soon as it starts using your [ComVisible] C# class then your C# assembly gets loaded. And the breakpoint will hit when your vb6 code calls the method. Switch back and forth as needed. Note that you cannot single-step from vb6 to C# code, you have to set breakpoints to get the debugger to stop.

3
votes

Ah, the wonders of using .NET from VB6 in a debuggable manner.

  • in the VB6 project compile options (reached using the Options button on the Make Project dialog window), choose the Compile to Native Code, No Optimization, and Create Symbolic Debug Info options. Then compile your VB6 project. These options allow proper VB6 binary-to-source mapping.
  • Go to the Configuration Properties...Debugging property page of your solution and change the Start Action to launch your VB6 executable.
  • In VS Solution Explorer, go to File...Add Existing Item and navigate to the folder containing the VB6 source code you want to debug. Double-click on the VB6 source code file you want to debug, and a source window should open in VS that allows you to set breakpoints in the VB6 source code.
  • Make sure that your .NET library has a public default constructor. This is essential.
  • Now also set any C# breakpoints that you need. Do not step into the .NET code - this doesn't work.
  • When you start debugging with VS, your VB6 and C# breakpoints should be hit normally.
1
votes

One approach is to debug each individually:

  • Debugging the VB6 code can be done in the IDE after compiling the C# DLL and adding it as a reference to the VB6 project.

  • Debugging the DLL with the VB6 host is possible in Visual Studio by compiling the VB6 project and using it in the project properties as the executable to run.

In some cases this is simpler/quicker than setting up the environment to debug both simultaneously.

This approach will require having at least the framework of each working beforehand.