Signal and Exception Handlers in DLLs

Handling signals and operating system exceptions in DLLs is no different than handling signals in executable files, provided that all your DLLs and the executable files that use them are created using IBM C and C++ Compilers, and only one IBM C and C++ Compilers library environment exists for your executable module and all DLLs.

Signal Handlers in DLLs
If your signal handler resides in a DLL, ensure that you change the signal handler when you unload the DLL. You see no warnings or error messages if you unload the DLL without changing the handler, but your program will probably terminate the next time the handler is called. If another DLL has been loaded in the same address range, your program may continue but with undefined results.

Exception Handlers in DLLs
Using exception handlers is especially advantageous in system DLLs, because you can run process termination routines even if your DLL has global initialization and termination.

When a process terminates, functions are called in the following order:

  1. Functions registered with the atexit or _onexit functions
  2. Exception handlers for termination exceptions
  3. Functions registered with the OS/2 API DosExitList
  4. DLL termination routines.

Handlers in Multiple Library Environments
When you have multiple library environments, you must treat signal and exception handlers in a slightly different manner. If you have registered signal handlers in a DLL's run-time environment, you need to register an exception handler for every entry point in the DLL, in order to ensure that exceptions (and thus, signals) are always handled by the exception handler (or signal handler) for the environment where the exception occurred.

For example, if you have an executable module and a DLL, each with its own library environment, the _Exception handler is automatically registered for the executable module when it starts. When the executable module calls a function in the DLL, the thread of execution passes to the DLL. If an exception then occurs in the code in the DLL, it is actually handled by the exception handler in the executable module's library environment. Any signal handling set up in the DLL is ignored.

Include #pragma handler statements in your DLL for every function in the DLL that can be called from another module. This directive ensures the exception handler for the DLL's library environment is correctly registered when the function is called, and deregistered when the function returns to the calling module. Also, include a #pragma handler statement in every function in your executable module that can be called from a DLL.



Signals and Exceptions
Signal and Exception Handling
Run-Time Library Environments
Dynamic Link Libraries