Jun 132017

Note to self: Do not call SafeLoadLibrary simply as a replacement for LoadLibrary. It has a second parameter witch is used to call SetErrorMode. That parameter is optional and defaults to SEM_NOOPENFILEERRORBOX which is NOT always what you want. E.g. if the DLL you are loading depends on other DLLs which cannot be loaded, you will get a system error dialog like this one:

(My question on StackOverflow)

This might not be what you want if you are loading a DLL dynamically and want to show your own error message if it fails.

So you might want to pass a second parameter, e.g.

  Res := SafeLoadLibrary('dllname.dll', SEM_FAILCRITICALERRORS);

There might, however be a reason to call SafeLoadLibrary instead of LoadLibrary. It safes and restores the FPU Control Word. Some DLLs change it in DllMain which might cause your program to misbehave if it isn’t restored.

I remember spending days tracking down such a problem in the late 1990ies so I made it a habit calling SafeLoadLibrary rather than LoadLibrary.

On the other hand that could also happen in any other call into the DLL. So maybe it is time to revise that habit.

Sorry, the comment form is closed at this time.

%d bloggers like this: