Click here to Skip to main content
15,885,546 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.3K   4.4K   170  
This article shows how to use CrashRpt error reporting library with an MFC application
<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>CrashRpt: CrAutoInstallHelper Class Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>

<table border="0" bgcolor="#FFFFFF" cellspacing="5" width="100%">
 <tr>
  <td width="24px" rowspan="2"><img src="../logo.png" alt="Logo" /></td>
  <td><font family="Arial" size="+2">crashrpt</font></td>
  <td rowspan="2" align="right"><a href="http://sourceforge.net/donate/index.php?group_id=279722"><img src="../donate_small.png" alt="Donate" /></a></td>
 </tr>
 <tr>
  <td colspan="2"><i>A crash reporting system for Windows applications</i></td>
 </tr>


</table>


</body>
<!-- Generated by Doxygen 1.5.9 -->
<script type="text/javascript">
<!--
function changeDisplayState (e){
  var num=this.id.replace(/[^[0-9]/g,'');
  var button=this.firstChild;
  var sectionDiv=document.getElementById('dynsection'+num);
  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
    sectionDiv.style.display='block';
    button.src='open.gif';
  }else{
    sectionDiv.style.display='none';
    button.src='closed.gif';
  }
}
function initDynSections(){
  var divs=document.getElementsByTagName('div');
  var sectionCounter=1;
  for(var i=0;i<divs.length-1;i++){
    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
      var header=divs[i];
      var section=divs[i+1];
      var button=header.firstChild;
      if (button!='IMG'){
        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
        button=document.createElement('img');
        divs[i].insertBefore(button,divs[i].firstChild);
      }
      header.style.cursor='pointer';
      header.onclick=changeDisplayState;
      header.id='dynheader'+sectionCounter;
      button.src='closed.gif';
      section.id='dynsection'+sectionCounter;
      section.style.display='none';
      section.style.marginLeft='14px';
      sectionCounter++;
    }
  }
}
window.onload = initDynSections;
-->
</script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Contents</span></a></li>
      <li><a href="modules.html"><span>API&nbsp;Reference</span></a></li>
      <li><a href="files.html"><span>File&nbsp;Reference</span></a></li>
    <li>
      <form action="search.php" method="get">
        <table cellspacing="0" cellpadding="0" border="0">
          <tr>
            <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
            <td><input type="text" name="query" value="" size="20" accesskey="s"/></td>
          </tr>
        </table>
      </form>
    </li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>CrAutoInstallHelper Class Reference<br>
<small>
[<a class="el" href="group___crash_rpt_wrappers.html">CrashRpt Wrapper Classes</a>]</small>
</h1><!-- doxytag: class="CrAutoInstallHelper" -->Installs exception handlers in constructor and uninstalls in destructor.  
<a href="#_details">More...</a>
<p>
<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="1e8d5b169a55217628d3beee88b9cad0"></a><!-- doxytag: member="CrAutoInstallHelper::CrAutoInstallHelper" ref="1e8d5b169a55217628d3beee88b9cad0" args="(PCR_INSTALL_INFOA pInfo)" -->
&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_cr_auto_install_helper.html#1e8d5b169a55217628d3beee88b9cad0">CrAutoInstallHelper</a> (PCR_INSTALL_INFOA pInfo)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Installs exception handlers to the caller process. <br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="62eec1967dd10594da2e915612df06a9"></a><!-- doxytag: member="CrAutoInstallHelper::CrAutoInstallHelper" ref="62eec1967dd10594da2e915612df06a9" args="(PCR_INSTALL_INFOW pInfo)" -->
&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_cr_auto_install_helper.html#62eec1967dd10594da2e915612df06a9">CrAutoInstallHelper</a> (PCR_INSTALL_INFOW pInfo)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Installs exception handlers to the caller process. <br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="9bb33f0919881705443b02f127952536"></a><!-- doxytag: member="CrAutoInstallHelper::~CrAutoInstallHelper" ref="9bb33f0919881705443b02f127952536" args="()" -->
&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_cr_auto_install_helper.html#9bb33f0919881705443b02f127952536">~CrAutoInstallHelper</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Uninstalls exception handlers from the caller process. <br></td></tr>
<tr><td colspan="2"><br><h2>Data Fields</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="4face3f75d00435626fe3e892bace6f6"></a><!-- doxytag: member="CrAutoInstallHelper::m_nInstallStatus" ref="4face3f75d00435626fe3e892bace6f6" args="" -->
int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_cr_auto_install_helper.html#4face3f75d00435626fe3e892bace6f6">m_nInstallStatus</a></td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Install status. <br></td></tr>
</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
Installs exception handlers in constructor and uninstalls in destructor. 
<p>
<dl class="remark" compact><dt><b>Remarks:</b></dt><dd>Use this class to easily install/uninstall exception handlers in you <b>main()</b> or <b>WinMain()</b> function.</dd></dl>
This wrapper class calls <a class="el" href="group___crash_rpt_a_p_i.html#g24acb589d629460590d85735e12b5337" title="Character set-independent mapping of crInstallW() and crInstallA() functions.">crInstall()</a> in its constructor and calls <a class="el" href="group___crash_rpt_a_p_i.html#gb5ff3a104014a1e138878a1882822442" title="Unsinstalls exception handlers previously installed with crInstall().">crUninstall()</a> in its destructor.<p>
Use <a class="el" href="class_cr_auto_install_helper.html#4face3f75d00435626fe3e892bace6f6" title="Install status.">CrAutoInstallHelper::m_nInstallStatus</a> member to check the return status of <a class="el" href="group___crash_rpt_a_p_i.html#g24acb589d629460590d85735e12b5337" title="Character set-independent mapping of crInstallW() and crInstallA() functions.">crInstall()</a>.<p>
Example:<p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">    #include &lt;<a class="code" href="_crash_rpt_8h.html" title="Defines the interface for the CrashRpt.DLL.">CrashRpt.h</a>&gt;</span>

    <span class="keywordtype">void</span> main()
    {      
      <a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html">CR_INSTALL_INFO</a> info;
      memset(&amp;info, 0, <span class="keyword">sizeof</span>(<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html">CR_INSTALL_INFO</a>));
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#9867d0b22e6f50ac3db844c297f230db" title="Size of this structure in bytes; must be initialized before using!">cb</a> = <span class="keyword">sizeof</span>(<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html">CR_INSTALL_INFO</a>);  
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#626bed9c6b114c92e3bcfa16990d6c0c" title="Name of application.">pszAppName</a> = _T(<span class="stringliteral">"My App Name"</span>);
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#4e341f0ae33c88f65e770a69dbba300c" title="Application version.">pszAppVersion</a> = _T(<span class="stringliteral">"1.2.3"</span>);
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#3ae88305c70b042bf0ea4a0a5131d4fd" title="Subject of crash report e-mail.">pszEmailSubject</a> = <span class="stringliteral">"Error Report from My App v.1.2.3"</span>;
      <span class="comment">// The address to send reports by E-mail</span>
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#e1eb10fda8115ad82cee692d2837018d" title="E-mail address of crash reports recipient.">pszEmailTo</a> = _T(<span class="stringliteral">"myname@hotmail.com"</span>);  
      <span class="comment">// The URL to send reports via HTTP connection</span>
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#9708cd90ec937ff61ffdbc9f5dbb94c9" title="URL of server-side script (used in HTTP connection).">pszUrl</a> = _T(<span class="stringliteral">"http://myappname.com/utils/crashrpt.php"</span>); 
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#e05bbd707bd0dd3f778cf8c0db7f824a" title="User crash callback.">pfnCrashCallback</a> = CrashCallback; 
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#01d5bd46223beabcced7329662ef9837" title="Array of error sending transport priorities.">uPriorities</a>[<a class="code" href="_crash_rpt_8h.html#7cb7df55f0fa979d1d4f5899e64e81e1" title="Send error report via HTTP (or HTTPS) connection.">CR_HTTP</a>] = 3; <span class="comment">// Try HTTP first</span>
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#01d5bd46223beabcced7329662ef9837" title="Array of error sending transport priorities.">uPriorities</a>[<a class="code" href="_crash_rpt_8h.html#8a9140d2d3fcc6c1903e6ebe4bb8843d" title="Send error report via SMTP connection.">CR_SMTP</a>] = 2; <span class="comment">// Try SMTP second</span>
      info.<a class="code" href="struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html#01d5bd46223beabcced7329662ef9837" title="Array of error sending transport priorities.">uPriorities</a>[<a class="code" href="_crash_rpt_8h.html#0874e8ec6bad8eee3121cd8044f2a5bb" title="Send error report via simple MAPI (using default mail client).">CR_SMAPI</a>] = 1; <span class="comment">// Try system email program last</span>

      <span class="comment">// Install crash reporting</span>
      <a class="code" href="class_cr_auto_install_helper.html" title="Installs exception handlers in constructor and uninstalls in destructor.">CrAutoInstallHelper</a> cr_install_helper(&amp;info);
      <span class="comment">// Check that installed OK</span>
      assert(cr_install_helper.m_nInstallStatus==0);

      <span class="comment">// Your code follows here ...</span>

    }
</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Sat Oct 22 17:37:44 2011 for CrashRpt by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>
</body>
</html>

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