Click here to Skip to main content
15,891,136 members
Articles / Web Development / HTML

Integrating Crash Reporting into Your Application - A Beginners Tutorial

Rate me:
Please Sign up or sign in to vote.
4.91/5 (30 votes)
5 Feb 2012CPOL12 min read 90.6K   4.4K   170  
This article shows how to use CrashRpt error reporting library with an MFC application
/************************************************************************************* 
This file is a part of CrashRpt library.

Copyright (c) 2003, Michael Carruth
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this 
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

* Neither the name of the author nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************************/

#include "stdafx.h"
#include "resource.h"
#include "ErrorReportDlg.h"
#include "ResendDlg.h"
#include "CrashInfoReader.h"
#include "strconv.h"
#include "Utility.h"

CAppModule _Module;
CErrorReportDlg dlgErrorReport;
CResendDlg dlgResend;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int /*nCmdShow*/ = SW_SHOWDEFAULT)
{ 
    LPCWSTR szCommandLine = GetCommandLineW();

    // Split command line
    int argc = 0;
    LPWSTR* argv = CommandLineToArgvW(szCommandLine, &argc);

    // Read the crash info passed by CrashRpt.dll to CrashSender.exe 
    if(argc!=2)
        return 1; // No arguments passed

    // Read crash info
    CString sFileName = CString(argv[1]);
    int nInit = g_CrashInfo.Init(sFileName.GetBuffer(0));
    if(nInit!=0)
    {
        MessageBox(NULL, _T("Couldn't initialize!"), _T("CrashSender.exe"), MB_ICONERROR);
        return 1;
    }

    if(!g_CrashInfo.m_bSendRecentReports)
    {
        // Do the crash info collection work assynchronously
        g_ErrorReportSender.DoWork(COLLECT_CRASH_INFO);
    }

    // Check window mirroring settings 
    CString sRTL = Utility::GetINIString(g_CrashInfo.m_sLangFileName, _T("Settings"), _T("RTLReading"));
    if(sRTL.CompareNoCase(_T("1"))==0)
    {
        SetProcessDefaultLayout(LAYOUT_RTL);  
    }  

    CMessageLoop theLoop;
    _Module.AddMessageLoop(&theLoop);

    if(!g_CrashInfo.m_bSendRecentReports)
    {
        // Create "Error Report" dialog
        if(dlgErrorReport.Create(NULL) == NULL)
        {
            ATLTRACE(_T("Error report dialog creation failed!\n"));
            return 0;
        }
    }
    else
    {
        // Create "Send Error Reports" dialog

        // Check if another instance of CrashSender.exe is running
        ::CreateMutex( NULL, FALSE,_T("Local\\43773530-129a-4298-88f2-20eea3e4a59b"));
        if (::GetLastError() == ERROR_ALREADY_EXISTS)
        {		
            // Another CrashSender.exe already tries to resend recent reports; exit.
            return 0;
        }

        if(g_CrashInfo.GetReportCount()==0)
            return 0; // There are no reports for us to send

        // Check if it is ok to remind user now
        if(!g_CrashInfo.IsRemindNowOK())
            return 0;

        if(dlgResend.Create(NULL) == NULL)
        {
            ATLTRACE(_T("Resend dialog creation failed!\n"));
            return 0;
        }
    }

    // Process window messages
    int nRet = theLoop.Run();

    // Wait until the worker thread is exited  
    g_ErrorReportSender.WaitForCompletion();
    nRet = g_ErrorReportSender.GetGlobalStatus();

    // Remove temporary files we might create and perform other finalizing work
    g_ErrorReportSender.Finalize();

    _Module.RemoveMessageLoop();

    return nRet;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{  
    HRESULT hRes = ::CoInitialize(NULL);
    // If you are running on NT 4.0 or higher you can use the following call instead to 
    // make the EXE free threaded. This means that calls come in on a random RPC thread.
    //	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
    ATLASSERT(SUCCEEDED(hRes));

    // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
    ::DefWindowProc(NULL, 0, 0, 0L);

    AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);	// add flags to support other controls

    hRes = _Module.Init(NULL, hInstance);
    ATLASSERT(SUCCEEDED(hRes));

    int nRet = Run(lpstrCmdLine, nCmdShow);

    _Module.Term();
    ::CoUninitialize();

    return nRet;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
I am a software developer currently living in Tomsk, Russia. I received a PhD degree in Computer Science from Tomsk Polytechnic University in 2010. I have been professionally developing C/C++ and PHP software since 2005. I like contributing to open-source and writing programming articles for popular web resources, like CodeProject. Besides writing, I love skiing and watching Formula-1.

Comments and Discussions