banan01

Hello everyone.
Here is a problem. I have to pass an Exception(error) number from class library written in C# to a routine written in VB6.
Extend Exception class to include Number field, assign some value to it and then throw it so it can be handled by VB.
Problem is that it doesn't seam to work the way I want it to. I change the Number to some different values and every time I receive the same number = -2146233088 (it might mean "managed code casting unhandled exception" but I'm not sure)
Even though that other(native) fields in Exception pass what I tell them to.
Here is the test code that I use:

Code Snippet

C#

using System;
using System.Collections.Generic;
using System.Text;

namespace ExcTestingDLL
{
public class TestException : Exception
{
public long Number;
public TestException()
{
Number = 1000;
}
}
public class TestThingy
{
public void testIt ()
{
TestException e = new TestException();
e.Source = "TestThingy";
e.HelpLink = "google it";
throw e;
}
}
}
------------------------------------------------------------------------

VB6

Private Sub btnOne_Click()
On Error GoTo ErrorHandler
Dim test As TestThingy

Set test = New TestThingy
Call test.testIt
txtOne.Text = "did not work"

ErrorHandler:
txtOne.Text = Err.Number & " " & Err.Source & " " & Err.HelpFile '- vbObjectError
End Sub

THE RESULT IN txtOne --> -2146233088 TestThingy google it


I really need that Number thing, and I need to access it with Err.Number.
Is it possible at all
Any help will be much appreciated.


Re: Visual C# General Passing Exceptions between C# and VB6

V1.0.0.0

http://msdn.microsoft.com/msdnmag/issues/05/01/ExceptionLogging/

This gives some information on issues with cross boundary exception handling and the corresponding workarounds.

Hope it helps....





Re: Visual C# General Passing Exceptions between C# and VB6

banan01

Thanks for your response, but I think that it is not quite what I was looking for. Quote: "Whenever a .NET exception is thrown from .NET components to the ASP page, it's converted into an Err object that contains information on the error. Unfortunately, a wealth of information such as the stack trace, all inner exceptions, and other properties within the .NET exception are lost; only the string description and a number are returned to the ASP page."
And as I was saying, my major problem and goal is to get the error number with Err.Number in VB6 thrown from C# 05. Unfortunately, as far as I know C# doesn't have the Number property in its Exception class.
Any ideas
Thanks.




Re: Visual C# General Passing Exceptions between C# and VB6

BlueBay


The Number property on the VB6 end comes from the HRESULT of the .NET error, so your .NET exception class should look more like this:

public class TestException : Exception
{
public TestException(string Message, int Number) : base(Message)
{
base.HResult = Number;
}
}

Remembering that the only way to set the Exception.Message property is in the constructor, and that the error number has to be negative (otherwise you seem to get a standard "Automation Error" number 440 at the VB6 end.

Hope this helps...




Re: Visual C# General Passing Exceptions between C# and VB6

banan01

Thank you very much, BlueBay!
That was exactly what I was looking for.