tags:

views:

28

answers:

1

Is it possible to silently catch errors popup like "the procedure entry point xxx could not be located int the dynamic link library xxx" when calling LoadLibrary() ?

+3  A: 

You can suppress the error popups by calling SetErrorMode():

// GetErrorMode() only exists on Vista and higher,
// call SetErrorMode() twice to achieve the same effect.
UINT oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
SetErrorMode(oldErrorMode | SEM_FAILCRITICALERRORS);

HMODULE library = LoadLibrary("YourLibrary.dll");

// Restore previous error mode.
SetErrorMode(oldErrorMode);

The call to LoadLibrary() will still fail, though.

Frédéric Hamidi
Thanks a lot for the trick !
Soubok
perhaps the fist call could be oldErrorMode = SetErrorMode(0) ?
Soubok
That's debatable :) Another thread could have called `SetErrorMode(SEM_FAILCRITICALERRORS)`, so it would be problematic to reset the error mode, even for a short time. Remember, `SetErrorMode()` is process-scoped. [SetThreadErrorMode()](http://msdn.microsoft.com/en-us/library/dd553630%28v=VS.85%29.aspx) is thread-scoped, but it's only supported on Windows 7 / Server 2008 R2 and higher.
Frédéric Hamidi