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);
110 static inline char *create_utf8_string(const wchar_t *wideChar)
118 count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,NULL,0,NULL,NULL);
120 return((char *) NULL);
121 utf8=(char *) AcquireQuantumMemory(count+1,sizeof(*utf8));
122 if (utf8 == (char *) NULL)
123 return((char *) NULL);
124 count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,utf8,count,NULL,NULL);
127 utf8=DestroyString(utf8);
128 return((char *) NULL);
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % DllMain() is an entry point to the DLL which is called when processes and
146 % threads are initialized and terminated, or upon calls to the Windows
147 % LoadLibrary and FreeLibrary functions.
149 % The function returns TRUE of it succeeds, or FALSE if initialization fails.
151 % The format of the DllMain method is:
153 % BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
155 % A description of each parameter follows:
157 % o handle: handle to the DLL module
159 % o reason: reason for calling function:
161 % DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
162 % space of current process.
163 % DLL_THREAD_ATTACH - Indicates that the current process is
164 % creating a new thread. Called under the
165 % context of the new thread.
166 % DLL_THREAD_DETACH - Indicates that the thread is exiting.
167 % Called under the context of the exiting
169 % DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
170 % from the virtual address space of the
173 % o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
174 % and DLL_PROCESS_DETACH.
177 #if defined(_DLL) && defined( ProvideDllMain )
178 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
182 case DLL_PROCESS_ATTACH:
193 wide_path=(wchar_t *) AcquireQuantumMemory(MaxTextExtent,
195 if (wide_path == (wchar_t *) NULL)
197 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MaxTextExtent);
203 module_path=create_utf8_string(wide_path);
204 for ( ; count > 0; count--)
205 if (module_path[count] == '\\')
207 module_path[count+1]='\0';
210 MagickCoreGenesis(module_path,MagickFalse);
211 path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
212 if (path == (char *) NULL)
214 module_path=DestroyString(module_path);
215 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
218 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
219 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
221 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
226 variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
228 if (variable == (char *) NULL)
230 path=DestroyString(path);
231 module_path=DestroyString(module_path);
232 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
235 (void) FormatLocaleString(variable,16*MaxTextExtent,
236 "%s;%s",module_path,path);
237 SetEnvironmentVariable("PATH",variable);
238 variable=DestroyString(variable);
241 path=DestroyString(path);
242 module_path=DestroyString(module_path);
244 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
247 case DLL_PROCESS_DETACH:
249 MagickCoreTerminus();
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 % Exit() calls TerminateProcess for Win95.
272 % The format of the exit method is:
274 % int Exit(int status)
276 % A description of each parameter follows:
278 % o status: an integer value representing the status of the terminating
282 MagickPrivate int Exit(int status)
286 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
292 #if !defined(__MINGW32__) && !defined(__MINGW64__)
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % g e t t i m e o f d a y %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % The gettimeofday() method get the time of day.
306 % The format of the gettimeofday method is:
308 % int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
310 % A description of each parameter follows:
312 % o time_value: the time value.
314 % o time_zone: the time zone.
317 MagickPrivate int gettimeofday (struct timeval *time_value,
318 struct timezone *time_zone)
320 #define EpochFiletime MagickLLConstant(116444736000000000)
325 if (time_value != (struct timeval *) NULL)
336 GetSystemTimeAsFileTime(&file_time);
337 date_time.LowPart=file_time.dwLowDateTime;
338 date_time.HighPart=file_time.dwHighDateTime;
339 time=date_time.QuadPart;
342 time_value->tv_sec=(ssize_t) (time / 1000000);
343 time_value->tv_usec=(ssize_t) (time % 1000000);
345 if (time_zone != (struct timezone *) NULL)
352 time_zone->tz_minuteswest=_timezone/60;
353 time_zone->tz_dsttime=_daylight;
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 % I s W i n d o w s 9 5 %
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % IsWindows95() returns true if the system is Windows 95.
372 % The format of the IsWindows95 method is:
377 MagickPrivate int IsWindows95()
382 version_info.dwOSVersionInfoSize=sizeof(version_info);
383 if (GetVersionEx(&version_info) &&
384 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % N T A r g v T o U T F 8 %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
401 % compatibility with Linux.
403 % The format of the NTArgvToUTF8 method is:
405 % char **NTArgvToUTF8(const int argc,wchar_t **argv)
407 % A description of each parameter follows:
409 % o argc: the number of command line arguments.
411 % o argv: the wide-character command line arguments.
414 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
422 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
423 if (utf8 == (char **) NULL)
424 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
425 for (i=0; i < (ssize_t) argc; i++)
427 utf8[i]=create_utf8_string(argv[i]);
428 if (utf8[i] == (char *) NULL)
430 for (i--; i >= 0; i--)
431 utf8[i]=DestroyString(utf8[i]);
432 ThrowFatalException(ResourceLimitFatalError,
433 "UnableToConvertStringToARGV");
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % N T C l o s e D i r e c t o r y %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450 % NTCloseDirectory() closes the named directory stream and frees the DIR
453 % The format of the NTCloseDirectory method is:
455 % int NTCloseDirectory(DIR *entry)
457 % A description of each parameter follows:
459 % o entry: Specifies a pointer to a DIR structure.
462 MagickPrivate int NTCloseDirectory(DIR *entry)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
465 assert(entry != (DIR *) NULL);
466 FindClose(entry->hSearch);
467 entry=(DIR *) RelinquishMagickMemory(entry);
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % N T C l o s e L i b r a r y %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % NTCloseLibrary() unloads the module associated with the passed handle.
484 % The format of the NTCloseLibrary method is:
486 % void NTCloseLibrary(void *handle)
488 % A description of each parameter follows:
490 % o handle: Specifies a handle to a previously loaded dynamic module.
493 MagickPrivate int NTCloseLibrary(void *handle)
496 return(FreeLibrary((HINSTANCE) handle));
497 return(!(FreeLibrary((HINSTANCE) handle)));
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % N T C o n t r o l H a n d l e r %
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511 % NTControlHandler() registers a control handler that is activated when, for
512 % example, a ctrl-c is received.
514 % The format of the NTControlHandler method is:
516 % int NTControlHandler(void)
520 static BOOL ControlHandler(DWORD type)
523 AsynchronousResourceComponentTerminus();
527 MagickPrivate int NTControlHandler(void)
529 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 % N T E l a p s e d T i m e %
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 % NTElapsedTime() returns the elapsed time (in seconds) since the last call to
546 % The format of the ElapsedTime method is:
548 % double NTElapsedTime(void)
551 MagickPrivate double NTElapsedTime(void)
565 GetSystemTime(&system_time);
566 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
567 return((double) 1.0e-7*elapsed_time.filetime64);
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 + N T E r r o r H a n d l e r %
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581 % NTErrorHandler() displays an error reason and then terminates the program.
583 % The format of the NTErrorHandler method is:
585 % void NTErrorHandler(const ExceptionType severity,const char *reason,
586 % const char *description)
588 % A description of each parameter follows:
590 % o severity: Specifies the numeric error category.
592 % o reason: Specifies the reason to display before terminating the
595 % o description: Specifies any description to the reason.
598 MagickPrivate void NTErrorHandler(const ExceptionType severity,
599 const char *reason,const char *description)
602 buffer[3*MaxTextExtent],
606 if (reason == (char *) NULL)
608 MagickCoreTerminus();
611 message=GetExceptionMessage(errno);
612 if ((description != (char *) NULL) && errno)
613 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
614 GetClientName(),reason,description,message);
616 if (description != (char *) NULL)
617 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
618 GetClientName(),reason,description);
621 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
622 GetClientName(),reason,message);
624 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
625 GetClientName(),reason);
626 message=DestroyString(message);
627 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
628 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
629 MagickCoreTerminus();
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % N T E x i t L i b r a r y %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % NTExitLibrary() exits the dynamic module loading subsystem.
646 % The format of the NTExitLibrary method is:
648 % int NTExitLibrary(void)
651 MagickPrivate int NTExitLibrary(void)
657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661 % N T G a t h e r R a n d o m D a t a %
665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 % NTGatherRandomData() gathers random data and returns it.
669 % The format of the GatherRandomData method is:
671 % MagickBooleanType NTGatherRandomData(const size_t length,
672 % unsigned char *random)
674 % A description of each parameter follows:
676 % length: the length of random data buffer
678 % random: the random data is returned here.
681 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
682 unsigned char *random)
684 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
691 handle=(HCRYPTPROV) NULL;
692 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
693 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
695 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
696 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
699 status=CryptGenRandom(handle,(DWORD) length,random);
702 status=CryptReleaseContext(handle,0);
705 status=CryptReleaseContext(handle,0);
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % N T G e t E x e c u t i o n P a t h %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % NTGetExecutionPath() returns the execution path of a program.
728 % The format of the GetExecutionPath method is:
730 % MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
732 % A description of each parameter follows:
734 % o path: the pathname of the executable that started the process.
736 % o extent: the maximum extent of the path.
739 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
743 wide_path[MaxTextExtent];
745 (void) GetModuleFileNameW(0,wide_path,(DWORD) extent);
746 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % N T G e t L a s t E r r o r %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % NTGetLastError() returns the last error that occurred.
764 % The format of the NTGetLastError method is:
766 % char *NTGetLastError(void)
769 char *NTGetLastError(void)
780 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
781 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
782 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
784 reason=AcquireString("An unknown error occurred");
787 reason=AcquireString((const char *) buffer);
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % N T G e t L i b r a r y E r r o r %
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % Lt_dlerror() returns a pointer to a string describing the last error
805 % associated with a lt_dl method. Note that this function is not thread
806 % safe so it should only be used under the protection of a lock.
808 % The format of the NTGetLibraryError method is:
810 % const char *NTGetLibraryError(void)
813 MagickPrivate const char *NTGetLibraryError(void)
816 last_error[MaxTextExtent];
822 error=NTGetLastError();
824 (void) CopyMagickString(last_error,error,MaxTextExtent);
825 error=DestroyString(error);
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % N T G e t L i b r a r y S y m b o l %
838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840 % NTGetLibrarySymbol() retrieve the procedure address of the method
841 % specified by the passed character string.
843 % The format of the NTGetLibrarySymbol method is:
845 % void *NTGetLibrarySymbol(void *handle,const char *name)
847 % A description of each parameter follows:
849 % o handle: Specifies a handle to the previously loaded dynamic module.
851 % o name: Specifies the procedure entry point to be returned.
854 void *NTGetLibrarySymbol(void *handle,const char *name)
859 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
861 return((void *) NULL);
862 return((void *) lpfnDllFunc1);
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % N T G e t M o d u l e P a t h %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 % NTGetModulePath() returns the path of the specified module.
878 % The format of the GetModulePath method is:
880 % MagickBooleanType NTGetModulePath(const char *module,char *path)
882 % A description of each parameter follows:
884 % modith: the module name.
886 % path: the module path is returned here.
889 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
892 module_path[MaxTextExtent];
901 handle=GetModuleHandle(module);
902 if (handle == (HMODULE) NULL)
904 length=GetModuleFileName(handle,module_path,MaxTextExtent);
906 GetPathComponent(module_path,HeadPath,path);
911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 % N T G h o s t s c r i p t D L L %
919 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % NTGhostscriptDLL() returns the path to the most recent Ghostscript version
922 % DLL. The method returns TRUE on success otherwise FALSE.
924 % The format of the NTGhostscriptDLL method is:
926 % int NTGhostscriptDLL(char *path,int length)
928 % A description of each parameter follows:
930 % o path: return the Ghostscript DLL path here.
932 % o length: the buffer length.
936 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
937 char *value,int *length)
954 Get a registry value: key = root\\key, named value = name.
956 if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
957 return(1); /* no match */
961 if (p == (BYTE *) NULL)
962 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
963 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
965 if (status == ERROR_SUCCESS)
968 return(0); /* return the match */
970 if (status == ERROR_MORE_DATA)
973 return(-1); /* buffer not large enough */
975 return(1); /* not found */
978 static int NTLocateGhostscript(DWORD flags,int *root_index,
979 const char **product_family,int *major_version,int *minor_version)
993 "Aladdin Ghostscript"
997 Find the most recent version of Ghostscript.
1001 *product_family=NULL;
1003 *minor_version=49; /* min version of Ghostscript is 5.50 */
1004 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
1018 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
1019 for (j=0; j < (ssize_t) (sizeof(registry_roots)/sizeof(registry_roots[0]));
1022 mode=KEY_READ | flags;
1023 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1033 Now enumerate the keys.
1035 extent=sizeof(key)/sizeof(char);
1036 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1044 if (sscanf(key,"%d.%d",&major,&minor) != 2)
1046 if ((major > *major_version) || ((major == *major_version) &&
1047 (minor > *minor_version)))
1050 *product_family=products[i];
1051 *major_version=major;
1052 *minor_version=minor;
1056 (void) RegCloseKey(hkey);
1060 if (status == MagickFalse)
1065 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1066 "version %d.%02d",*product_family,*major_version,*minor_version);
1070 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,
1071 char *value,const size_t length)
1074 buffer[MaxTextExtent],
1081 *product_family=(const char *) NULL;
1084 is_64_bit_version=FALSE;
1093 Get a string from the installed Ghostscript.
1096 directory=(char *) NULL;
1097 if (LocaleCompare(name, "GS_DLL") == 0)
1099 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_PATH");
1100 if (directory != (char *) NULL)
1102 (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll32.dll",
1103 directory,DirectorySeparator);
1104 if (IsPathAccessible(buffer) != MagickFalse)
1106 (void) CopyMagickString(value,buffer,length);
1107 if (is_64_bit != NULL)
1111 (void) FormatLocaleString(buffer,MaxTextExtent,"%s%sgsdll64.dll",
1112 directory,DirectorySeparator);
1113 if (IsPathAccessible(buffer) != MagickFalse)
1115 (void) CopyMagickString(value,buffer,length);
1116 if (is_64_bit != NULL)
1123 if (product_family == NULL)
1126 #if defined(KEY_WOW64_32KEY)
1128 flags=KEY_WOW64_64KEY;
1130 flags=KEY_WOW64_32KEY;
1132 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1133 &major_version,&minor_version);
1134 if (product_family == NULL)
1136 flags=KEY_WOW64_32KEY;
1138 is_64_bit_version=TRUE;
1140 flags=KEY_WOW64_64KEY;
1144 if (product_family == NULL)
1146 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1147 &major_version,&minor_version);
1148 #if !defined(_WIN64)
1149 is_64_bit_version=TRUE;
1152 if (product_family == NULL)
1154 if (is_64_bit != NULL)
1155 *is_64_bit=is_64_bit_version;
1156 (void) FormatLocaleString(buffer,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1157 product_family,major_version,minor_version);
1158 extent=(int) length;
1159 if (NTGetRegistryValue(registry_roots[root_index].hkey,buffer,flags,name,
1160 value,&extent) == 0)
1162 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1163 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1170 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1173 dll[MaxTextExtent] = { "" };
1179 if ((*dll == '\0') &&
1180 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1184 if (!is_64_bit_version)
1187 if (is_64_bit_version)
1190 (void) CopyMagickString(path,dll,length);
1195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1199 % N T G h o s t s c r i p t D L L V e c t o r s %
1203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1205 % NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1206 % function vectors to invoke Ghostscript DLL functions. A null pointer is
1207 % returned if there is an error when loading the DLL or retrieving the
1210 % The format of the NTGhostscriptDLLVectors method is:
1212 % const GhostInfo *NTGhostscriptDLLVectors(void)
1215 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1217 if (NTGhostscriptLoadDLL() == FALSE)
1218 return((GhostInfo *) NULL);
1219 return(&ghost_info);
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227 % N T G h o s t s c r i p t E X E %
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 % NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1234 % The method returns FALSE if a full path value is not obtained and returns
1235 % a default path of gswin32c.exe.
1237 % The format of the NTGhostscriptEXE method is:
1239 % int NTGhostscriptEXE(char *path,int length)
1241 % A description of each parameter follows:
1243 % o path: return the Ghostscript executable path here.
1245 % o length: length of buffer.
1248 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1254 program[MaxTextExtent] = { "" };
1257 is_64_bit_version = FALSE;
1259 (void) CopyMagickString(path,"gswin32c.exe",length);
1260 if ((*program == '\0') &&
1261 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1263 p=strrchr(program,'\\');
1264 if (p != (char *) NULL)
1268 (void) ConcatenateMagickString(program,is_64_bit_version ?
1269 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1271 (void) CopyMagickString(path,program,length);
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 % N T G h o s t s c r i p t F o n t s %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1287 % returns FALSE if it cannot determine the font path.
1289 % The format of the NTGhostscriptFonts method is:
1291 % int NTGhostscriptFonts(char *path, int length)
1293 % A description of each parameter follows:
1295 % o path: return the font path here.
1297 % o length: length of the path buffer.
1300 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1303 buffer[MaxTextExtent],
1305 filename[MaxTextExtent];
1312 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_FONT_PATH");
1313 if (directory != (char *) NULL)
1315 (void) CopyMagickString(buffer,directory,MaxTextExtent);
1316 directory=DestroyString(directory);
1320 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1323 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1325 (void) CopyMagickString(path,p+1,length+1);
1326 q=strchr(path,DirectoryListSeparator);
1327 if (q != (char *) NULL)
1329 (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1330 DirectorySeparator);
1331 if (IsPathAccessible(filename) != MagickFalse)
1339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1343 % N T G h o s t s c r i p t L o a d D L L %
1347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1349 % NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1350 % TRUE if it succeeds.
1352 % The format of the NTGhostscriptLoadDLL method is:
1354 % int NTGhostscriptLoadDLL(void)
1357 MagickPrivate int NTGhostscriptLoadDLL(void)
1360 path[MaxTextExtent];
1362 if (ghost_handle != (void *) NULL)
1364 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1366 ghost_handle=lt_dlopen(path);
1367 if (ghost_handle == (void *) NULL)
1369 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1370 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1371 lt_dlsym(ghost_handle,"gsapi_exit");
1372 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1373 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1374 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1375 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1376 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1377 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1378 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1379 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1380 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1381 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1382 (ghost_info.delete_instance == NULL))
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392 % N T G h o s t s c r i p t U n L o a d D L L %
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 % NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1401 % The format of the NTGhostscriptUnLoadDLL method is:
1403 % int NTGhostscriptUnLoadDLL(void)
1406 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1411 if (ghost_handle == (void *) NULL)
1413 status=lt_dlclose(ghost_handle);
1414 ghost_handle=(void *) NULL;
1415 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % N T I n i t i a l i z e L i b r a r y %
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % NTInitializeLibrary() initializes the dynamic module loading subsystem.
1432 % The format of the NTInitializeLibrary method is:
1434 % int NTInitializeLibrary(void)
1437 MagickPrivate int NTInitializeLibrary(void)
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 + N T M a p M e m o r y %
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % Mmap() emulates the Unix method of the same name.
1455 % The format of the NTMapMemory method is:
1457 % MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1458 % int access,int file,MagickOffsetType offset)
1461 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1462 int flags,int file,MagickOffsetType offset)
1481 file_handle=INVALID_HANDLE_VALUE;
1482 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1483 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1484 map_handle=INVALID_HANDLE_VALUE;
1486 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1487 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1489 if (protection & PROT_WRITE)
1491 access_mode=FILE_MAP_WRITE;
1492 if (!(flags & MAP_PRIVATE))
1493 protection_mode=PAGE_READWRITE;
1496 access_mode=FILE_MAP_COPY;
1497 protection_mode=PAGE_WRITECOPY;
1501 if (protection & PROT_READ)
1503 access_mode=FILE_MAP_READ;
1504 protection_mode=PAGE_READONLY;
1506 if ((file == -1) && (flags & MAP_ANONYMOUS))
1507 file_handle=INVALID_HANDLE_VALUE;
1509 file_handle=(HANDLE) _get_osfhandle(file);
1510 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1514 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1516 CloseHandle(map_handle);
1518 if (map == (void *) NULL)
1519 return((void *) ((char *) MAP_FAILED));
1520 return((void *) ((char *) map));
1524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 % N T O p e n D i r e c t o r y %
1532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1534 % NTOpenDirectory() opens the directory named by filename and associates a
1535 % directory stream with it.
1537 % The format of the NTOpenDirectory method is:
1539 % DIR *NTOpenDirectory(const char *path)
1541 % A description of each parameter follows:
1543 % o entry: Specifies a pointer to a DIR structure.
1546 MagickPrivate DIR *NTOpenDirectory(const char *path)
1555 file_specification[MaxTextExtent];
1557 assert(path != (const char *) NULL);
1558 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1561 return((DIR *) NULL);
1562 if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1563 MaxTextExtent-wcslen(file_specification)-1) == (wchar_t*)NULL)
1564 return((DIR *) NULL);
1565 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1566 if (entry != (DIR *) NULL)
1568 entry->firsttime=TRUE;
1569 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1571 if (entry->hSearch == INVALID_HANDLE_VALUE)
1573 if(wcsncat(file_specification,L"*.*",
1574 MaxTextExtent-wcslen(file_specification)-1) == (wchar_t*)NULL)
1576 entry=(DIR *) RelinquishMagickMemory(entry);
1577 return((DIR *) NULL);
1579 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1580 if (entry->hSearch == INVALID_HANDLE_VALUE)
1582 entry=(DIR *) RelinquishMagickMemory(entry);
1583 return((DIR *) NULL);
1590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1594 % N T O p e n L i b r a r y %
1598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 % NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1601 % can be used to access the various procedures in the module.
1603 % The format of the NTOpenLibrary method is:
1605 % void *NTOpenLibrary(const char *filename)
1607 % A description of each parameter follows:
1609 % o path: Specifies a pointer to string representing dynamic module that
1614 static inline const char *GetSearchPath(void)
1616 #if defined(MAGICKCORE_LTDL_DELEGATE)
1617 return(lt_dlgetsearchpath());
1619 return(lt_slsearchpath);
1623 static UINT ChangeErrorMode(void)
1626 (CALLBACK *GETERRORMODE)(void);
1637 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1639 handle=GetModuleHandle("kernel32.dll");
1640 if (handle == (HMODULE) NULL)
1641 return SetErrorMode(mode);
1643 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1644 if (getErrorMode != (GETERRORMODE) NULL)
1645 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1647 return SetErrorMode(mode);
1650 static inline void *NTLoadLibrary(const char *filename)
1656 path[MaxTextExtent];
1658 length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MaxTextExtent);
1660 return((void *) NULL);
1661 return (void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1664 MagickPrivate void *NTOpenLibrary(const char *filename)
1667 path[MaxTextExtent];
1679 mode=ChangeErrorMode();
1680 handle=NTLoadLibrary(filename);
1681 if (handle == (void *) NULL)
1684 while (p != (const char*) NULL)
1686 q=strchr(p,DirectoryListSeparator);
1687 if (q != (const char*) NULL)
1688 (void) CopyMagickString(path,p,q-p+1);
1690 (void) CopyMagickString(path,p,MaxTextExtent);
1691 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
1692 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
1693 handle=NTLoadLibrary(path);
1694 if (handle != (void *) NULL || q == (const char*) NULL)
1704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % N T R e a d D i r e c t o r y %
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714 % NTReadDirectory() returns a pointer to a structure representing the
1715 % directory entry at the current position in the directory stream to which
1718 % The format of the NTReadDirectory
1720 % NTReadDirectory(entry)
1722 % A description of each parameter follows:
1724 % o entry: Specifies a pointer to a DIR structure.
1727 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1735 if (entry == (DIR *) NULL)
1736 return((struct dirent *) NULL);
1737 if (!entry->firsttime)
1739 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1741 return((struct dirent *) NULL);
1743 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1744 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1746 return((struct dirent *) NULL);
1747 entry->firsttime=FALSE;
1748 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1749 return(&entry->file_info);
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 % N T R e g i s t r y K e y L o o k u p %
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763 % NTRegistryKeyLookup() returns ImageMagick installation path settings
1764 % stored in the Windows Registry. Path settings are specific to the
1765 % installed ImageMagick version so that multiple Image Magick installations
1768 % Values are stored in the registry under a base path path similar to
1769 % "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1770 % "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1771 % is appended to this base path to form the full key.
1773 % The format of the NTRegistryKeyLookup method is:
1775 % unsigned char *NTRegistryKeyLookup(const char *subkey)
1777 % A description of each parameter follows:
1779 % o subkey: Specifies a string that identifies the registry object.
1780 % Currently supported sub-keys include: "BinPath", "ConfigurePath",
1781 % "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1784 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1787 package_key[MaxTextExtent];
1805 (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1806 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1807 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1808 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1809 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,®istry_key);
1810 if (status != ERROR_SUCCESS)
1811 status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1813 if (status != ERROR_SUCCESS)
1815 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1816 return((unsigned char *) NULL);
1822 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1823 if (value == (unsigned char *) NULL)
1825 RegCloseKey(registry_key);
1826 return((unsigned char *) NULL);
1828 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1829 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1830 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1832 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1833 if (value == (BYTE *) NULL)
1835 RegCloseKey(registry_key);
1836 return((unsigned char *) NULL);
1838 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1840 RegCloseKey(registry_key);
1841 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1842 value=(unsigned char *) RelinquishMagickMemory(value);
1843 return((unsigned char *) value);
1847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1851 % N T R e p o r t E v e n t %
1855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1857 % NTReportEvent() reports an event.
1859 % The format of the NTReportEvent method is:
1861 % MagickBooleanType NTReportEvent(const char *event,
1862 % const MagickBooleanType error)
1864 % A description of each parameter follows:
1866 % o event: the event.
1868 % o error: MagickTrue the event is an error.
1871 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1872 const MagickBooleanType error)
1883 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1885 return(MagickFalse);
1887 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1888 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1889 DeregisterEventSource(handle);
1894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1898 % N T R e s o u r c e T o B l o b %
1902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1904 % NTResourceToBlob() returns a blob containing the contents of the resource
1905 % in the current executable specified by the id parameter. This currently
1906 % used to retrieve MGK files tha have been embedded into the various command
1909 % The format of the NTResourceToBlob method is:
1911 % unsigned char *NTResourceToBlob(const char *id)
1913 % A description of each parameter follows:
1915 % o id: Specifies a string that identifies the resource.
1918 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1921 #ifndef MAGICKCORE_LIBRARY_NAME
1923 path[MaxTextExtent];
1942 assert(id != (const char *) NULL);
1943 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1944 #ifdef MAGICKCORE_LIBRARY_NAME
1945 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1947 (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1948 DirectorySeparator,GetClientName());
1949 if (IsPathAccessible(path) != MagickFalse)
1950 handle=GetModuleHandle(path);
1952 handle=GetModuleHandle(0);
1955 return((unsigned char *) NULL);
1956 resource=FindResource(handle,id,"IMAGEMAGICK");
1958 return((unsigned char *) NULL);
1959 global=LoadResource(handle,resource);
1961 return((unsigned char *) NULL);
1962 length=SizeofResource(handle,resource);
1963 value=(unsigned char *) LockResource(global);
1966 FreeResource(global);
1967 return((unsigned char *) NULL);
1969 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1971 if (blob != (unsigned char *) NULL)
1973 (void) CopyMagickMemory(blob,value,length);
1976 UnlockResource(global);
1977 FreeResource(global);
1982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1986 % N T S e e k D i r e c t o r y %
1990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1992 % NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1993 % on the directory stream.
1995 % The format of the NTSeekDirectory method is:
1997 % void NTSeekDirectory(DIR *entry,ssize_t position)
1999 % A description of each parameter follows:
2001 % o entry: Specifies a pointer to a DIR structure.
2003 % o position: specifies the position associated with the directory
2007 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
2010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2011 assert(entry != (DIR *) NULL);
2015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2019 % N T S e t S e a r c h P a t h %
2023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2025 % NTSetSearchPath() sets the current locations that the subsystem should
2026 % look at to find dynamically loadable modules.
2028 % The format of the NTSetSearchPath method is:
2030 % int NTSetSearchPath(const char *path)
2032 % A description of each parameter follows:
2034 % o path: Specifies a pointer to string representing the search path
2035 % for DLL's that can be dynamically loaded.
2038 MagickPrivate int NTSetSearchPath(const char *path)
2040 #if defined(MAGICKCORE_LTDL_DELEGATE)
2041 lt_dlsetsearchpath(path);
2043 if (lt_slsearchpath != (char *) NULL)
2044 lt_slsearchpath=DestroyString(lt_slsearchpath);
2045 if (path != (char *) NULL)
2046 lt_slsearchpath=AcquireString(path);
2052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2056 + N T S y n c M e m o r y %
2060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2062 % NTSyncMemory() emulates the Unix method of the same name.
2064 % The format of the NTSyncMemory method is:
2066 % int NTSyncMemory(void *address,size_t length,int flags)
2068 % A description of each parameter follows:
2070 % o address: the address of the binary large object.
2072 % o length: the length of the binary large object.
2074 % o flags: Option flags (ignored for Windows).
2077 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2080 if (FlushViewOfFile(address,length) == MagickFalse)
2086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2090 % N T S y s t e m C o m m a n d %
2094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2096 % NTSystemCommand() executes the specified command and waits until it
2097 % terminates. The returned value is the exit status of the command.
2099 % The format of the NTSystemCommand method is:
2101 % int NTSystemCommand(MagickFalse,const char *command)
2103 % A description of each parameter follows:
2105 % o command: This string is the command to execute.
2108 MagickPrivate int NTSystemCommand(const char *command)
2111 local_command[MaxTextExtent];
2128 if (command == (char *) NULL)
2130 GetStartupInfo(&startup_info);
2131 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2132 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2133 (void) CopyMagickString(local_command,command,MaxTextExtent);
2134 asynchronous=command[strlen(command)-1] == '&' ? MagickTrue : MagickFalse;
2135 if (asynchronous != MagickFalse)
2137 local_command[strlen(command)-1]='\0';
2138 startup_info.wShowWindow=SW_SHOWDEFAULT;
2141 if (command[strlen(command)-1] == '|')
2142 local_command[strlen(command)-1]='\0';
2144 startup_info.wShowWindow=SW_HIDE;
2145 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2146 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2147 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2151 if (asynchronous != MagickFalse)
2152 return(status == 0);
2153 status=WaitForSingleObject(process_info.hProcess,INFINITE);
2154 if (status != WAIT_OBJECT_0)
2156 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2159 CloseHandle(process_info.hProcess);
2160 CloseHandle(process_info.hThread);
2161 return((int) child_status);
2165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2169 % N T S y s t e m C o n i f i g u r a t i o n %
2173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2175 % NTSystemConfiguration() provides a way for the application to determine
2176 % values for system limits or options at runtime.
2178 % The format of the exit method is:
2180 % ssize_t NTSystemConfiguration(int name)
2182 % A description of each parameter follows:
2184 % o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2187 MagickPrivate ssize_t NTSystemConfiguration(int name)
2196 GetSystemInfo(&system_info);
2197 return(system_info.dwPageSize);
2199 case _SC_PHYS_PAGES:
2213 handle=GetModuleHandle("kernel32.dll");
2214 if (handle == (HMODULE) NULL)
2216 GetSystemInfo(&system_info);
2217 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2218 if (module == (LPFNDLLFUNC2) NULL)
2223 GlobalMemoryStatus(&status);
2224 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2226 status.dwLength=sizeof(status);
2227 if (module(&status) == 0)
2229 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2244 % N T T e l l D i r e c t o r y %
2248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2250 % NTTellDirectory() returns the current location associated with the named
2253 % The format of the NTTellDirectory method is:
2255 % ssize_t NTTellDirectory(DIR *entry)
2257 % A description of each parameter follows:
2259 % o entry: Specifies a pointer to a DIR structure.
2262 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2264 assert(entry != (DIR *) NULL);
2269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2273 % N T T r u n c a t e F i l e %
2277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2279 % NTTruncateFile() truncates a file to a specified length.
2281 % The format of the NTTruncateFile method is:
2283 % int NTTruncateFile(int file,off_t length)
2285 % A description of each parameter follows:
2289 % o length: the file length.
2292 MagickPrivate int NTTruncateFile(int file,off_t length)
2304 file_handle=(HANDLE) _get_osfhandle(file);
2305 if (file_handle == (HANDLE) -1L)
2307 low=(long) (length & 0xffffffffUL);
2308 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2309 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2310 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2312 if (SetEndOfFile(file_handle) == 0)
2318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2322 + N T U n m a p M e m o r y %
2326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2328 % NTUnmapMemory() emulates the Unix munmap method.
2330 % The format of the NTUnmapMemory method is:
2332 % int NTUnmapMemory(void *map,size_t length)
2334 % A description of each parameter follows:
2336 % o map: the address of the binary large object.
2338 % o length: the length of the binary large object.
2341 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2344 if (UnmapViewOfFile(map) == 0)
2350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2354 % N T U s e r T i m e %
2358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2360 % NTUserTime() returns the total time the process has been scheduled (e.g.
2361 % seconds) since the last call to StartTimer().
2363 % The format of the UserTime method is:
2365 % double NTUserTime(void)
2368 MagickPrivate double NTUserTime(void)
2398 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2399 GetVersionEx(&OsVersionInfo);
2400 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2401 return(NTElapsedTime());
2402 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2403 &kernel_time.filetime,&user_time.filetime);
2406 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2414 % N T W a r n i n g H a n d l e r %
2418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2420 % NTWarningHandler() displays a warning reason.
2422 % The format of the NTWarningHandler method is:
2424 % void NTWarningHandler(const ExceptionType severity,const char *reason,
2425 % const char *description)
2427 % A description of each parameter follows:
2429 % o severity: Specifies the numeric warning category.
2431 % o reason: Specifies the reason to display before terminating the
2434 % o description: Specifies any description to the reason.
2437 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2438 const char *reason,const char *description)
2441 buffer[2*MaxTextExtent];
2444 if (reason == (char *) NULL)
2446 if (description == (char *) NULL)
2447 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2450 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2451 GetClientName(),reason,description);
2452 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2453 MB_SETFOREGROUND | MB_ICONINFORMATION);
2457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2461 % N T W i n d o w s G e n e s i s %
2465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2467 % NTWindowsGenesis() initializes the MagickCore Windows environment.
2469 % The format of the NTWindowsGenesis method is:
2471 % void NTWindowsGenesis(void)
2474 MagickPrivate void NTWindowsGenesis(void)
2479 mode=GetEnvironmentValue("MAGICK_ERRORMODE");
2480 if (mode != (char *) NULL)
2482 (void) SetErrorMode(StringToInteger(mode));
2483 mode=DestroyString(mode);
2485 #if defined(_DEBUG) && !defined(__BORLANDC__) && !defined(__MINGW32__) && !defined(__MINGW64__)
2486 if (IsEventLogging() != MagickFalse)
2491 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2492 debug|=_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF;
2493 (void) _CrtSetDbgFlag(debug);
2494 _ASSERTE(_CrtCheckMemory());