Everything works fine, but _OnExit is never called. I tried with SetConsoleCtrlHandle, in Kernel32.dll, and I'm able to catch and handle the events, but the close event always close the process and _OnExit is never called. Any ideas for a possible solution? Is not a big problem, and shutting down the library is not fully required, but I don't like to leave libraries open.