Adding a function to the safeseh list

A long time ago, Matt Pietrek wrote a great article on compiler level structured exception handling. Since Matt’s article, the windows and VC linker team came up with a technology called safeseh. Chris Brumme talked about it here. Basically, dlls/exes get a new section that contains a list of all the possible exception handlers in that dll. Usually, the only interesting thing in this list is _except_handler3, which is a CRT function that is called by the OS.

The only problem with all this new stuff, is that there don’t seem to be many examples of how to use it, so it took me all morning to get it to work.

First you write your handler in C/C++:

extern "C" EXCEPTION_DISPOSITION __cdecl

MyExceptionFilter(

      EXCEPTION_RECORD *pExceptionRecord,

      EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,

      CONTEXT *pContext,

      void *DispatcherContext

      )

{

      ...

      return ExceptionContinueSearch;

}

Now you add a .asm file:

MyExceptionFilter proto

.safeseh MyExceptionFilter

end

Lastly, you compile your new .asm file (ml.exe /safeseh ...) and link your objects together (link.exe /safeseh ...).