]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/nt-base.c
(no commit message)
[imagemagick] / MagickCore / nt-base.c
index e6398f89d91ae4760b74edd51a384aa44a3ad8df..ebdb25460922f7dae0cbbefdb64b449504edf709 100644 (file)
 %                   Windows NT Utility Methods for MagickCore                 %
 %                                                                             %
 %                               Software Design                               %
-%                                 John Cristy                                 %
+%                                    Cristy                                   %
 %                                December 1996                                %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 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/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-private.h"
 #if defined(MAGICKCORE_CIPHER_SUPPORT)
 #include <ntsecapi.h>
 #include <wincrypt.h>
@@ -84,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.
 */
@@ -91,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
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -148,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] == '\\')
               {
@@ -169,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);
@@ -185,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,
@@ -194,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:
@@ -236,9 +282,11 @@ BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
 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__) && !defined(__MINGW64__)
@@ -376,23 +424,14 @@ MagickPrivate 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);
 }
@@ -700,7 +739,12 @@ MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
 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
@@ -931,8 +975,8 @@ static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *
   return(1);  /* not found */
 }
 
-static int NTLocateGhostscript(DWORD flags,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;
@@ -953,6 +997,7 @@ static int NTLocateGhostscript(DWORD flags,const char **product_family,int *majo
     Find the most recent version of Ghostscript.
   */
   status=MagickFalse;
+  *root_index=0;
   *product_family=NULL;
   *major_version=5;
   *minor_version=49; /* min version of Ghostscript is 5.50 */
@@ -962,48 +1007,55 @@ static int NTLocateGhostscript(DWORD flags,const char **product_family,int *majo
       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 | flags;
-    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=MagickTrue;
-            }
+            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 == MagickFalse)
     {
@@ -1015,101 +1067,103 @@ static int NTLocateGhostscript(DWORD flags,const char **product_family,int *majo
   return(status);
 }
 
-static BOOL NTIs64BitPlatform()
-{
-#if defined(_WIN64) || !defined(KEY_WOW64_32KEY)
-  return(TRUE);
-#else
-  BOOL is64=FALSE;
-  return(IsWow64Process(GetCurrentProcess(), &is64) && is64);
-#endif
-}
-
-static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,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;
+    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.
   */
-  if (is_64_bit!=NULL)
-    *is_64_bit=FALSE;
   *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)
-  {
-    flags=0;
-#if defined(KEY_WOW64_32KEY)
-    flags=NTIs64BitPlatform() ? KEY_WOW64_64KEY : 0;
-#endif
-    (void) NTLocateGhostscript(flags,&product_family,&major_version,
-      &minor_version);
-    if (product_family == NULL)
     {
-      if (flags!=0)
-      {
-        /*
-          We are running on a 64 bit platform - check for a 32 bit Ghostscript.
-        */
-        flags=0;
+      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
-        (void) NTLocateGhostscript(flags,&product_family,&major_version,
-          &minor_version);
-      }
     }
-    else
-      is_64_bit_version=NTIs64BitPlatform();
-  }
-  if (is_64_bit != NULL)
-    *is_64_bit=is_64_bit_version;
   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,flags,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);
 }
 
@@ -1247,6 +1301,7 @@ MagickPrivate int NTGhostscriptFonts(char *path,int length)
 {
   char
     buffer[MaxTextExtent],
+    *directory,
     filename[MaxTextExtent];
 
   register char
@@ -1254,8 +1309,17 @@ MagickPrivate int NTGhostscriptFonts(char *path,int length)
     *q;
 
   *path='\0';
-  if (NTGhostscriptGetString("GS_LIB",NULL,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);
@@ -1267,6 +1331,7 @@ MagickPrivate int NTGhostscriptFonts(char *path,int length)
     if (IsPathAccessible(filename) != MagickFalse)
       return(TRUE);
   }
+  *path='\0';
   return(FALSE);
 }
 \f
@@ -1451,7 +1516,7 @@ MagickPrivate 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
@@ -1480,38 +1545,38 @@ MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
 */
 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);
@@ -1582,23 +1647,29 @@ static UINT ChangeErrorMode(void)
   return SetErrorMode(mode);
 }
 
-MagickPrivate void *NTOpenLibrary(const char *filename)
+static inline void *NTLoadLibrary(const char *filename)
 {
-#define MaxPathElements 31
+  int
+    length;
 
-  char
-    buffer[MaxTextExtent];
+  wchar_t
+    path[MaxTextExtent];
 
-  int
-    index;
+  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;
 
@@ -1606,35 +1677,25 @@ MagickPrivate void *NTOpenLibrary(const char *filename)
     *handle;
 
   mode=ChangeErrorMode();
-  handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
-  if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
+  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);
 }
@@ -1675,13 +1736,13 @@ MagickPrivate 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);
@@ -2056,7 +2117,7 @@ MagickPrivate int NTSystemCommand(const char *command)
     status;
 
   MagickBooleanType
-    background_process;
+    asynchronous;
 
   PROCESS_INFORMATION
     process_info;
@@ -2070,21 +2131,24 @@ MagickPrivate 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 != MagickFalse)
-    local_command[strlen(command)-1]='\0';
-  if (command[strlen(command)-1] == '|')
-     local_command[strlen(command)-1]='\0';
-   else
-     startup_info.wShowWindow=SW_SHOWDEFAULT;
+  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 != MagickFalse)
+  if (asynchronous != MagickFalse)
     return(status == 0);
   status=WaitForSingleObject(process_info.hProcess,INFINITE);
   if (status != WAIT_OBJECT_0)
@@ -2230,20 +2294,22 @@ 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);
 }
@@ -2386,4 +2452,47 @@ MagickPrivate 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