Hello, I'm building a library (in C) amd am wondering about handling exception information. I was thinking about something like this. What do you think?
Code:
// Functions look like this...
void ALibraryFunction() {
LIB_ENTER(L"ALibraryFunction");
// function work
return LIB_EXIT_EX(hr, "what ever");
}
// exception code...
typedef struct tagLIB_EXCEPTION {
UINT nStackCount;
LPWSTR szInitialFunction;
LPWSTR szInnerFunction;
LPWSTR szErrorInfo;
// more exception info here.
HRESULT hr;
} LIB_EXCEPTION, * LPLIB_EXCEPTION;
LIB_EXCEPTION g_LibException;
// =============================================
VOID LIB_ENTER(LPCWSTR szFunctionName) {
LPLIB_EXCEPTION pLibException = &g_LibException;
if (pLibException->nStackCount++ == 0) {
ZeroMemory(pLibException, sizeof(LIB_EXCEPTION));
pLibException->szInitialFunction = szFunctionName;
}
pLibException->szInnerFunction = szFunctionName;
}
//=============================================HRESULT LIB_EXITEX(HRESULT hr, LPCWSTR szErrorInfo) {
if (szErrorInfo) wcsncpy(pLibException->szErrorInfo, szMember, 63);
pLibException->hr = hr;
if (--pLibException->nStackCount == 0 && FAILED(hr)) LibShowException();
return hr;
}
// ============================================
HRESULT LibFormatException(LPWSTR szBuffer, UINT cchBufferSize) {
// format exception structure into nice string..
}
// =============================================
HRESULT LibShowException(void) {
WCHAR szMessage[512];
LibFormatException(szMessage, 512);
MessageBoxW(NULL, szMessage, NULL, MB_ICONWARNING);
}