Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 App + .Net component working as compiled app but not in VB6 IDE

Tags:

.net

interop

vb6

I have a VB6 App that uses a .Net component (via a .tlb reference in the VB6 app) which is working fine when executed as a compiled app, but it produces an error from the VB6 IDE a certain point when it is trying to use the .NET component.

I should note that the error occurs when the .NET component is meant to be invoking a third party reporting component. The error is specific to the reporting component. Something about not being able to cast from String to some other type.

The .tlb is in the same location as the application executable so I don't why there should be a problem. There is a .config for one of the DLLs but it only specifies where the log file should be.

I need to have the application running in the IDE in order to debug and step through the code. What could the problem be? Could the VB6 IDE be looking in a different location for certain DLLs?

like image 825
CJ7 Avatar asked Dec 10 '22 16:12

CJ7


2 Answers

This could be due to a runtime version mismatch. Try the following:

Create a .config file for the IDE (e.g. the EXE name of the IDE executable + .config).

Paste the following in it:

<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
    <requiredRuntime version="v2.0.50727" safemode="true"/>
  </startup>
</configuration>

This will ensure that the .NET 2 runtime is loaded, no matter in which order .NET COM components are activated (some of which may be loaded by the IDE, causing a runtime mismatch in the process).

like image 104
Lucero Avatar answered Jan 11 '23 22:01

Lucero


A casting exception isn't easily explained by a problem with the default directory of a program. Which is about all that ought to be different when you run your app from the VB6 IDE rather than directly.

You'll have to use a debugger to find out what's going wrong. Luckily that's easy when you use VB6. Start by opening your .NET project in Visual Studio. Project + Properties, Debug tab. Select "Start external program", click the button with the dots and navigate to vb6.exe. It should be located in c:\program files\microsoft visual studio\vb98\vb6.exe if you used the default install options.

You can set the "Command line options" setting to the path to your .vbp project so the IDE automatically loads it. Not strictly necessary.

Debug + Exceptions, check the "Common Language Runtime Exception" Thrown check box. Press F5 to start the debugger. The VB6 IDE will now open, load your VB6 project if necessary. Run the project and recreate the failure case. That should cause a breakpoint in the Visual Studio debugger. You may have to switch to it by hand, the taskbar button should be blinking. Use the normal debugging tools to find out why the exception was thrown.

Note that you can also set breakpoints in Visual Studio, useful if you need to single-step through the code to find out what's wrong. When you press F5, the breakpoint indicator turns hollow since the DLL isn't loaded yet. As soon as your VB6 project creates a class object from your .NET code, the DLL will be loaded (visible in the Output window) and the breakpoint indicator will turn solid. If that doesn't happen then you may need to run Regasm.exe with the /codebase option so that the DLL in the project's bin\Debug folder gets registered as the COM server.

like image 21
Hans Passant Avatar answered Jan 11 '23 22:01

Hans Passant