Make LoadLibrary() failures silent on Win2k
For a custom installer application, I have been working on a crude and efficient way to determine whether the particular version of the C runtime (CRT) we need is installed or not. The technique I use is simply to check if a dummy DLL linked to the CRT libraries would load up properly.
This works gloriously well, except for one simple detail. When the CRT is not installed, on Windows 2000 only, the LoadLibrary error would also cause a MessageBox to be displayed. This stops the flow of the installer and of course, is not very elegant…
But there is a simple trick to get rid of the MessageBox, using the very obscure SetErrorMode system call.
::SetErrorMode(SEM_FAILCRITICALERRORS);
HMODULE hDll = ::LoadLibrary("CrtCheck.Dll"); // Won't bark
if (hDll) ; // CRT is installed
else ; // CRT is NOT installed
Do you guys know a better (yet simple) way to determine if the CRT is installed?
Update: this is not a Windows 2000 specific issue (thanks Ferruccio). It’s also true for older versions of Windows. As far as I know WinXP, Vista and 7 do NOT display a MessageBox under the same circumstances.

This isn’t just a Win2k issue. This has been the case for all versions of Windows going back all the way to 16-bit Windows. The only problem with this approach is that loading the DLL in this manner will cause the code in its DllMain() function to be executed, which is probably harmless. You may want to use the LoadLibraryEx() function which lets you load the library as data, avoiding running DllMain().
Also SetErrorMode() returns its previous setting. It’s good practice to set it back after the LoadLibrary().
@Ferruccio
Are you thinking of using LoadLibraryEx() with LOAD_LIBRARY_AS_DATAFILE? That approach does not work. The LoadLibraryEx would always succeed regardless of whether the CRT is installed or not. Also, I suspect running DllMain is useful, there might be extra checks done by the CRT. If DllMain returns FALSE, the LoadLibrary call fails.