I have created a DLL with Visual C++ 6. It is a standard Win32 DLL, not using
MFC.
The exported function of my DLL are "extern C" functions, to be used from
pure C (not C++) code.
For this reason, I don't want exceptions to traverse the boundary of my DLL.
So, I have included the following code in all my exported functions :
extern "C" void exportedFunction()
{
try
{
// The code body ...
}
catch ( MyException& e )
{
// Here, I handle my own exceptions.
}
catch ( ... )
{
// Here, I handle unknown exception.
}
}

The problem is that, when the catch (...) is reached, I have no information
about the problem that occured. The only treatment I can do is logging a
general message like "Unexpected exception thrown".

My question is :
How can I obtain information about the exception in order to create a
precise message ?

In my DLL I use Win32 functions like EnterCriticalSection for example, and I
have read in the documentation that this function can raise exception ("This
function can raise EXCEPTION_POSSIBLE_DEADLOCK if the critical section is
corrupt").

Should I reread my 200000 lines source code to see all the Win32 functions I
am using, then read the documentation of each function to know which
exception they can raise, and finally put a dedicated catch block for each
exception ???

Re: How to get information about an unknown exception by William

William
Tue Jun 07 09:34:12 CDT 2005

"Hervé" <Hervé@discussions.microsoft.com> wrote in message
news:A492DCA4-3E4F-4C04-ADEC-2F7849BEE3E9@microsoft.com...
>I have created a DLL with Visual C++ 6. It is a standard Win32 DLL, not
>using
> MFC.

OK.

> The exported function of my DLL are "extern C" functions, to be used from
> pure C (not C++) code.

OK.


> For this reason, I don't want exceptions to traverse the boundary of my
> DLL.
> So, I have included the following code in all my exported functions :

OK.


> My question is :
> How can I obtain information about the exception in order to create a
> precise message ?

First, it is generally considered a defect in the VC+6.0 compiler that it
catches the operating system's structured exceptions inside a C++ catch
handler that uses the ellipsis notation (...) to catch everything. Second,
by the time such a severe error occurs enough damage may have been done to
your application's state that it would be better to crash and burn than to
continue.

That said, in _development_, handling SEs as you suggest is often the first
step in fixing them. The class I posted here:

http://groups-beta.google.com/group/microsoft.public.vc.language/browse_frm/thread/25922abebb63fa1b/d8aa89b74adeeeab?q=structured+exception+group:microsoft.*+author:depalo&rnum=1&hl=en#d8aa89b74adeeeab

can catch SEs and report on their location.

Regards,
Will



Re: How to get information about an unknown exception by Herv

Herv
Tue Jun 07 10:16:02 CDT 2005

I have read the code sample and I think it will be very helpfull in my
context. I definitively think that my catch (...) was not a good practice,
not suitable for Structured Exceptions.

Since I don't have a "main" method, my intention is to put the
"Win32Exception::enable()" and "Win32Exception::disable()" calls in a "dll
main" function declared as "/ENTRY" in the linker (respectively in case of
DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH). I hope this will have no weird
effect on the Win32Exception behavior ...

Thank you.


Re: How to get information about an unknown exception by Herv

Herv
Tue Jun 07 10:49:01 CDT 2005

Ouups ...

Reading more carefully the attached piece of code, I understand that the
enablement of the conversion from Win32 Structured Exception to C++ exception
is done on a per thread basis.

I think I don't need such a sophisticated system. My idea so, is to remove
any TLS (Thread Local Storage) consideration in the piece of code you sent in
order to have a per process enablement.

What is not clear to me at this point is :
Will the "exception translation" system be enabled only on my own code (my
DLL), or will it be active on any other DLL/EXE that is part of the
application ?

If the answer is the second, I think it is a problem since other DLL do not
know our Win32Exception class.

Re: How to get information about an unknown exception by Herv

Herv
Tue Jun 07 11:39:42 CDT 2005

Finally, I think I need to enable/disable exception conversion on entry/exit
of each function exported by my DLL. Like this :

extern "C" void exportedFunction()
{
Win32Exception::enable();
try
{
// ...
}
catch ( MyException& me )
{
// Here I handle my own exceptions.
}
catch ( Win32Exception& we )
{
// Here I handle structured exceptions converted into Win32Exception.
}
catch ( ... )
{
// Here I handle unknow exception (should never occur).
}

Win32Exception::disable();
}

I'm right this time ?

Re: How to get information about an unknown exception by William

William
Tue Jun 07 12:51:30 CDT 2005

"Hervé" <Herv@discussions.microsoft.com> wrote in message
news:D9E467CF-7B3F-4A2C-9B80-41E24E489C23@microsoft.com...
> Since I don't have a "main" method, my intention is to put the
> "Win32Exception::enable()" and "Win32Exception::disable()" calls in a "dll
> main" function declared as "/ENTRY" in the linker (respectively in case of
> DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH). I hope this will have no weird
> effect on the Win32Exception behavior ...

In the beginning, the exception mechanism was stack-frame based. Information
about the handlers for the frame-based exceptions is stored with a thread.
That's the kind I was describing.

Much later support was added for "vectored" exception handlers. You can read
about that here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/vectored_exception_handling.asp?frame=true

If your plan is to do something in DllMain(), my guess is that that only
makes sense for vectored handlers but to be honest it is not something I
have thought about.

Regards,
Will



Re: How to get information about an unknown exception by William

William
Tue Jun 07 12:55:27 CDT 2005

"Hervé" <Herv@discussions.microsoft.com> wrote in message
news:86CDBF1A-2FE0-4908-9887-57278B9D3893@microsoft.com...
> Finally, I think I need to enable/disable exception conversion on
> entry/exit
> of each function exported by my DLL. Like this :
>
> extern "C" void exportedFunction()
> {
> Win32Exception::enable();
> try
> {
> // ...
> }
> catch ( MyException& me )
> {
> // Here I handle my own exceptions.
> }
> catch ( Win32Exception& we )
> {
> // Here I handle structured exceptions converted into
> Win32Exception.
> }
> catch ( ... )
> {
> // Here I handle unknow exception (should never occur).
> }
>
> Win32Exception::disable();
> }
>
> I'm right this time ?

Yes, I think you have it now.

Note too that you should build with the /Eha option. This tells the C++
compiler that exceptions may emanate from sources about which it has no
knowledge. If you fail to do that it is possible that the compiler will
optimize away the exception handler in release builds.

Regards,
Will



Re: How to get information about an unknown exception by Herv

Herv
Wed Jun 08 03:45:02 CDT 2005

This seems clear to me now.
I thank you, William, for the accurate information and documentation
pointers you sent to me.
I'm ready to go with the non-vectored mechanism, with a handler attached to
a thread stack frame.