Thursday, November 17, 2005

Using DbgHelp.dll MiniDumpWriteDump function with Borland C++ Builder 5

I have written a handy class that allows to use the DbgHelp.dll MiniDumpWriteDump function in Borland C++ Builder 5 environment. Feel free to use it:

//DLL wrapper class
#define CALLING_CONVENTION __stdcall

class CDbgHelpDLL
{
public:
CDbgHelpDLL()
{
m_hDLL = ::LoadLibraryA("dbghelp.dll");
if(NULL != m_hDLL)
{
//CodeSite API
DLLMiniDumpWriteDump = (BOOL (CALLING_CONVENTION *)
(HANDLE,
DWORD,
HANDLE,
MINIDUMP_TYPE,
CONST PMINIDUMP_EXCEPTION_INFORMATION,
CONST PMINIDUMP_USER_STREAM_INFORMATION,
CONST PMINIDUMP_CALLBACK_INFORMATION))
::GetProcAddress(m_hDLL, "MiniDumpWriteDump");
}
else
{
DLLMiniDumpWriteDump = NULL;
}
}
~CDbgHelpDLL()
{
if(NULL != m_hDLL)
{
::FreeLibrary(m_hDLL);
m_hDLL = NULL;
}
}

BOOL MiniDumpWriteDump(
HANDLE hProcess,
DWORD ProcessId,
HANDLE hFile,
MINIDUMP_TYPE DumpType,
PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
PMINIDUMP_CALLBACK_INFORMATION CallbackParam
)
{
if (DLLMiniDumpWriteDump){
return DLLMiniDumpWriteDump( hProcess,
ProcessId,
hFile,
DumpType,
ExceptionParam,
UserStreamParam,
CallbackParam);
}
else
{
return FALSE;
}
}



private:
HINSTANCE m_hDLL;
//DLL functions
BOOL (CALLING_CONVENTION *DLLMiniDumpWriteDump)( HANDLE hProcess,
DWORD ProcessId,
HANDLE hFile,
MINIDUMP_TYPE DumpType,
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
};

In order to compile with dbghelp.h from ms, you need to "patch" it:

before // for those without specstrings.h add:

//CBuilder
#ifdef __BORLANDC__
using namespace System;
#endif

Change this:
#ifdef __BORLANDC__
enum hdEnums {
#else
typedef enum {
#endif
hdBase = 0, // root directory for dbghelp
hdSym, // where symbols are stored
hdSrc, // where source is stored
hdMax // end marker
};

and this:
#ifdef __BORLANDC__
enum sfEnums {
#else
typedef enum {
#endif
sfImage = 0,
sfDbg,
sfPdb,
sfMpd,
sfMax
};

and here we go ...
Happy crash dumping with borland ;-)

No comments: