Solved

Hook For All exceptions in my application...

Posted on 2000-02-23
11
218 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 106
Add content to output file 4 58
iSeries DB2 Query 2 84
fix34  challenge 9 97
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

20 Experts available now in Live!

Get 1:1 Help Now