Fixed typo and removed unconditional GetModuleHandle chain by
inspecting used import-libraries.


git-svn-id: svn+ssh://svn.code.sf.net/p/mingw-w64/code/trunk@4309 4407c894-4637-0410-b4f5-ada5f102cad1
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 3917060..51954a8 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -420,21 +420,42 @@
 #endif
 }
 
+extern const char *
+__mingw_enum_import_library_names (int i);
+
 static void __cdecl 
 __mingw_prepare_except_for_msvcr80_and_higher (void)
 {
   _invalid_parameter_handler (*fIPH)(_invalid_parameter_handler) = NULL;
-  HMODULE hmsv = GetModuleHandleA ("msvcr80.dll");
-  if(!hmsv)
-    hmsv = GetModuleHandleA ("msvcr70.dll");
-  if (!hmsv)
-    hmsv = GetModuleHandleA ("msvcrt.dll");
-  if (!hmsv)
-    hmsv = LoadLibraryA ("msvcrt.dll");
-  if (!hmsv)
+  int i = 0;
+  const char *dllname;
+  HMODULE hmsv = NULL;
+
+  while ((dllname = __mingw_enum_import_library_names (i)) != NULL)
+    {
+      /* We don't use here strnicmp to avoid its dependency.
+	 See if DLL-name begins with "msvcr" and is followed by
+	 a "t" or a digit.  */
+      if ((dllname[0] != 'm' && dllname[0] != 'M')
+	  || (dllname[1] != 's' && dllname[1] != 'S')
+	  || (dllname[2] != 'v' && dllname[2] != 'V')
+	  || (dllname[3] != 'c' && dllname[3] != 'C')
+	  || (dllname[4] != 'r' && dllname[4] != 'R')
+	  || (dllname[5] != 't' && dllname[5] != 'T'
+	      && dllname[5] < '0' && dllname[5] > '9'))
+	{
+	  ++i;
+	  continue;
+	}
+      hmsv = GetModuleHandleA (dllname);
+      fIPH = (_invalid_parameter_handler (*)(_invalid_parameter_handler))
+	GetProcAddress (hmsv, "_set_invalid_parameter_handler");
+      if (fIPH)
+	break;
+      ++i;
+    }
+  if (!fIPH)
     return;
-  fIPH = (_invalid_parameter_handler (*)(_invalid_parameter_handler))
-    GetProcAddress (hmsv, "_set_invalid_parameter_handler");
   if (fIPH)
     (*fIPH)(__mingw_invalidParameterHandler);
 }
diff --git a/mingw-w64-crt/crt/pesect.c b/mingw-w64-crt/crt/pesect.c
index 22ed6e4..481b548 100644
--- a/mingw-w64-crt/crt/pesect.c
+++ b/mingw-w64-crt/crt/pesect.c
@@ -186,6 +186,9 @@
 }
 
 const char *
+__mingw_enum_import_library_names (int);
+
+const char *
 __mingw_enum_import_library_names (int i)
 {
   PBYTE pImageBase;
@@ -218,7 +221,7 @@
         break;
 
       if (i <= 0)
-      	return (char *) (pImageBase + importDesc->Name));
+      	return (char *) (pImageBase + importDesc->Name);
       --i;
       importDesc++;
     }