Thread: Exception idea in C compatible library. Comments.

  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

    Exception idea in C compatible library. Comments.

    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);
    
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Don't know if you've looked into this already but structured exception handling may be of some interest to you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Thanks a lot for the reply. The problem with seh is that it is not widely supported by C compilers(as far as I know). I've implemented my idea and I think it works quite well. It can be toggled at runtime and the exception code can be entirely excluded at compile time with a define.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 01:28 PM
  4. Inheritance and Arrays
    By codegirl in forum C++ Programming
    Replies: 18
    Last Post: 06-06-2003, 12:46 PM
  5. Library idea... is it possible to do?
    By Trauts in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2003, 08:08 PM