2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Windows NT Utility Methods for MagickCore %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "MagickCore/studio.h"
42 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
43 #include "MagickCore/client.h"
44 #include "MagickCore/exception-private.h"
45 #include "MagickCore/locale_.h"
46 #include "MagickCore/log.h"
47 #include "MagickCore/magick.h"
48 #include "MagickCore/memory_.h"
49 #include "MagickCore/nt-base.h"
50 #include "MagickCore/nt-base-private.h"
51 #include "MagickCore/resource_.h"
52 #include "MagickCore/resource-private.h"
53 #include "MagickCore/timer.h"
54 #include "MagickCore/string_.h"
55 #include "MagickCore/string-private.h"
56 #include "MagickCore/utility.h"
57 #include "MagickCore/utility-private.h"
58 #include "MagickCore/version.h"
59 #if defined(MAGICKCORE_LTDL_DELEGATE)
62 #if defined(MAGICKCORE_CIPHER_SUPPORT)
70 #if !defined(MAP_FAILED)
71 #define MAP_FAILED ((void *) -1)
77 #if !defined(MAGICKCORE_LTDL_DELEGATE)
79 *lt_slsearchpath = (char *) NULL;
86 *ghost_handle = (void *) NULL;
96 const registry_roots[2] =
98 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
99 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
103 External declarations.
105 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
106 extern "C" BOOL WINAPI
107 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % DllMain() is an entry point to the DLL which is called when processes and
122 % threads are initialized and terminated, or upon calls to the Windows
123 % LoadLibrary and FreeLibrary functions.
125 % The function returns TRUE of it succeeds, or FALSE if initialization fails.
127 % The format of the DllMain method is:
129 % BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
131 % A description of each parameter follows:
133 % o handle: handle to the DLL module
135 % o reason: reason for calling function:
137 % DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
138 % space of current process.
139 % DLL_THREAD_ATTACH - Indicates that the current process is
140 % creating a new thread. Called under the
141 % context of the new thread.
142 % DLL_THREAD_DETACH - Indicates that the thread is exiting.
143 % Called under the context of the exiting
145 % DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
146 % from the virtual address space of the
149 % o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
150 % and DLL_PROCESS_DETACH.
153 #if defined(_DLL) && defined( ProvideDllMain )
154 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
158 case DLL_PROCESS_ATTACH:
169 wide_path=(wchar_t *) AcquireQuantumMemory(MaxTextExtent,
171 if (wide_path == (wchar_t *) NULL)
173 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MaxTextExtent);
179 module_path=create_utf8_string(wide_path);
180 for ( ; count > 0; count--)
181 if (module_path[count] == '\\')
183 module_path[count+1]='\0';
186 MagickCoreGenesis(module_path,MagickFalse);
187 path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
188 if (path == (char *) NULL)
190 module_path=DestroyString(module_path);
191 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
194 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
195 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
197 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
202 variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
204 if (variable == (char *) NULL)
206 path=DestroyString(path);
207 module_path=DestroyString(module_path);
208 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
211 (void) FormatLocaleString(variable,16*MaxTextExtent,
212 "%s;%s",module_path,path);
213 SetEnvironmentVariable("PATH",variable);
214 variable=DestroyString(variable);
217 path=DestroyString(path);
218 module_path=DestroyString(module_path);
220 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
223 case DLL_PROCESS_DETACH:
225 MagickCoreTerminus();
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % Exit() calls TerminateProcess for Win95.
248 % The format of the exit method is:
250 % int Exit(int status)
252 % A description of each parameter follows:
254 % o status: an integer value representing the status of the terminating
258 MagickPrivate int Exit(int status)
262 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
268 #if !defined(__MINGW32__) && !defined(__MINGW64__)
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % g e t t i m e o f d a y %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % The gettimeofday() method get the time of day.
282 % The format of the gettimeofday method is:
284 % int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
286 % A description of each parameter follows:
288 % o time_value: the time value.
290 % o time_zone: the time zone.
293 MagickPrivate int gettimeofday (struct timeval *time_value,
294 struct timezone *time_zone)
296 #define EpochFiletime MagickLLConstant(116444736000000000)
301 if (time_value != (struct timeval *) NULL)
312 GetSystemTimeAsFileTime(&file_time);
313 date_time.LowPart=file_time.dwLowDateTime;
314 date_time.HighPart=file_time.dwHighDateTime;
315 time=date_time.QuadPart;
318 time_value->tv_sec=(ssize_t) (time / 1000000);
319 time_value->tv_usec=(ssize_t) (time % 1000000);
321 if (time_zone != (struct timezone *) NULL)
328 time_zone->tz_minuteswest=_timezone/60;
329 time_zone->tz_dsttime=_daylight;
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % I s W i n d o w s 9 5 %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % IsWindows95() returns true if the system is Windows 95.
348 % The format of the IsWindows95 method is:
353 MagickPrivate int IsWindows95()
358 version_info.dwOSVersionInfoSize=sizeof(version_info);
359 if (GetVersionEx(&version_info) &&
360 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % N T A r g v T o U T F 8 %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
377 % compatibility with Linux.
379 % The format of the NTArgvToUTF8 method is:
381 % char **NTArgvToUTF8(const int argc,wchar_t **argv)
383 % A description of each parameter follows:
385 % o argc: the number of command line arguments.
387 % o argv: the wide-character command line arguments.
390 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
398 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
399 if (utf8 == (char **) NULL)
400 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
401 for (i=0; i < (ssize_t) argc; i++)
403 utf8[i]=create_utf8_string(argv[i]);
404 if (utf8[i] == (char *) NULL)
406 for (i--; i >= 0; i--)
407 utf8[i]=DestroyString(utf8[i]);
408 ThrowFatalException(ResourceLimitFatalError,
409 "UnableToConvertStringToARGV");
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 % N T C l o s e D i r e c t o r y %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % NTCloseDirectory() closes the named directory stream and frees the DIR
429 % The format of the NTCloseDirectory method is:
431 % int NTCloseDirectory(DIR *entry)
433 % A description of each parameter follows:
435 % o entry: Specifies a pointer to a DIR structure.
438 MagickPrivate int NTCloseDirectory(DIR *entry)
440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
441 assert(entry != (DIR *) NULL);
442 FindClose(entry->hSearch);
443 entry=(DIR *) RelinquishMagickMemory(entry);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % N T C l o s e L i b r a r y %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % NTCloseLibrary() unloads the module associated with the passed handle.
460 % The format of the NTCloseLibrary method is:
462 % void NTCloseLibrary(void *handle)
464 % A description of each parameter follows:
466 % o handle: Specifies a handle to a previously loaded dynamic module.
469 MagickPrivate int NTCloseLibrary(void *handle)
472 return(FreeLibrary((HINSTANCE) handle));
473 return(!(FreeLibrary((HINSTANCE) handle)));
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % N T C o n t r o l H a n d l e r %
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 % NTControlHandler() registers a control handler that is activated when, for
488 % example, a ctrl-c is received.
490 % The format of the NTControlHandler method is:
492 % int NTControlHandler(void)
496 static BOOL ControlHandler(DWORD type)
499 AsynchronousResourceComponentTerminus();
503 MagickPrivate int NTControlHandler(void)
505 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 % N T E l a p s e d T i m e %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % NTElapsedTime() returns the elapsed time (in seconds) since the last call to
522 % The format of the ElapsedTime method is:
524 % double NTElapsedTime(void)
527 MagickPrivate double NTElapsedTime(void)
541 GetSystemTime(&system_time);
542 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
543 return((double) 1.0e-7*elapsed_time.filetime64);
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 + N T E r r o r H a n d l e r %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % NTErrorHandler() displays an error reason and then terminates the program.
559 % The format of the NTErrorHandler method is:
561 % void NTErrorHandler(const ExceptionType severity,const char *reason,
562 % const char *description)
564 % A description of each parameter follows:
566 % o severity: Specifies the numeric error category.
568 % o reason: Specifies the reason to display before terminating the
571 % o description: Specifies any description to the reason.
574 MagickPrivate void NTErrorHandler(const ExceptionType severity,
575 const char *reason,const char *description)
578 buffer[3*MaxTextExtent],
582 if (reason == (char *) NULL)
584 MagickCoreTerminus();
587 message=GetExceptionMessage(errno);
588 if ((description != (char *) NULL) && errno)
589 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
590 GetClientName(),reason,description,message);
592 if (description != (char *) NULL)
593 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
594 GetClientName(),reason,description);
597 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
598 GetClientName(),reason,message);
600 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
601 GetClientName(),reason);
602 message=DestroyString(message);
603 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
604 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
605 MagickCoreTerminus();
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % N T E x i t L i b r a r y %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % NTExitLibrary() exits the dynamic module loading subsystem.
622 % The format of the NTExitLibrary method is:
624 % int NTExitLibrary(void)
627 MagickPrivate int NTExitLibrary(void)
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % N T G a t h e r R a n d o m D a t a %
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % NTGatherRandomData() gathers random data and returns it.
645 % The format of the GatherRandomData method is:
647 % MagickBooleanType NTGatherRandomData(const size_t length,
648 % unsigned char *random)
650 % A description of each parameter follows:
652 % length: the length of random data buffer
654 % random: the random data is returned here.
657 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
658 unsigned char *random)
660 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
667 handle=(HCRYPTPROV) NULL;
668 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
669 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
671 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
672 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
675 status=CryptGenRandom(handle,(DWORD) length,random);
678 status=CryptReleaseContext(handle,0);
681 status=CryptReleaseContext(handle,0);
692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 % N T G e t E x e c u t i o n P a t h %
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % NTGetExecutionPath() returns the execution path of a program.
704 % The format of the GetExecutionPath method is:
706 % MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
708 % A description of each parameter follows:
710 % o path: the pathname of the executable that started the process.
712 % o extent: the maximum extent of the path.
715 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
719 wide_path[MaxTextExtent];
721 (void) GetModuleFileNameW(0,wide_path,(DWORD) extent);
722 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,
728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % N T G e t L a s t E r r o r %
736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 % NTGetLastError() returns the last error that occurred.
740 % The format of the NTGetLastError method is:
742 % char *NTGetLastError(void)
745 char *NTGetLastError(void)
756 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
757 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
758 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
760 reason=AcquireString("An unknown error occurred");
763 reason=AcquireString((const char *) buffer);
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % N T G e t L i b r a r y E r r o r %
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % Lt_dlerror() returns a pointer to a string describing the last error
781 % associated with a lt_dl method. Note that this function is not thread
782 % safe so it should only be used under the protection of a lock.
784 % The format of the NTGetLibraryError method is:
786 % const char *NTGetLibraryError(void)
789 MagickPrivate const char *NTGetLibraryError(void)
792 last_error[MaxTextExtent];
798 error=NTGetLastError();
800 (void) CopyMagickString(last_error,error,MaxTextExtent);
801 error=DestroyString(error);
806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % N T G e t L i b r a r y S y m b o l %
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 % NTGetLibrarySymbol() retrieve the procedure address of the method
817 % specified by the passed character string.
819 % The format of the NTGetLibrarySymbol method is:
821 % void *NTGetLibrarySymbol(void *handle,const char *name)
823 % A description of each parameter follows:
825 % o handle: Specifies a handle to the previously loaded dynamic module.
827 % o name: Specifies the procedure entry point to be returned.
830 void *NTGetLibrarySymbol(void *handle,const char *name)
835 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
837 return((void *) NULL);
838 return((void *) lpfnDllFunc1);
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 % N T G e t M o d u l e P a t h %
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 % NTGetModulePath() returns the path of the specified module.
854 % The format of the GetModulePath method is:
856 % MagickBooleanType NTGetModulePath(const char *module,char *path)
858 % A description of each parameter follows:
860 % modith: the module name.
862 % path: the module path is returned here.
865 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
868 module_path[MaxTextExtent];
877 handle=GetModuleHandle(module);
878 if (handle == (HMODULE) NULL)
880 length=GetModuleFileName(handle,module_path,MaxTextExtent);
882 GetPathComponent(module_path,HeadPath,path);
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 % N T G h o s t s c r i p t D L L %
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 % NTGhostscriptDLL() returns the path to the most recent Ghostscript version
898 % DLL. The method returns TRUE on success otherwise FALSE.
900 % The format of the NTGhostscriptDLL method is:
902 % int NTGhostscriptDLL(char *path,int length)
904 % A description of each parameter follows:
906 % o path: return the Ghostscript DLL path here.
908 % o length: the buffer length.
912 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
913 char *value,int *length)
930 Get a registry value: key = root\\key, named value = name.
932 if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
933 return(1); /* no match */
937 if (p == (BYTE *) NULL)
938 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
939 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
941 if (status == ERROR_SUCCESS)
944 return(0); /* return the match */
946 if (status == ERROR_MORE_DATA)
949 return(-1); /* buffer not large enough */
951 return(1); /* not found */
954 static int NTLocateGhostscript(DWORD flags,int *root_index,
955 const char **product_family,int *major_version,int *minor_version)
969 "Aladdin Ghostscript"
973 Find the most recent version of Ghostscript.
977 *product_family=NULL;
979 *minor_version=49; /* min version of Ghostscript is 5.50 */
980 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
994 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
995 for (j=0; j < (ssize_t) (sizeof(registry_roots)/sizeof(registry_roots[0]));
998 mode=KEY_READ | flags;
999 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1009 Now enumerate the keys.
1011 extent=sizeof(key)/sizeof(char);
1012 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1020 if (sscanf(key,"%d.%d",&major,&minor) != 2)
1022 if ((major > *major_version) || ((major == *major_version) &&
1023 (minor > *minor_version)))
1026 *product_family=products[i];
1027 *major_version=major;
1028 *minor_version=minor;
1032 (void) RegCloseKey(hkey);
1036 if (status == MagickFalse)
1041 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1042 "version %d.%02d",*product_family,*major_version,*minor_version);
1046 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,
1047 char *value,const size_t length)
1050 buffer[MaxTextExtent],
1057 *product_family=(const char *) NULL;
1060 is_64_bit_version=FALSE;
1069 Get a string from the installed Ghostscript.
1072 directory=(char *) NULL;
1073 if (LocaleCompare(name, "GS_DLL") == 0)
1075 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_PATH");
1076 if (directory != (char *) NULL)
1078 (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll32.dll",
1079 directory,DirectorySeparator);
1080 if (IsPathAccessible(buffer) != MagickFalse)
1082 (void) CopyMagickString(value,buffer,length);
1083 if (is_64_bit != NULL)
1087 (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll64.dll",
1088 directory,DirectorySeparator);
1089 if (IsPathAccessible(buffer) != MagickFalse)
1091 (void) CopyMagickString(value,buffer,length);
1092 if (is_64_bit != NULL)
1099 if (product_family == NULL)
1102 #if defined(KEY_WOW64_32KEY)
1104 flags=KEY_WOW64_64KEY;
1106 flags=KEY_WOW64_32KEY;
1108 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1109 &major_version,&minor_version);
1110 if (product_family == NULL)
1112 flags=KEY_WOW64_32KEY;
1114 is_64_bit_version=TRUE;
1116 flags=KEY_WOW64_64KEY;
1120 if (product_family == NULL)
1122 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1123 &major_version,&minor_version);
1125 if (product_family == NULL)
1127 if (is_64_bit != NULL)
1128 *is_64_bit=is_64_bit_version;
1129 (void) FormatLocaleString(buffer,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1130 product_family,major_version,minor_version);
1131 extent=(int) length;
1132 if (NTGetRegistryValue(registry_roots[root_index].hkey,buffer,flags,name,
1133 value,&extent) == 0)
1135 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1136 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1143 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1146 dll[MaxTextExtent] = { "" };
1152 if ((*dll == '\0') &&
1153 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1157 if (!is_64_bit_version)
1160 if (is_64_bit_version)
1163 (void) CopyMagickString(path,dll,length);
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 % N T G h o s t s c r i p t D L L V e c t o r s %
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1178 % NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1179 % function vectors to invoke Ghostscript DLL functions. A null pointer is
1180 % returned if there is an error when loading the DLL or retrieving the
1183 % The format of the NTGhostscriptDLLVectors method is:
1185 % const GhostInfo *NTGhostscriptDLLVectors(void)
1188 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1190 if (NTGhostscriptLoadDLL() == FALSE)
1191 return((GhostInfo *) NULL);
1192 return(&ghost_info);
1196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 % N T G h o s t s c r i p t E X E %
1204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206 % NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1207 % The method returns FALSE if a full path value is not obtained and returns
1208 % a default path of gswin32c.exe.
1210 % The format of the NTGhostscriptEXE method is:
1212 % int NTGhostscriptEXE(char *path,int length)
1214 % A description of each parameter follows:
1216 % o path: return the Ghostscript executable path here.
1218 % o length: length of buffer.
1221 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1227 program[MaxTextExtent] = { "" };
1230 is_64_bit_version = FALSE;
1232 (void) CopyMagickString(path,"gswin32c.exe",length);
1233 if ((*program == '\0') &&
1234 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1236 p=strrchr(program,'\\');
1237 if (p != (char *) NULL)
1241 (void) ConcatenateMagickString(program,is_64_bit_version ?
1242 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1244 (void) CopyMagickString(path,program,length);
1249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 % N T G h o s t s c r i p t F o n t s %
1257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 % NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1260 % returns FALSE if it cannot determine the font path.
1262 % The format of the NTGhostscriptFonts method is:
1264 % int NTGhostscriptFonts(char *path, int length)
1266 % A description of each parameter follows:
1268 % o path: return the font path here.
1270 % o length: length of the path buffer.
1273 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1276 buffer[MaxTextExtent],
1278 filename[MaxTextExtent];
1285 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_FONT_PATH");
1286 if (directory != (char *) NULL)
1288 (void) CopyMagickString(buffer,directory,MaxTextExtent);
1289 directory=DestroyString(directory);
1293 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1296 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1298 (void) CopyMagickString(path,p+1,length+1);
1299 q=strchr(path,DirectoryListSeparator);
1300 if (q != (char *) NULL)
1302 (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1303 DirectorySeparator);
1304 if (IsPathAccessible(filename) != MagickFalse)
1312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1316 % N T G h o s t s c r i p t L o a d D L L %
1320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1322 % NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1323 % TRUE if it succeeds.
1325 % The format of the NTGhostscriptLoadDLL method is:
1327 % int NTGhostscriptLoadDLL(void)
1330 MagickPrivate int NTGhostscriptLoadDLL(void)
1333 path[MaxTextExtent];
1335 if (ghost_handle != (void *) NULL)
1337 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1339 ghost_handle=lt_dlopen(path);
1340 if (ghost_handle == (void *) NULL)
1342 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1343 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1344 lt_dlsym(ghost_handle,"gsapi_exit");
1345 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1346 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1347 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1348 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1349 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1350 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1351 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1352 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1353 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1354 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1355 (ghost_info.delete_instance == NULL))
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 % N T G h o s t s c r i p t U n L o a d D L L %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1374 % The format of the NTGhostscriptUnLoadDLL method is:
1376 % int NTGhostscriptUnLoadDLL(void)
1379 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1384 if (ghost_handle == (void *) NULL)
1386 status=lt_dlclose(ghost_handle);
1387 ghost_handle=(void *) NULL;
1388 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 % N T I n i t i a l i z e L i b r a r y %
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403 % NTInitializeLibrary() initializes the dynamic module loading subsystem.
1405 % The format of the NTInitializeLibrary method is:
1407 % int NTInitializeLibrary(void)
1410 MagickPrivate int NTInitializeLibrary(void)
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 + N T M a p M e m o r y %
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 % Mmap() emulates the Unix method of the same name.
1428 % The format of the NTMapMemory method is:
1430 % MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1431 % int access,int file,MagickOffsetType offset)
1434 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1435 int flags,int file,MagickOffsetType offset)
1454 file_handle=INVALID_HANDLE_VALUE;
1455 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1456 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1457 map_handle=INVALID_HANDLE_VALUE;
1459 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1460 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1462 if (protection & PROT_WRITE)
1464 access_mode=FILE_MAP_WRITE;
1465 if (!(flags & MAP_PRIVATE))
1466 protection_mode=PAGE_READWRITE;
1469 access_mode=FILE_MAP_COPY;
1470 protection_mode=PAGE_WRITECOPY;
1474 if (protection & PROT_READ)
1476 access_mode=FILE_MAP_READ;
1477 protection_mode=PAGE_READONLY;
1479 if ((file == -1) && (flags & MAP_ANONYMOUS))
1480 file_handle=INVALID_HANDLE_VALUE;
1482 file_handle=(HANDLE) _get_osfhandle(file);
1483 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1487 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1489 CloseHandle(map_handle);
1491 if (map == (void *) NULL)
1492 return((void *) ((char *) MAP_FAILED));
1493 return((void *) ((char *) map));
1497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1501 % N T O p e n D i r e c t o r y %
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507 % NTOpenDirectory() opens the directory named by filename and associates a
1508 % directory stream with it.
1510 % The format of the NTOpenDirectory method is:
1512 % DIR *NTOpenDirectory(const char *path)
1514 % A description of each parameter follows:
1516 % o entry: Specifies a pointer to a DIR structure.
1519 MagickPrivate DIR *NTOpenDirectory(const char *path)
1528 file_specification[MaxTextExtent];
1530 assert(path != (const char *) NULL);
1531 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1534 return((DIR *) NULL);
1535 if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1536 MaxTextExtent) == (wchar_t*)NULL)
1537 return((DIR *) NULL);
1538 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1539 if (entry != (DIR *) NULL)
1541 entry->firsttime=TRUE;
1542 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1544 if (entry->hSearch == INVALID_HANDLE_VALUE)
1546 if(wcsncat(file_specification,L"*.*",
1547 MaxTextExtent) == (wchar_t*)NULL)
1549 entry=(DIR *) RelinquishMagickMemory(entry);
1550 return((DIR *) NULL);
1552 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1553 if (entry->hSearch == INVALID_HANDLE_VALUE)
1555 entry=(DIR *) RelinquishMagickMemory(entry);
1556 return((DIR *) NULL);
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 % N T O p e n L i b r a r y %
1571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1573 % NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1574 % can be used to access the various procedures in the module.
1576 % The format of the NTOpenLibrary method is:
1578 % void *NTOpenLibrary(const char *filename)
1580 % A description of each parameter follows:
1582 % o path: Specifies a pointer to string representing dynamic module that
1587 static inline const char *GetSearchPath(void)
1589 #if defined(MAGICKCORE_LTDL_DELEGATE)
1590 return(lt_dlgetsearchpath());
1592 return(lt_slsearchpath);
1596 static UINT ChangeErrorMode(void)
1599 (CALLBACK *GETERRORMODE)(void);
1610 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1612 handle=GetModuleHandle("kernel32.dll");
1613 if (handle == (HMODULE) NULL)
1614 return SetErrorMode(mode);
1616 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1617 if (getErrorMode != (GETERRORMODE) NULL)
1618 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1620 return SetErrorMode(mode);
1623 static inline void *NTLoadLibrary(const char *filename)
1629 path[MaxTextExtent];
1631 length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MaxTextExtent);
1633 return((void *) NULL);
1634 return (void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1637 MagickPrivate void *NTOpenLibrary(const char *filename)
1640 path[MaxTextExtent];
1652 mode=ChangeErrorMode();
1653 handle=NTLoadLibrary(filename);
1654 if (handle == (void *) NULL)
1657 while (p != (const char*) NULL)
1659 q=strchr(p,DirectoryListSeparator);
1660 if (q != (const char*) NULL)
1661 (void) CopyMagickString(path,p,q-p+1);
1663 (void) CopyMagickString(path,p,MaxTextExtent);
1664 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
1665 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
1666 handle=NTLoadLibrary(path);
1667 if (handle != (void *) NULL || q == (const char*) NULL)
1677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1681 % N T R e a d D i r e c t o r y %
1685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687 % NTReadDirectory() returns a pointer to a structure representing the
1688 % directory entry at the current position in the directory stream to which
1691 % The format of the NTReadDirectory
1693 % NTReadDirectory(entry)
1695 % A description of each parameter follows:
1697 % o entry: Specifies a pointer to a DIR structure.
1700 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1708 if (entry == (DIR *) NULL)
1709 return((struct dirent *) NULL);
1710 if (!entry->firsttime)
1712 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1714 return((struct dirent *) NULL);
1716 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1717 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1719 return((struct dirent *) NULL);
1720 entry->firsttime=FALSE;
1721 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1722 return(&entry->file_info);
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % N T R e g i s t r y K e y L o o k u p %
1734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736 % NTRegistryKeyLookup() returns ImageMagick installation path settings
1737 % stored in the Windows Registry. Path settings are specific to the
1738 % installed ImageMagick version so that multiple Image Magick installations
1741 % Values are stored in the registry under a base path path similar to
1742 % "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1743 % "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1744 % is appended to this base path to form the full key.
1746 % The format of the NTRegistryKeyLookup method is:
1748 % unsigned char *NTRegistryKeyLookup(const char *subkey)
1750 % A description of each parameter follows:
1752 % o subkey: Specifies a string that identifies the registry object.
1753 % Currently supported sub-keys include: "BinPath", "ConfigurePath",
1754 % "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1757 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1760 package_key[MaxTextExtent];
1778 (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1779 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1780 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1781 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1782 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,®istry_key);
1783 if (status != ERROR_SUCCESS)
1784 status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1786 if (status != ERROR_SUCCESS)
1788 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1789 return((unsigned char *) NULL);
1795 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1796 if (value == (unsigned char *) NULL)
1798 RegCloseKey(registry_key);
1799 return((unsigned char *) NULL);
1801 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1802 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1803 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1805 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1806 if (value == (BYTE *) NULL)
1808 RegCloseKey(registry_key);
1809 return((unsigned char *) NULL);
1811 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1813 RegCloseKey(registry_key);
1814 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1815 value=(unsigned char *) RelinquishMagickMemory(value);
1816 return((unsigned char *) value);
1820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1824 % N T R e p o r t E v e n t %
1828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 % NTReportEvent() reports an event.
1832 % The format of the NTReportEvent method is:
1834 % MagickBooleanType NTReportEvent(const char *event,
1835 % const MagickBooleanType error)
1837 % A description of each parameter follows:
1839 % o event: the event.
1841 % o error: MagickTrue the event is an error.
1844 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1845 const MagickBooleanType error)
1856 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1858 return(MagickFalse);
1860 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1861 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1862 DeregisterEventSource(handle);
1867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871 % N T R e s o u r c e T o B l o b %
1875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1877 % NTResourceToBlob() returns a blob containing the contents of the resource
1878 % in the current executable specified by the id parameter. This currently
1879 % used to retrieve MGK files tha have been embedded into the various command
1882 % The format of the NTResourceToBlob method is:
1884 % unsigned char *NTResourceToBlob(const char *id)
1886 % A description of each parameter follows:
1888 % o id: Specifies a string that identifies the resource.
1891 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1894 #ifndef MAGICKCORE_LIBRARY_NAME
1896 path[MaxTextExtent];
1915 assert(id != (const char *) NULL);
1916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1917 #ifdef MAGICKCORE_LIBRARY_NAME
1918 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1920 (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1921 DirectorySeparator,GetClientName());
1922 if (IsPathAccessible(path) != MagickFalse)
1923 handle=GetModuleHandle(path);
1925 handle=GetModuleHandle(0);
1928 return((unsigned char *) NULL);
1929 resource=FindResource(handle,id,"IMAGEMAGICK");
1931 return((unsigned char *) NULL);
1932 global=LoadResource(handle,resource);
1934 return((unsigned char *) NULL);
1935 length=SizeofResource(handle,resource);
1936 value=(unsigned char *) LockResource(global);
1939 FreeResource(global);
1940 return((unsigned char *) NULL);
1942 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1944 if (blob != (unsigned char *) NULL)
1946 (void) CopyMagickMemory(blob,value,length);
1949 UnlockResource(global);
1950 FreeResource(global);
1955 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959 % N T S e e k D i r e c t o r y %
1963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1965 % NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1966 % on the directory stream.
1968 % The format of the NTSeekDirectory method is:
1970 % void NTSeekDirectory(DIR *entry,ssize_t position)
1972 % A description of each parameter follows:
1974 % o entry: Specifies a pointer to a DIR structure.
1976 % o position: specifies the position associated with the directory
1980 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1983 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1984 assert(entry != (DIR *) NULL);
1988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1992 % N T S e t S e a r c h P a t h %
1996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1998 % NTSetSearchPath() sets the current locations that the subsystem should
1999 % look at to find dynamically loadable modules.
2001 % The format of the NTSetSearchPath method is:
2003 % int NTSetSearchPath(const char *path)
2005 % A description of each parameter follows:
2007 % o path: Specifies a pointer to string representing the search path
2008 % for DLL's that can be dynamically loaded.
2011 MagickPrivate int NTSetSearchPath(const char *path)
2013 #if defined(MAGICKCORE_LTDL_DELEGATE)
2014 lt_dlsetsearchpath(path);
2016 if (lt_slsearchpath != (char *) NULL)
2017 lt_slsearchpath=DestroyString(lt_slsearchpath);
2018 if (path != (char *) NULL)
2019 lt_slsearchpath=AcquireString(path);
2025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029 + N T S y n c M e m o r y %
2033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2035 % NTSyncMemory() emulates the Unix method of the same name.
2037 % The format of the NTSyncMemory method is:
2039 % int NTSyncMemory(void *address,size_t length,int flags)
2041 % A description of each parameter follows:
2043 % o address: the address of the binary large object.
2045 % o length: the length of the binary large object.
2047 % o flags: Option flags (ignored for Windows).
2050 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2053 if (FlushViewOfFile(address,length) == MagickFalse)
2059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2063 % N T S y s t e m C o m m a n d %
2067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069 % NTSystemCommand() executes the specified command and waits until it
2070 % terminates. The returned value is the exit status of the command.
2072 % The format of the NTSystemCommand method is:
2074 % int NTSystemCommand(MagickFalse,const char *command)
2076 % A description of each parameter follows:
2078 % o command: This string is the command to execute.
2081 MagickPrivate int NTSystemCommand(const char *command)
2084 local_command[MaxTextExtent];
2101 if (command == (char *) NULL)
2103 GetStartupInfo(&startup_info);
2104 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2105 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2106 (void) CopyMagickString(local_command,command,MaxTextExtent);
2107 asynchronous=command[strlen(command)-1] == '&' ? MagickTrue : MagickFalse;
2108 if (asynchronous != MagickFalse)
2110 local_command[strlen(command)-1]='\0';
2111 startup_info.wShowWindow=SW_SHOWDEFAULT;
2114 if (command[strlen(command)-1] == '|')
2115 local_command[strlen(command)-1]='\0';
2117 startup_info.wShowWindow=SW_HIDE;
2118 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2119 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2120 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2124 if (asynchronous != MagickFalse)
2125 return(status == 0);
2126 status=WaitForSingleObject(process_info.hProcess,INFINITE);
2127 if (status != WAIT_OBJECT_0)
2129 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2132 CloseHandle(process_info.hProcess);
2133 CloseHandle(process_info.hThread);
2134 return((int) child_status);
2138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2142 % N T S y s t e m C o n i f i g u r a t i o n %
2146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2148 % NTSystemConfiguration() provides a way for the application to determine
2149 % values for system limits or options at runtime.
2151 % The format of the exit method is:
2153 % ssize_t NTSystemConfiguration(int name)
2155 % A description of each parameter follows:
2157 % o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2160 MagickPrivate ssize_t NTSystemConfiguration(int name)
2169 GetSystemInfo(&system_info);
2170 return(system_info.dwPageSize);
2172 case _SC_PHYS_PAGES:
2186 handle=GetModuleHandle("kernel32.dll");
2187 if (handle == (HMODULE) NULL)
2189 GetSystemInfo(&system_info);
2190 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2191 if (module == (LPFNDLLFUNC2) NULL)
2196 GlobalMemoryStatus(&status);
2197 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2199 status.dwLength=sizeof(status);
2200 if (module(&status) == 0)
2202 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2217 % N T T e l l D i r e c t o r y %
2221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2223 % NTTellDirectory() returns the current location associated with the named
2226 % The format of the NTTellDirectory method is:
2228 % ssize_t NTTellDirectory(DIR *entry)
2230 % A description of each parameter follows:
2232 % o entry: Specifies a pointer to a DIR structure.
2235 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2237 assert(entry != (DIR *) NULL);
2242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2246 % N T T r u n c a t e F i l e %
2250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2252 % NTTruncateFile() truncates a file to a specified length.
2254 % The format of the NTTruncateFile method is:
2256 % int NTTruncateFile(int file,off_t length)
2258 % A description of each parameter follows:
2262 % o length: the file length.
2265 MagickPrivate int NTTruncateFile(int file,off_t length)
2277 file_handle=(HANDLE) _get_osfhandle(file);
2278 if (file_handle == (HANDLE) -1L)
2280 low=(long) (length & 0xffffffffUL);
2281 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2282 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2283 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2285 if (SetEndOfFile(file_handle) == 0)
2291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2295 + N T U n m a p M e m o r y %
2299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2301 % NTUnmapMemory() emulates the Unix munmap method.
2303 % The format of the NTUnmapMemory method is:
2305 % int NTUnmapMemory(void *map,size_t length)
2307 % A description of each parameter follows:
2309 % o map: the address of the binary large object.
2311 % o length: the length of the binary large object.
2314 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2317 if (UnmapViewOfFile(map) == 0)
2323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327 % N T U s e r T i m e %
2331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2333 % NTUserTime() returns the total time the process has been scheduled (e.g.
2334 % seconds) since the last call to StartTimer().
2336 % The format of the UserTime method is:
2338 % double NTUserTime(void)
2341 MagickPrivate double NTUserTime(void)
2371 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2372 GetVersionEx(&OsVersionInfo);
2373 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2374 return(NTElapsedTime());
2375 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2376 &kernel_time.filetime,&user_time.filetime);
2379 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2387 % N T W a r n i n g H a n d l e r %
2391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2393 % NTWarningHandler() displays a warning reason.
2395 % The format of the NTWarningHandler method is:
2397 % void NTWarningHandler(const ExceptionType severity,const char *reason,
2398 % const char *description)
2400 % A description of each parameter follows:
2402 % o severity: Specifies the numeric warning category.
2404 % o reason: Specifies the reason to display before terminating the
2407 % o description: Specifies any description to the reason.
2410 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2411 const char *reason,const char *description)
2414 buffer[2*MaxTextExtent];
2417 if (reason == (char *) NULL)
2419 if (description == (char *) NULL)
2420 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2423 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2424 GetClientName(),reason,description);
2425 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2426 MB_SETFOREGROUND | MB_ICONINFORMATION);
2430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2434 % N T W i n d o w s G e n e s i s %
2438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2440 % NTWindowsGenesis() initializes the MagickCore Windows environment.
2442 % The format of the NTWindowsGenesis method is:
2444 % void NTWindowsGenesis(void)
2447 MagickPrivate void NTWindowsGenesis(void)
2452 mode=GetEnvironmentValue("MAGICK_ERRORMODE");
2453 if (mode != (char *) NULL)
2455 (void) SetErrorMode(StringToInteger(mode));
2456 mode=DestroyString(mode);
2458 #if defined(_DEBUG) && !defined(__BORLANDC__) && !defined(__MINGW32__) && !defined(__MINGW64__)
2459 if (IsEventLogging() != MagickFalse)
2464 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2465 debug|=_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF;
2466 (void) _CrtSetDbgFlag(debug);
2467 _ASSERTE(_CrtCheckMemory());