Dumping exception information in Windows applications
Had to make a Windows application dump exception info into a file for debugging purposes. Generally MS provies a prety neat function for it – GetExceptionInformation. One tricky thing was getting the module file name of the process that generated the exception – I had to do VirtualQuery on the exception address, then use the mbi.AllocationBase as a module handle (didn’t figure that out myself, googled for it of course).
Anyway, since I lost more then 30 minutes for it I decided to share it. Use this source and the exception handle your main function like this:
int _tmain(int argc, _TCHAR* argv[])
{
__try
{
}
__except( GetExceptionInfo( GetExceptionInformation(), “error.log” ), EXCEPTION_EXECUTE_HANDLER )
{
}
}
This way, when an exception occurs a file error.log will be generated that looks like this:
Exception Code: C0000005, Address: 00401646
Module: C:\DUMP\DEBUG\DUMP.EXE
Registers:
EAX:00000000, EBX:0063F6B4, ECX:0063F644, EDX:00410078, ESI:000086D8, EDI:0063F668
SS:00000167, ESP:0063F62C, EBP:0063F654, CS:0000015F, EIP:00401646, Flags:00010246
DS:00000167, ES:00000167, FS:00003CD7, GS:00000000
Stack:
0063F668 000086D8 0063F6B4 0063FCF0 0063F62C 0063F45C 0063F69C 00401B48
004051C8 00000000 0063F660 5F43507B 0063FCF0 0063F680 BFF7363B 00000250