I wish to provide the following changes to CrashRpt to support capture to .reg files instead of the usual XML files using the API crAddRegKey.
I made the following additions to the CrashRpt code base to support this:
include\CrashRpt.h:
1. #define CR_AR_USE_REG_FILE_FORMAT 0x2 //!< If this flag is specified, the registry will be included as a .reg file.
2. I also updated the documentation comments in this header file to document this new flag.
reporting\crashrpt\CrashHandler.cpp:
1. In the method DWORD CCrashHandler::PackRegKey added the line:
pKey->m_bRegFileFormat = rki.m_bRegFileFormat;
after the existing line: "pKey->m_bAllowDelete = rki.m_bAllowDelete;"
2. In the method int CCrashHandler::AddRegKey(LPCTSTR szRegKey, LPCTSTR szDstFileName, DWORD dwFlags) added the line:
rki.m_bRegFileFormat = (dwFlags&CR_AR_USE_REG_FILE_FORMAT)!=0;
after the existing line: "rki.m_bAllowDelete = (dwFlags&CR_AR_ALLOW_DELETE)!=0;"
reporting\crashrpt\CrashHandler.h:
1. Added "m_bRegFileFormat = false;" to the RegKeyInfo constructor
2. Added "bool m_bRegFileFormat; // Whether the data be stored as a .reg format file" to the RegKeyInfo struct
reporting\crashrpt\SharedMem.h
1. Added "BOOL m_bRegFileFormat; // Should the data be stored as a .reg format file" to the REG_KEY struct
reporting\crashsender\CrashInfoReader.cpp:
1. In the method int CCrashInfoReader::UnpackCrashDescription(CErrorReportInfo& eri) added the line:
rki.m_bRegFileFormat = pKey->m_bRegFileFormat!=0;
after the existing line: "rki.m_bAllowDelete = pKey->m_bAllowDelete!=0;"
reporting\crashsender\CrashInfoReader.h:
1. Added "m_bRegFileFormat = false;" to the ERIRegKey constructor
2. Added "bool m_bRegFileFormat; // Whether the data be stored as a .reg format file" to the ERIRegKey struct
reporting\crashsender\ErrorReportSender.cpp:
1. In the method BOOL CErrorReportSender::CollectCrashFiles() added the lines:
if (rki.m_bRegFileFormat)
DumpRegKeyToRegFile(sKeyName, sFilePath, sErrorMsg);
else
just before the existing line: "DumpRegKey(sKeyName, sFilePath, sErrorMsg);". This is the key change where we call a new method called DumpRegKeyToRegFile instead of the existing DumpRegKey method.
2. Addition of a new method:
// This method dumps a registry key contents to a reg file
int CErrorReportSender::DumpRegKeyToRegFile(CString sRegKey, CString sDestFile, CString& sErrorMsg)
{
//Patch up the sRegKey parameter to use the format required by REG.EXE
sRegKey.Replace(_T("HKEY_LOCAL_MACHINE"), _T("HKLM"));
sRegKey.Replace(_T("HKEY_CURRENT_USER"), _T("HKCU"));
// Set up process start up info
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
// Set up process information
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
// Format command line
CString sCmdLine;
sCmdLine.Format(_T("REG EXPORT \"%s\" \"%s\" /y"), sRegKey.operator LPCTSTR(), sDestFile.operator LPCTSTR());
// Create process using the command line prepared earlier
BOOL bCreateProcess = CreateProcess(NULL, sCmdLine.GetBuffer(0), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
sCmdLine.ReleaseBuffer();
if (!bCreateProcess)
{
sErrorMsg = _T("Error calling reg.exe to export to file: ");
sErrorMsg += sDestFile;
}
// The following is to avoid a handle leak
if(pi.hProcess)
{
CloseHandle(pi.hProcess);
pi.hProcess = NULL;
}
// The following is to avoid a handle leak
if(pi.hThread)
{
CloseHandle(pi.hThread);
pi.hThread = NULL;
}
return bCreateProcess ? 0 : 1;
}
reporting\crashsender\ErrorReportSender.h:
1. Added new method declaration:
// Dumps registry key to a REG file.
int DumpRegKeyToRegFile(CString sRegKey, CString sDestFile, CString& sErrorMsg);
just before the existing lines:
// Used internally for dumping a registry key.
int DumpRegKey(HKEY hKeyParent, CString sSubKey, TiXmlElement* elem);
I hope you consider this a useful addition to CrashRpt and can add integrate the changes above which I donate to the public domain into the next public release of CrashRpt. I can provide raw modified files if this is easier for your integration of the changes.
Regards,
PJ Naughter [Visual C++ MVP]
Naughter Software Ltd
Mail: pjna at naughter.com
Web: www.naughter.com
Hotmail/MSN Messenger: pjnaughter at hotmail.com