Solved

Hook For All exceptions in my application...

Posted on 2000-02-23
11
221 Views
Last Modified: 2013-11-20
Is there any way that I can trap all my Application Exceptions before anyother handler could handle it.

Want a detailed explanation.
0
Comment
Question by:nv3prasad
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2550479
You can do that using 'SetUnhandledExceptionFilter()':


LONG WINAPI __XceptFilter   (   EXCEPTION_POINTERS* pExp)
{
    LONG    lnRC    =   EXCEPTION_EXECUTE_HANDLER;

    TRACE3  (   "__XceptFilter(): got exception, code == 0x%x @ 0x%x, flags == 0x%x\r\n",  
                            pExp->ExceptionRecord->ExceptionCode,
                ( DWORD)    pExp->ExceptionRecord->ExceptionAddress,
                            pExp->ExceptionRecord->ExceptionFlags
            );

    return( lnRC);
}

// in your code:

    SetUnhandledExceptionFilter (   __XceptFilter);

By calling this API, your function replaces 'UnhandledExceptionFilter()', which will usually signal the exception to the user with one of these 'nice' message boxes and terminate the program.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2552069
But this hooks unhandled exceptions. Your function will be alled only when there is no handler. nv3prasad wanted a crack ant every exception before any other handler had a chance.

..B ekiM
0
 
LVL 86

Expert Comment

by:jkr
ID: 2552137
Thanx for the reminder, Mike...

Should have remembered the good ol' "Think before you post an answer" rule ;-)
0
 
LVL 32

Expert Comment

by:jhance
ID: 2552295
This might do the job, however.  

What exceptions are handled already that  SetUnhandledExceptionFilter won't catch?

(I don't know, I'm just curious about this.  I've used SetUnhandledExceptionFilter and it catches most errors that occur in a running app.)
0
 
LVL 86

Expert Comment

by:jkr
ID: 2552395
You're right jhance, but

__try
{
 char* pc=NULL;

*pc='a';
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}

would handle the exception before it reaches the filter...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:mikeblas
ID: 2552899
As jkr implies, any exception which is actually handled won't reach your installed handler. nv3prasad wanted something to get all exceptions, handled or not.

..B ekiM
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2553013
Adjusted points to 100
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2553014
mikeblas has got my point!

Can anyone provide me a way where I can give the first try to all possible exceptions?

Thank you Mike

Prasad
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 2554329
>>Can anyone provide me a way where I
>>can give the first try to all
>>possible exceptions?

Only debuggers can do that. However, writing such a 'mini' debugger isn't too difficult:

    DWORD       dwStatus;

    HANDLE      hProcess    =   INVALID_HANDLE_VALUE;

    STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb           =   sizeof  (   STARTUPINFO);
    si.dwFlags      =   STARTF_USESHOWWINDOW;
    si.wShowWindow  =   SW_SHOWDEFAULT;

    if  (   !CreateProcess  (   NULL,
                                pszModule,
                                NULL,
                                NULL,
                                FALSE,
                                DEBUG_ONLY_THIS_PROCESS | CREATE_NEW_CONSOLE,
                                GetEnvironmentStrings   (),
                                NULL,
                                &si,
                                &pi
                            )
        )
        {
            return  (   0);
        }

    CloseHandle (   pi.hThread);
    CloseHandle (   pi.hProcess);

    SetDebugErrorLevel  (   SLE_WARNING);


    while   (   WaitForDebugEvent   (   &dbgev, INFINITE))
            {
                dwStatus    =   DBG_CONTINUE;
                *acModule   =   ( char) 0;

                switch  (   dbgev.dwDebugEventCode)
                        {
                            case    EXCEPTION_DEBUG_EVENT       :


                                    wsprintf    (   acBuf,
                                                    "EXCEPTION_DEBUG_EVENT %s, code == 0x%08x at 0x%08x",
                                                    dbgev.u.Exception.dwFirstChance ? "first chance" : "second chance",
                                                    dbgev.u.Exception.ExceptionRecord.ExceptionCode,
                                                    dbgev.u.Exception.ExceptionRecord.ExceptionAddress
                                                );

                                    if  (   EXCEPTION_BREAKPOINT != dbgev.u.Exception.ExceptionRecord.ExceptionCode)
                                            dwStatus    =   DBG_EXCEPTION_NOT_HANDLED;


                                    break;


                            case    RIP_EVENT                   :

                                    wsprintf    (   acBuf,
                                                    "RIP_EVENT, error code == 0x%08x",
                                                    dbgev.u.RipInfo.dwError
                                                );

                                    break;

                            default:    break;
                        }

                printf  (   "PID:%u TID:%u %s\n",
                            dbgev.dwProcessId,  
                            dbgev.dwThreadId,
                            acBuf
                        );

                ContinueDebugEvent  (   dbgev.dwProcessId,  
                                        dbgev.dwThreadId,
                                        dwStatus
                                    );

                if  (   EXIT_PROCESS_DEBUG_EVENT    ==  dbgev.dwDebugEventCode)
                    {
                        break;
                    }
            }

(just a snippet)
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2555045
> Only debuggers can do that.

Now we're getting somewhere!

Aside from writing a debugger to monitor your process, your only other alternative is to write a DLL that replaces and watches the RaiseException() API.

..B ekiM
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2558753
I think Jkr has thrown a lot of light on this and also I thank Mike for his valuable suggestions.

I accept jkr's comment as the answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now