]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/nt-base.c
(no commit message)
[imagemagick] / MagickCore / nt-base.c
index 82cad0a8a81cb73a6d58cf993a8b236c1dff1330..ebdb25460922f7dae0cbbefdb64b449504edf709 100644 (file)
 %                   Windows NT Utility Methods for MagickCore                 %
 %                                                                             %
 %                               Software Design                               %
-%                                 John Cristy                                 %
+%                                    Cristy                                   %
 %                                December 1996                                %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "MagickCore/log.h"
 #include "MagickCore/magick.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/nt-base.h"
+#include "MagickCore/nt-base-private.h"
 #include "MagickCore/resource_.h"
 #include "MagickCore/resource-private.h"
 #include "MagickCore/timer.h"
 #include "MagickCore/string_.h"
+#include "MagickCore/string-private.h"
 #include "MagickCore/utility.h"
+#include "MagickCore/utility-private.h"
 #include "MagickCore/version.h"
 #if defined(MAGICKCORE_LTDL_DELEGATE)
 #  include "ltdl.h"
 #endif
-#include "MagickCore/nt-base.h"
 #if defined(MAGICKCORE_CIPHER_SUPPORT)
 #include <ntsecapi.h>
 #include <wincrypt.h>
@@ -82,6 +85,20 @@ static GhostInfo
 static void
   *ghost_handle = (void *) NULL;
 \f
+struct
+{
+  const HKEY
+    hkey;
+
+  const char
+    *name;
+}
+const registry_roots[2] =
+{
+  { HKEY_CURRENT_USER,  "HKEY_CURRENT_USER"  },
+  { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
+};
+
 /*
   External declarations.
 */
@@ -89,6 +106,30 @@ static void
 extern "C" BOOL WINAPI
   DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
 #endif
+
+static inline char *create_utf8_string(const wchar_t *wideChar)
+{
+  char
+    *utf8;
+
+  int
+    count;
+
+  count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,NULL,0,NULL,NULL);
+  if (count < 0)
+    return((char *) NULL);
+  utf8=(char *) AcquireQuantumMemory(count+1,sizeof(*utf8));
+  if (utf8 == (char *) NULL)
+    return((char *) NULL);
+  count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,utf8,count,NULL,NULL);
+  if (count == 0)
+    {
+      utf8=DestroyString(utf8);
+      return((char *) NULL);
+    }
+  utf8[count]=0;
+  return(utf8);
+}
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -146,16 +187,20 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
       ssize_t
         count;
 
-      module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
-        sizeof(*module_path));
-      if (module_path == (char *) NULL)
+      wchar_t
+        *wide_path;
+
+      wide_path=(wchar_t *) AcquireQuantumMemory(MaxTextExtent,
+        sizeof(*wide_path));
+      if (wide_path == (wchar_t *) NULL)
         return(FALSE);
-      count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
+      count=(ssize_t) GetModuleFileNameW(handle,wide_path,MaxTextExtent);
       if (count != 0)
         {
           char
             *path;
 
+          module_path=create_utf8_string(wide_path);
           for ( ; count > 0; count--)
             if (module_path[count] == '\\')
               {
@@ -167,6 +212,7 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
           if (path == (char *) NULL)
             {
               module_path=DestroyString(module_path);
+              wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
               return(FALSE);
             }
           count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
@@ -183,6 +229,7 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
                     {
                       path=DestroyString(path);
                       module_path=DestroyString(module_path);
+                      wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
                       return(FALSE);
                     }
                   (void) FormatLocaleString(variable,16*MaxTextExtent,
@@ -192,8 +239,9 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
                 }
             }
           path=DestroyString(path);
+          module_path=DestroyString(module_path);
         }
-      module_path=DestroyString(module_path);
+      wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
       break;
     }
     case DLL_PROCESS_DETACH:
@@ -231,15 +279,17 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
 %      process.
 %
 */
-MagickExport int Exit(int status)
+MagickPrivate int Exit(int status)
 {
   if (IsWindows95())
-    TerminateProcess(GetCurrentProcess(),(unsigned int) status);
+    {
+      TerminateProcess(GetCurrentProcess(),(unsigned int) status);
+      return(0);
+    }
   exit(status);
-  return(0);
 }
 \f
-#if !defined(__MINGW32__)
+#if !defined(__MINGW32__) && !defined(__MINGW64__)
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
@@ -264,7 +314,7 @@ MagickExport int Exit(int status)
 %    o time_zone: the time zone.
 %
 */
-MagickExport int gettimeofday (struct timeval *time_value,
+MagickPrivate int gettimeofday (struct timeval *time_value,
   struct timezone *time_zone)
 {
 #define EpochFiletime  MagickLLConstant(116444736000000000)
@@ -324,7 +374,7 @@ MagickExport int gettimeofday (struct timeval *time_value,
 %      int IsWindows95()
 %
 */
-MagickExport int IsWindows95()
+MagickPrivate int IsWindows95()
 {
   OSVERSIONINFO
     version_info;
@@ -361,7 +411,7 @@ MagickExport int IsWindows95()
 %    o argv:  the  wide-character command line arguments.
 %
 */
-MagickExport char **NTArgvToUTF8(const int argc,wchar_t **argv)
+MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
 {
   char
     **utf8;
@@ -374,23 +424,14 @@ MagickExport char **NTArgvToUTF8(const int argc,wchar_t **argv)
     ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
   for (i=0; i < (ssize_t) argc; i++)
   {
-    ssize_t
-      count;
-
-    count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
-    if (count < 0)
-      count=0;
-    utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
+    utf8[i]=create_utf8_string(argv[i]);
     if (utf8[i] == (char *) NULL)
       {
         for (i--; i >= 0; i--)
           utf8[i]=DestroyString(utf8[i]);
-        utf8=(char **) RelinquishMagickMemory(utf8);
         ThrowFatalException(ResourceLimitFatalError,
           "UnableToConvertStringToARGV");
       }
-    count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
-    utf8[i][count]=0;
   }
   return(utf8);
 }
@@ -418,7 +459,7 @@ MagickExport char **NTArgvToUTF8(const int argc,wchar_t **argv)
 %    o entry: Specifies a pointer to a DIR structure.
 %
 */
-MagickExport int NTCloseDirectory(DIR *entry)
+MagickPrivate int NTCloseDirectory(DIR *entry)
 {
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
   assert(entry != (DIR *) NULL);
@@ -449,7 +490,7 @@ MagickExport int NTCloseDirectory(DIR *entry)
 %    o handle: Specifies a handle to a previously loaded dynamic module.
 %
 */
-MagickExport int NTCloseLibrary(void *handle)
+MagickPrivate int NTCloseLibrary(void *handle)
 {
   if (IsWindows95())
     return(FreeLibrary((HINSTANCE) handle));
@@ -483,7 +524,7 @@ static BOOL ControlHandler(DWORD type)
   return(FALSE);
 }
 
-MagickExport int NTControlHandler(void)
+MagickPrivate int NTControlHandler(void)
 {
   return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
 }
@@ -507,7 +548,7 @@ MagickExport int NTControlHandler(void)
 %      double NTElapsedTime(void)
 %
 */
-MagickExport double NTElapsedTime(void)
+MagickPrivate double NTElapsedTime(void)
 {
   union
   {
@@ -554,7 +595,7 @@ MagickExport double NTElapsedTime(void)
 %    o description: Specifies any description to the reason.
 %
 */
-MagickExport void NTErrorHandler(const ExceptionType severity,
+MagickPrivate void NTErrorHandler(const ExceptionType severity,
   const char *reason,const char *description)
 {
   char
@@ -607,7 +648,7 @@ MagickExport void NTErrorHandler(const ExceptionType severity,
 %      int NTExitLibrary(void)
 %
 */
-MagickExport int NTExitLibrary(void)
+MagickPrivate int NTExitLibrary(void)
 {
   return(0);
 }
@@ -637,7 +678,7 @@ MagickExport int NTExitLibrary(void)
 %    random: the random data is returned here.
 %
 */
-MagickExport MagickBooleanType NTGatherRandomData(const size_t length,
+MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
   unsigned char *random)
 {
 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
@@ -695,10 +736,15 @@ MagickExport MagickBooleanType NTGatherRandomData(const size_t length,
 %    o extent: the maximum extent of the path.
 %
 */
-MagickExport MagickBooleanType NTGetExecutionPath(char *path,
+MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
   const size_t extent)
 {
-  GetModuleFileName(0,path,(DWORD) extent);
+  wchar_t
+    wide_path[MaxTextExtent];
+
+  (void) GetModuleFileNameW(0,wide_path,(DWORD) extent);
+  (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,
+    NULL);
   return(MagickTrue);
 }
 \f
@@ -764,7 +810,7 @@ char *NTGetLastError(void)
 %      const char *NTGetLibraryError(void)
 %
 */
-MagickExport const char *NTGetLibraryError(void)
+MagickPrivate const char *NTGetLibraryError(void)
 {
   static char
     last_error[MaxTextExtent];
@@ -840,7 +886,7 @@ void *NTGetLibrarySymbol(void *handle,const char *name)
 %    path: the module path is returned here.
 %
 */
-MagickExport MagickBooleanType NTGetModulePath(const char *module,char *path)
+MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
 {
   char
     module_path[MaxTextExtent];
@@ -887,7 +933,7 @@ MagickExport MagickBooleanType NTGetModulePath(const char *module,char *path)
 %
 */
 
-static int NTGetRegistryValue(HKEY root,const char *key,const char *name,
+static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
   char *value,int *length)
 {
   BYTE
@@ -907,7 +953,7 @@ static int NTGetRegistryValue(HKEY root,const char *key,const char *name,
   /*
     Get a registry value: key = root\\key, named value = name.
   */
-  if (RegOpenKeyExA(root,key,0,KEY_READ,&hkey) != ERROR_SUCCESS)
+  if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
     return(1);  /* no match */
   p=(BYTE *) value;
   type=REG_SZ;
@@ -929,8 +975,8 @@ static int NTGetRegistryValue(HKEY root,const char *key,const char *name,
   return(1);  /* not found */
 }
 
-static int NTLocateGhostscript(const char **product_family,int *major_version,
-  int *minor_version)
+static int NTLocateGhostscript(DWORD flags,int *root_index,
+  const char **product_family,int *major_version,int *minor_version)
 {
   int
     i;
@@ -950,7 +996,8 @@ static int NTLocateGhostscript(const char **product_family,int *major_version,
   /*
     Find the most recent version of Ghostscript.
   */
-  status=FALSE;
+  status=MagickFalse;
+  *root_index=0;
   *product_family=NULL;
   *major_version=5;
   *minor_version=49; /* min version of Ghostscript is 5.50 */
@@ -960,53 +1007,57 @@ static int NTLocateGhostscript(const char **product_family,int *major_version,
       key[MaxTextExtent];
 
     HKEY
-      hkey,
-      root;
+      hkey;
+
+    int
+      j;
 
     REGSAM
       mode;
 
     (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
-    root=HKEY_LOCAL_MACHINE;
-    mode=KEY_READ;
-#if defined(KEY_WOW64_32KEY)
-    mode|=KEY_WOW64_32KEY;
-#endif
-    if (RegOpenKeyExA(root,key,0,mode,&hkey) == ERROR_SUCCESS)
-      {
-        DWORD
-          extent;
-
-        int
-          j;
-
-        /*
-          Now enumerate the keys.
-        */
-        extent=sizeof(key)/sizeof(char);
-        for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
+    for (j=0; j < (ssize_t) (sizeof(registry_roots)/sizeof(registry_roots[0]));
+         j++)
+    {
+      mode=KEY_READ | flags;
+      if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
+            ERROR_SUCCESS)
         {
+          DWORD
+            extent;
+
           int
-            major,
-            minor;
-
-          major=0;
-          minor=0;
-          if (sscanf(key,"%d.%d",&major,&minor) != 2)
-            continue;
-          if ((major > *major_version) || ((major == *major_version) &&
-              (minor > *minor_version)))
-            {
-              *product_family=products[i];
-              *major_version=major;
-              *minor_version=minor;
-              status=TRUE;
-            }
+            k;
+
+          /*
+            Now enumerate the keys.
+          */
+          extent=sizeof(key)/sizeof(char);
+          for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
+          {
+            int
+              major,
+              minor;
+
+            major=0;
+            minor=0;
+            if (sscanf(key,"%d.%d",&major,&minor) != 2)
+              continue;
+            if ((major > *major_version) || ((major == *major_version) &&
+                (minor > *minor_version)))
+              {
+                *root_index=j;
+                *product_family=products[i];
+                *major_version=major;
+                *minor_version=minor;
+                status=MagickTrue;
+              }
+         }
+         (void) RegCloseKey(hkey);
        }
-       (void) RegCloseKey(hkey);
-     }
+    }
   }
-  if (status == FALSE)
+  if (status == MagickFalse)
     {
       *major_version=0;
       *minor_version=0;
@@ -1016,71 +1067,126 @@ static int NTLocateGhostscript(const char **product_family,int *major_version,
   return(status);
 }
 
-static int NTGhostscriptGetString(const char *name,char *value,
-  const size_t length)
+static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,
+  char *value,const size_t length)
 {
   char
-    key[MaxTextExtent];
+    buffer[MaxTextExtent],
+    *directory;
 
   int
-    i,
     extent;
 
   static const char
-    *product_family = (const char *) NULL;
+    *product_family=(const char *) NULL;
+
+  static BOOL
+    is_64_bit_version=FALSE;
 
   static int
+    flags=0,
     major_version=0,
-    minor_version=0;
-
-  struct
-  {
-    const HKEY
-      hkey;
-
-    const char
-      *name;
-  }
-  hkeys[2] =
-  {
-    { HKEY_CURRENT_USER,  "HKEY_CURRENT_USER"  },
-    { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
-  };
+    minor_version=0,
+    root_index=0;
 
   /*
     Get a string from the installed Ghostscript.
   */
   *value='\0';
+  directory=(char *) NULL;
+  if (LocaleCompare(name, "GS_DLL") == 0)
+    {
+      directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_PATH");
+      if (directory != (char *) NULL)
+        {
+          (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll32.dll",
+            directory,DirectorySeparator);
+          if (IsPathAccessible(buffer) != MagickFalse)
+            {
+              (void) CopyMagickString(value,buffer,length);
+              if (is_64_bit != NULL)
+                *is_64_bit=FALSE;
+              return(TRUE);
+            }
+          (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll64.dll",
+            directory,DirectorySeparator);
+          if (IsPathAccessible(buffer) != MagickFalse)
+            {
+              (void) CopyMagickString(value,buffer,length);
+              if (is_64_bit != NULL)
+                *is_64_bit=TRUE;
+              return(TRUE);
+            }
+          return(FALSE);
+        }
+    }
   if (product_family == NULL)
-    (void) NTLocateGhostscript(&product_family,&major_version,&minor_version);
+    {
+      flags=0;
+#if defined(KEY_WOW64_32KEY)
+#if defined(_WIN64)
+      flags=KEY_WOW64_64KEY;
+#else
+      flags=KEY_WOW64_32KEY;
+#endif
+      (void) NTLocateGhostscript(flags,&root_index,&product_family,
+        &major_version,&minor_version);
+      if (product_family == NULL)
+#if defined(_WIN64)
+        flags=KEY_WOW64_32KEY;
+      else
+        is_64_bit_version=TRUE;
+#else
+        flags=KEY_WOW64_64KEY;
+#endif
+#endif
+    }
+  if (product_family == NULL)
+    {
+      (void) NTLocateGhostscript(flags,&root_index,&product_family,
+      &major_version,&minor_version);
+#if !defined(_WIN64)
+      is_64_bit_version=TRUE;
+#endif
+    }
   if (product_family == NULL)
     return(FALSE);
-  (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
+  if (is_64_bit != NULL)
+    *is_64_bit=is_64_bit_version;
+  (void) FormatLocaleString(buffer,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
     product_family,major_version,minor_version);
-  for (i=0; i < (ssize_t) (sizeof(hkeys)/sizeof(hkeys[0])); i++)
-  {
-    extent=(int) length;
-    if (NTGetRegistryValue(hkeys[i].hkey,key,name,value,&extent) == 0)
-      {
-        (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
-          "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
-        return(TRUE);
-      }
-    (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
-      "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
-  }
+  extent=(int) length;
+  if (NTGetRegistryValue(registry_roots[root_index].hkey,buffer,flags,name,
+    value,&extent) == 0)
+    {
+      (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
+        "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
+        buffer,name,value);
+      return(TRUE);
+    }
   return(FALSE);
 }
 
-MagickExport int NTGhostscriptDLL(char *path,int length)
+MagickPrivate int NTGhostscriptDLL(char *path,int length)
 {
   static char
     dll[MaxTextExtent] = { "" };
 
+  static BOOL
+    is_64_bit_version;
+
   *path='\0';
   if ((*dll == '\0') &&
-      (NTGhostscriptGetString("GS_DLL",dll,sizeof(dll)) == FALSE))
+      (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
+    return(FALSE);
+
+#if defined(_WIN64)
+  if (!is_64_bit_version)
     return(FALSE);
+#else
+  if (is_64_bit_version)
+    return(FALSE);
+#endif
   (void) CopyMagickString(path,dll,length);
   return(TRUE);
 }
@@ -1106,7 +1212,7 @@ MagickExport int NTGhostscriptDLL(char *path,int length)
 %      const GhostInfo *NTGhostscriptDLLVectors(void)
 %
 */
-MagickExport const GhostInfo *NTGhostscriptDLLVectors(void)
+MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
 {
   if (NTGhostscriptLoadDLL() == FALSE)
     return((GhostInfo *) NULL);
@@ -1139,7 +1245,7 @@ MagickExport const GhostInfo *NTGhostscriptDLLVectors(void)
 %    o length: length of buffer.
 %
 */
-MagickExport int NTGhostscriptEXE(char *path,int length)
+MagickPrivate int NTGhostscriptEXE(char *path,int length)
 {
   register char
     *p;
@@ -1147,16 +1253,20 @@ MagickExport int NTGhostscriptEXE(char *path,int length)
   static char
     program[MaxTextExtent] = { "" };
 
+  static BOOL
+    is_64_bit_version = FALSE;
+
   (void) CopyMagickString(path,"gswin32c.exe",length);
   if ((*program == '\0') &&
-      (NTGhostscriptGetString("GS_DLL",program,sizeof(program)) == FALSE))
+      (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
     return(FALSE);
   p=strrchr(program,'\\');
   if (p != (char *) NULL)
     {
       p++;
       *p='\0';
-      (void) ConcatenateMagickString(program,"gswin32c.exe",sizeof(program));
+      (void) ConcatenateMagickString(program,is_64_bit_version ?
+        "gswin64c.exe" : "gswin32c.exe",sizeof(program));
     }
   (void) CopyMagickString(path,program,length);
   return(TRUE);
@@ -1187,10 +1297,11 @@ MagickExport int NTGhostscriptEXE(char *path,int length)
 %    o length: length of the path buffer.
 %
 */
-MagickExport int NTGhostscriptFonts(char *path,int length)
+MagickPrivate int NTGhostscriptFonts(char *path,int length)
 {
   char
     buffer[MaxTextExtent],
+    *directory,
     filename[MaxTextExtent];
 
   register char
@@ -1198,8 +1309,17 @@ MagickExport int NTGhostscriptFonts(char *path,int length)
     *q;
 
   *path='\0';
-  if (NTGhostscriptGetString("GS_LIB",buffer,MaxTextExtent) == FALSE)
-    return(FALSE);
+  directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_FONT_PATH");
+  if (directory != (char *) NULL)
+    {
+      (void) CopyMagickString(buffer,directory,MaxTextExtent);
+      directory=DestroyString(directory);
+    }
+  else
+    {
+      if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
+        return(FALSE);
+    }
   for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
   {
     (void) CopyMagickString(path,p+1,length+1);
@@ -1211,6 +1331,7 @@ MagickExport int NTGhostscriptFonts(char *path,int length)
     if (IsPathAccessible(filename) != MagickFalse)
       return(TRUE);
   }
+  *path='\0';
   return(FALSE);
 }
 \f
@@ -1233,7 +1354,7 @@ MagickExport int NTGhostscriptFonts(char *path,int length)
 %      int NTGhostscriptLoadDLL(void)
 %
 */
-MagickExport int NTGhostscriptLoadDLL(void)
+MagickPrivate int NTGhostscriptLoadDLL(void)
 {
   char
     path[MaxTextExtent];
@@ -1282,7 +1403,7 @@ MagickExport int NTGhostscriptLoadDLL(void)
 %      int NTGhostscriptUnLoadDLL(void)
 %
 */
-MagickExport int NTGhostscriptUnLoadDLL(void)
+MagickPrivate int NTGhostscriptUnLoadDLL(void)
 {
   int
     status;
@@ -1313,7 +1434,7 @@ MagickExport int NTGhostscriptUnLoadDLL(void)
 %      int NTInitializeLibrary(void)
 %
 */
-MagickExport int NTInitializeLibrary(void)
+MagickPrivate int NTInitializeLibrary(void)
 {
   return(0);
 }
@@ -1333,11 +1454,11 @@ MagickExport int NTInitializeLibrary(void)
 %
 %  The format of the NTMapMemory method is:
 %
-%    MagickExport void *NTMapMemory(char *address,size_t length,int protection,
+%    MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
 %      int access,int file,MagickOffsetType offset)
 %
 */
-MagickExport void *NTMapMemory(char *address,size_t length,int protection,
+MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
   int flags,int file,MagickOffsetType offset)
 {
   DWORD
@@ -1395,7 +1516,7 @@ MagickExport void *NTMapMemory(char *address,size_t length,int protection,
       CloseHandle(map_handle);
     }
   if (map == (void *) NULL)
-    return((void *) MAP_FAILED);
+    return((void *) ((char *) MAP_FAILED));
   return((void *) ((char *) map));
 }
 \f
@@ -1422,40 +1543,40 @@ MagickExport void *NTMapMemory(char *address,size_t length,int protection,
 %    o entry: Specifies a pointer to a DIR structure.
 %
 */
-MagickExport DIR *NTOpenDirectory(const char *path)
+MagickPrivate DIR *NTOpenDirectory(const char *path)
 {
-  char
-    file_specification[MaxTextExtent];
-
   DIR
     *entry;
 
   size_t
     length;
 
+  wchar_t
+    file_specification[MaxTextExtent];
+
   assert(path != (const char *) NULL);
-  length=CopyMagickString(file_specification,path,MaxTextExtent);
-  if (length >= (MaxTextExtent-1))
-    return((DIR *) NULL);
-  length=ConcatenateMagickString(file_specification,DirectorySeparator,
+  length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
     MaxTextExtent);
-  if (length >= (MaxTextExtent-1))
+  if (length == 0)
+    return((DIR *) NULL);
+  if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
+       MaxTextExtent-wcslen(file_specification)-1) == (wchar_t*)NULL)
     return((DIR *) NULL);
   entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
   if (entry != (DIR *) NULL)
     {
       entry->firsttime=TRUE;
-      entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
+      entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
     }
   if (entry->hSearch == INVALID_HANDLE_VALUE)
     {
-      length=ConcatenateMagickString(file_specification,"\\*.*",MaxTextExtent);
-      if (length >= (MaxTextExtent-1))
+      if(wcsncat(file_specification,L"*.*",
+        MaxTextExtent-wcslen(file_specification)-1) == (wchar_t*)NULL)
         {
           entry=(DIR *) RelinquishMagickMemory(entry);
           return((DIR *) NULL);
         }
-      entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
+      entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
       if (entry->hSearch == INVALID_HANDLE_VALUE)
         {
           entry=(DIR *) RelinquishMagickMemory(entry);
@@ -1490,7 +1611,7 @@ MagickExport DIR *NTOpenDirectory(const char *path)
 %
 */
 
-static const char *GetSearchPath( void )
+static inline const char *GetSearchPath(void)
 {
 #if defined(MAGICKCORE_LTDL_DELEGATE)
   return(lt_dlgetsearchpath());
@@ -1499,59 +1620,82 @@ static const char *GetSearchPath( void )
 #endif
 }
 
-MagickExport void *NTOpenLibrary(const char *filename)
+static UINT ChangeErrorMode(void)
 {
-#define MaxPathElements  31
+  typedef UINT
+    (CALLBACK *GETERRORMODE)(void);
 
-  char
-    buffer[MaxTextExtent];
+  GETERRORMODE
+    getErrorMode;
+
+  HMODULE
+    handle;
+
+  UINT
+    mode;
 
+  mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
+
+  handle=GetModuleHandle("kernel32.dll");
+  if (handle == (HMODULE) NULL)
+    return SetErrorMode(mode);
+
+  getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
+  if (getErrorMode != (GETERRORMODE) NULL)
+    mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
+
+  return SetErrorMode(mode);
+}
+
+static inline void *NTLoadLibrary(const char *filename)
+{
   int
-    index;
+    length;
+
+  wchar_t
+    path[MaxTextExtent];
+
+  length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MaxTextExtent);
+  if (length == 0)
+    return((void *) NULL);
+  return (void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
+}
+
+MagickPrivate void *NTOpenLibrary(const char *filename)
+{
+  char
+    path[MaxTextExtent];
 
   register const char
     *p,
     *q;
 
-  register int
-    i;
-
   UINT
     mode;
 
   void
     *handle;
 
-  mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
-  handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
-  if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
+  mode=ChangeErrorMode();
+  handle=NTLoadLibrary(filename);
+  if (handle == (void *) NULL)
     {
-      SetErrorMode(mode);
-      return(handle);
-    }
-  p=(char *) GetSearchPath();
-  index=0;
-  while (index < MaxPathElements)
-  {
-    q=strchr(p,DirectoryListSeparator);
-    if (q == (char *) NULL)
+      p=GetSearchPath();
+      while (p != (const char*) NULL)
       {
-        (void) CopyMagickString(buffer,p,MaxTextExtent);
-        (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
-        (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
-        handle=(void *) LoadLibraryEx(buffer,NULL,
-          LOAD_WITH_ALTERED_SEARCH_PATH);
-        break;
+        q=strchr(p,DirectoryListSeparator);
+        if (q != (const char*) NULL)
+          (void) CopyMagickString(path,p,q-p+1);
+        else
+          (void) CopyMagickString(path,p,MaxTextExtent);
+        (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
+        (void) ConcatenateMagickString(path,filename,MaxTextExtent);
+        handle=NTLoadLibrary(path);
+        if (handle != (void *) NULL || q == (const char*) NULL)
+          break;
+        p=q+1;
       }
-    i=q-p;
-    (void) CopyMagickString(buffer,p,i+1);
-    (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
-    (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
-    handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
-    if (handle != (void *) NULL)
-      break;
-    p=q+1;
-  }
+    }
   SetErrorMode(mode);
   return(handle);
 }
@@ -1580,7 +1724,7 @@ MagickExport void *NTOpenLibrary(const char *filename)
 %    o entry: Specifies a pointer to a DIR structure.
 %
 */
-MagickExport struct dirent *NTReadDirectory(DIR *entry)
+MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
 {
   int
     status;
@@ -1592,13 +1736,13 @@ MagickExport struct dirent *NTReadDirectory(DIR *entry)
     return((struct dirent *) NULL);
   if (!entry->firsttime)
     {
-      status=FindNextFile(entry->hSearch,&entry->Win32FindData);
+      status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
       if (status == 0)
         return((struct dirent *) NULL);
     }
-  length=CopyMagickString(entry->file_info.d_name,
-    entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
-  if (length >= sizeof(entry->file_info.d_name))
+  length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
+    entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
+  if (length == 0)
     return((struct dirent *) NULL);
   entry->firsttime=FALSE;
   entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
@@ -1622,7 +1766,8 @@ MagickExport struct dirent *NTReadDirectory(DIR *entry)
 %  may coexist.
 %
 %  Values are stored in the registry under a base path path similar to
-%  "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\5.5.7\Q:16". The provided subkey
+%  "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
+%  "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
 %  is appended to this base path to form the full key.
 %
 %  The format of the NTRegistryKeyLookup method is:
@@ -1636,7 +1781,7 @@ MagickExport struct dirent *NTReadDirectory(DIR *entry)
 %      "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
 %
 */
-MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
+MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
 {
   char
     package_key[MaxTextExtent];
@@ -1662,6 +1807,9 @@ MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
   registry_key=(HKEY) INVALID_HANDLE_VALUE;
   status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
+  if (status != ERROR_SUCCESS)
+    status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
+      &registry_key);
   if (status != ERROR_SUCCESS)
     {
       registry_key=(HKEY) INVALID_HANDLE_VALUE;
@@ -1720,7 +1868,7 @@ MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
 %    o error: MagickTrue the event is an error.
 %
 */
-MagickExport MagickBooleanType NTReportEvent(const char *event,
+MagickPrivate MagickBooleanType NTReportEvent(const char *event,
   const MagickBooleanType error)
 {
   const char
@@ -1767,10 +1915,13 @@ MagickExport MagickBooleanType NTReportEvent(const char *event,
 %    o id: Specifies a string that identifies the resource.
 %
 */
-MagickExport unsigned char *NTResourceToBlob(const char *id)
+MagickPrivate unsigned char *NTResourceToBlob(const char *id)
 {
+
+#ifndef MAGICKCORE_LIBRARY_NAME
   char
     path[MaxTextExtent];
+#endif
 
   DWORD
     length;
@@ -1790,12 +1941,16 @@ MagickExport unsigned char *NTResourceToBlob(const char *id)
 
   assert(id != (const char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
+#ifdef MAGICKCORE_LIBRARY_NAME
+  handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
+#else
   (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
     DirectorySeparator,GetClientName());
   if (IsPathAccessible(path) != MagickFalse)
     handle=GetModuleHandle(path);
   else
     handle=GetModuleHandle(0);
+#endif
   if (!handle)
     return((unsigned char *) NULL);
   resource=FindResource(handle,id,"IMAGEMAGICK");
@@ -1849,7 +2004,7 @@ MagickExport unsigned char *NTResourceToBlob(const char *id)
 %      stream.
 %
 */
-MagickExport void NTSeekDirectory(DIR *entry,ssize_t position)
+MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
 {
   (void) position;
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
@@ -1880,7 +2035,7 @@ MagickExport void NTSeekDirectory(DIR *entry,ssize_t position)
 %      for DLL's that can be dynamically loaded.
 %
 */
-MagickExport int NTSetSearchPath(const char *path)
+MagickPrivate int NTSetSearchPath(const char *path)
 {
 #if defined(MAGICKCORE_LTDL_DELEGATE)
   lt_dlsetsearchpath(path);
@@ -1919,7 +2074,7 @@ MagickExport int NTSetSearchPath(const char *path)
 %    o flags: Option flags (ignored for Windows).
 %
 */
-MagickExport int NTSyncMemory(void *address,size_t length,int flags)
+MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
 {
   (void) flags;
   if (FlushViewOfFile(address,length) == MagickFalse)
@@ -1950,7 +2105,7 @@ MagickExport int NTSyncMemory(void *address,size_t length,int flags)
 %    o command: This string is the command to execute.
 %
 */
-MagickExport int NTSystemCommand(const char *command)
+MagickPrivate int NTSystemCommand(const char *command)
 {
   char
     local_command[MaxTextExtent];
@@ -1962,7 +2117,7 @@ MagickExport int NTSystemCommand(const char *command)
     status;
 
   MagickBooleanType
-    background_process;
+    asynchronous;
 
   PROCESS_INFORMATION
     process_info;
@@ -1976,21 +2131,24 @@ MagickExport int NTSystemCommand(const char *command)
   startup_info.dwFlags=STARTF_USESHOWWINDOW;
   startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
   (void) CopyMagickString(local_command,command,MaxTextExtent);
-  background_process=command[strlen(command)-1] == '&' ? MagickTrue :
-    MagickFalse;
-  if (background_process)
-    local_command[strlen(command)-1]='\0';
-  if (command[strlen(command)-1] == '|')
-     local_command[strlen(command)-1]='\0';
-   else
-     startup_info.wShowWindow=SW_SHOWDEFAULT;
-  status=CreateProcess((LPCTSTR) NULL,local_command,
-    (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
-    (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
+  asynchronous=command[strlen(command)-1] == '&' ? MagickTrue : MagickFalse;
+  if (asynchronous != MagickFalse)
+    {
+      local_command[strlen(command)-1]='\0';
+      startup_info.wShowWindow=SW_SHOWDEFAULT;
+    }
+  else
+    if (command[strlen(command)-1] == '|')
+      local_command[strlen(command)-1]='\0';
+    else
+      startup_info.wShowWindow=SW_HIDE;
+  status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
+    NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
+    NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
     &process_info);
   if (status == 0)
     return(-1);
-  if (background_process)
+  if (asynchronous != MagickFalse)
     return(status == 0);
   status=WaitForSingleObject(process_info.hProcess,INFINITE);
   if (status != WAIT_OBJECT_0)
@@ -2026,7 +2184,7 @@ MagickExport int NTSystemCommand(const char *command)
 %    o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
 %
 */
-MagickExport ssize_t NTSystemConfiguration(int name)
+MagickPrivate ssize_t NTSystemConfiguration(int name)
 {
   switch (name)
   {
@@ -2063,12 +2221,12 @@ MagickExport ssize_t NTSystemConfiguration(int name)
             status;
 
           GlobalMemoryStatus(&status);
-          return((ssize_t) status.dwTotalPhys/system_info.dwPageSize);
+          return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
         }
       status.dwLength=sizeof(status);
       if (module(&status) == 0)
         return(0L);
-      return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
+      return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
     }
     case _SC_OPEN_MAX:
       return(2048);
@@ -2101,7 +2259,7 @@ MagickExport ssize_t NTSystemConfiguration(int name)
 %    o entry: Specifies a pointer to a DIR structure.
 %
 */
-MagickExport ssize_t NTTellDirectory(DIR *entry)
+MagickPrivate ssize_t NTTellDirectory(DIR *entry)
 {
   assert(entry != (DIR *) NULL);
   return(0);
@@ -2131,25 +2289,27 @@ MagickExport ssize_t NTTellDirectory(DIR *entry)
 %    o length: the file length.
 %
 */
-MagickExport int NTTruncateFile(int file,off_t length)
+MagickPrivate int NTTruncateFile(int file,off_t length)
 {
   DWORD
     file_pointer;
 
+  HANDLE
+    file_handle;
+
   long
-    file_handle,
     high,
     low;
 
-  file_handle=_get_osfhandle(file);
-  if (file_handle == -1L)
+  file_handle=(HANDLE) _get_osfhandle(file);
+  if (file_handle == (HANDLE) -1L)
     return(-1);
   low=(long) (length & 0xffffffffUL);
   high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
-  file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
+  file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
   if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
     return(-1);
-  if (SetEndOfFile((HANDLE) file_handle) == 0)
+  if (SetEndOfFile(file_handle) == 0)
     return(-1);
   return(0);
 }
@@ -2178,7 +2338,7 @@ MagickExport int NTTruncateFile(int file,off_t length)
 %    o length: the length of the binary large object.
 %
 */
-MagickExport int NTUnmapMemory(void *map,size_t length)
+MagickPrivate int NTUnmapMemory(void *map,size_t length)
 {
   (void) length;
   if (UnmapViewOfFile(map) == 0)
@@ -2205,7 +2365,7 @@ MagickExport int NTUnmapMemory(void *map,size_t length)
 %      double NTUserTime(void)
 %
 */
-MagickExport double NTUserTime(void)
+MagickPrivate double NTUserTime(void)
 {
   DWORD
     status;
@@ -2274,7 +2434,7 @@ MagickExport double NTUserTime(void)
 %    o description: Specifies any description to the reason.
 %
 */
-MagickExport void NTWarningHandler(const ExceptionType severity,
+MagickPrivate void NTWarningHandler(const ExceptionType severity,
   const char *reason,const char *description)
 {
   char
@@ -2292,4 +2452,47 @@ MagickExport void NTWarningHandler(const ExceptionType severity,
   (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
     MB_SETFOREGROUND | MB_ICONINFORMATION);
 }
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   N T W i n d o w s G e n e s i s                                           %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  NTWindowsGenesis() initializes the MagickCore Windows environment.
+%
+%  The format of the NTWindowsGenesis method is:
+%
+%      void NTWindowsGenesis(void)
+%
+*/
+MagickPrivate void NTWindowsGenesis(void)
+{
+  char
+    *mode;
+
+  mode=GetEnvironmentValue("MAGICK_ERRORMODE");
+  if (mode != (char *) NULL)
+    {
+      (void) SetErrorMode(StringToInteger(mode));
+      mode=DestroyString(mode);
+    }
+#if defined(_DEBUG) && !defined(__BORLANDC__) && !defined(__MINGW32__) && !defined(__MINGW64__)
+  if (IsEventLogging() != MagickFalse)
+    {
+      int
+        debug;
+
+      debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
+      debug|=_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF;
+      (void) _CrtSetDbgFlag(debug);
+      _ASSERTE(_CrtCheckMemory());
+    }
+#endif
+}
 #endif