0

I am using C# in a desktop application.

I am calling a DLL written in C that I do not have the source code for.

Whenever I call this DLL I get an untrapped error which I trap in an UnhandledException event/delegate.

object reference not set to an instance of an object

But the stack trace is empty.

When I Googled this the info back was that the error was being hanlded eleswhere and then rethrown. But this can only be in the DLL I do not have the source code for.

So, is there anyway I can get more info about this error?

This is my code...

in program.cs...

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    try
    {
        Exception _ex = (Exception)e.ExceptionObject; 
        //the stact trace property is empty here..
    }
    finally
    {
        Application.Exit();
    }
}

My DLL...

[DllImport("AutoSearchDevice.dll", EntryPoint = "Start", ExactSpelling = false,     CallingConvention = CallingConvention.StdCall)]
public static extern int Start(int ASD_HANDLE);

An I call it like so:

public static void AutoSearchStart()
{
    try
    {
        Start(m_pASD);
    }
    catch (Exception ex)
    {

    }
}
BenG
  • 14,826
  • 5
  • 45
  • 60
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

1

My recommendation would be to look at integrating ELMAH

https://code.google.com/p/elmah/

ELMAH will log exceptions in the back ground while an application runs. It is used most often for Web Sites and Services where it is more difficult to see unhandled exceptions but here is a SO article on using it with console and desktop applications.

Using ELMAH in a console application

It should be able to see able to write out all pertinent data from an exception. It is also available through a Nuget package for easy installation, there are many good articles on setting up ELMAH like this one, so try gogling if you have setup questions http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

Word of Caution: you might not be seeing a stack trace if the dll developers we're baddies and caught an exception and threw a new one like so

try
{
//code
}
catch(Exception ex)
{
throw new Exception;
}

If they have done that, or created a new exception and just copied the message over they will have effectively swallowed the details you're looking for. And that stinks, ELMAH may still be able to snipe the exception from the dll, but i don't know that for sure. Good Luck.

Community
  • 1
  • 1
Mabdullah
  • 1,013
  • 6
  • 26