Dave's Brain

Browse - programming tips - win32 catch exceptions

Date: 2008jul31
Keywords: CxxThrowException
Platform: win32
Language: C/C++

Q.  How do I do catch exceptions that are being thrown by a DLL
I don't have the source for?

A.  Use SetUnhandledExceptionFilter() like this...

static void RestartClientProgram()
{
	char	szClient[MAX_PATH+1];

	GetModuleFileName(NULL, szClient, sizeof(szClient));
	printf("Restarting %s\n", szClient);
	WinExec(szClient, SW_NORMAL);

	_exit(1); // Don't use exit() since may be trapped
}

static LONG WINAPI MyUnHandleExceptionFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
	int		code;

	code = lpExceptionInfo->ExceptionRecord->ExceptionCode;
	printf("Exception code=0x%x\n", code);

	RestartClientProgram();

	return EXCEPTION_EXECUTE_HANDLER; // Terminate probably
}

void ExampleUse()
{
	SetUnhandledExceptionFilter(MyUnHandleExceptionFilter);	
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: