1
Vote

Strange floating point behaviour when accessing Single values is vb6

description

Hi there,

i think i found an error in the interop toolkit behaviour. I hope you can help me.
I have an example Project with a single InteropUserControl (a Windows Forms Button) which is embedded on vb6 form.
The Button is doing the following in his click event:
        double b = double.MinValue;
        double c = b - 100;
In the vb6 form i have a timer which is doing the following calculation
Dim a As Single
Dim b As Single
Dim c As Single
a = 5
b = 3
c = a / b

When the interop button is clicked, the vb6 application crashes with an error (runtime error 6: overflow) while accessing one of the single values.
It seems that the overflow on the .NET side of the interop is leading to an overflow on the vb6 side, the next time a single variable is accessed.

Is there anything i can do about? I already tried to read the fpu registers, because it seems that the exceptions are somehow not masked on the .NET side, but found nothing.

file attachments

comments

HolisticDeveloper wrote Sep 14, 2015 at 3:40 PM

I'm not sure that this is related to the Interop Forms Toolkit as it could be a general interop issue... Also, if I write a simple C# program with that code, it doesn't throw an OverflowException so I'm not sure how that could be getting passed back to VB6?

It also does not appear that the .NET code is throwing any exceptions as wrapping the button1_Click code in a try/catch doesn't catch anything.

I do see that you are using the _controlfp_s native method. I'm not familiar with it, but it does seem to have some effect on floating point operations. Perhaps calling that is causing the error in the VB6 code?

Poolitzer80 wrote Sep 15, 2015 at 6:50 AM

Sorry, when my example was too complicated. The second project should be more easy too understand.
Its now just a simple button click, with a call to double.Min (causing an underflow).

The error occurs however only once at the first click of the application. You have to restart visual basic sdk to reproduce the error then.

In .NET there are no Overflow Exceptions, these are caught by the framework and "masked". In VB6 there is a different floating point arithmetic, so overflow exceptions are NOT masked.

It seems that there are certain states, when the forms toolkit has such a overflow exception, the exception mask is not resetted in the transition from managed to unmanaged code.

However, when using breakpoints in the vb6 development kit, the error does not occur. So it has to do something with timing too.

wrote Sep 15, 2015 at 6:52 AM

Poolitzer80 wrote Sep 15, 2015 at 6:52 AM

Here is the simple project: