////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This is a part of the COM Serialization SDK VC++ Library.
//  
// Copyright 2009 BPM Microsystems
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//                                                                                                                    //
// $Header: //sesca/src/Winprog/COMSerializationSDK/VC++/HeaderLib/ComSerSdk/rcs/Debug.h 1.2 2009/06/03 09:37:56 arturoc Exp $
//                                                                                                                    //
///////// 120 columns, Tab = Insert 3 Spaces ///////////////////////////////////////////////////////////////////////////

/**
   This header file defines the ComSerSdk::PrepareDebugBreak() debug helper.
*/
#pragma once

#include "Atl.h"

namespace ComSerSdk
{
namespace Detail
{

/**
   http://www.codeproject.com/KB/debug/DebugBreakAnyway.aspx
*/
inline void __stdcall PreDebugBreakAnyway()
{
   if (IsDebuggerPresent())
    {
        // We're running under the debugger.

        // There's no need to call the inner DebugBreak

        // placed in the two __try/__catch blocks below,

        // because the outer DebugBreak will

        // force a first-chance exception handled in the debugger.

        return;
    }

    __try
    {
        __try
        {
            DebugBreak();
        }
        __except ( UnhandledExceptionFilter(GetExceptionInformation()) )
        {
            // You can place the ExitProcess here to emulate work 

            // of the __except block from BaseStartProcess

            // ExitProcess( 0 );

        }
    }
    __except ( EXCEPTION_EXECUTE_HANDLER )
    {
        // We'll get here if the user has pushed Cancel (Debug).

        // The debugger is already attached to our process.

        // Return to let the outer DebugBreak be called.

    }
}
} //end namespace detail

/**
   See "VC++ HEADER-ONLY LIBRARY	| Debugging" in the COM Serialization SDK documentation.
*/
inline void PrepareDebugBreak()
{
   Detail::PreDebugBreakAnyway();
}
}// end namespace ComSerSdk