2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Windows NT Utility Methods for MagickCore %
20 % Copyright 1999-2013 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/utility.h"
56 #include "MagickCore/version.h"
57 #if defined(MAGICKCORE_LTDL_DELEGATE)
60 #include "MagickCore/nt-base-private.h"
61 #if defined(MAGICKCORE_CIPHER_SUPPORT)
69 #if !defined(MAP_FAILED)
70 #define MAP_FAILED ((void *) -1)
76 #if !defined(MAGICKCORE_LTDL_DELEGATE)
78 *lt_slsearchpath = (char *) NULL;
85 *ghost_handle = (void *) NULL;
88 External declarations.
90 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
91 extern "C" BOOL WINAPI
92 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % DllMain() is an entry point to the DLL which is called when processes and
107 % threads are initialized and terminated, or upon calls to the Windows
108 % LoadLibrary and FreeLibrary functions.
110 % The function returns TRUE of it succeeds, or FALSE if initialization fails.
112 % The format of the DllMain method is:
114 % BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
116 % A description of each parameter follows:
118 % o handle: handle to the DLL module
120 % o reason: reason for calling function:
122 % DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
123 % space of current process.
124 % DLL_THREAD_ATTACH - Indicates that the current process is
125 % creating a new thread. Called under the
126 % context of the new thread.
127 % DLL_THREAD_DETACH - Indicates that the thread is exiting.
128 % Called under the context of the exiting
130 % DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
131 % from the virtual address space of the
134 % o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
135 % and DLL_PROCESS_DETACH.
138 #if defined(_DLL) && defined( ProvideDllMain )
139 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
143 case DLL_PROCESS_ATTACH:
151 module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
152 sizeof(*module_path));
153 if (module_path == (char *) NULL)
155 count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
161 for ( ; count > 0; count--)
162 if (module_path[count] == '\\')
164 module_path[count+1]='\0';
167 MagickCoreGenesis(module_path,MagickFalse);
168 path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
169 if (path == (char *) NULL)
171 module_path=DestroyString(module_path);
174 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
175 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
177 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
182 variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
184 if (variable == (char *) NULL)
186 path=DestroyString(path);
187 module_path=DestroyString(module_path);
190 (void) FormatLocaleString(variable,16*MaxTextExtent,
191 "%s;%s",module_path,path);
192 SetEnvironmentVariable("PATH",variable);
193 variable=DestroyString(variable);
196 path=DestroyString(path);
198 module_path=DestroyString(module_path);
201 case DLL_PROCESS_DETACH:
203 MagickCoreTerminus();
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % Exit() calls TerminateProcess for Win95.
226 % The format of the exit method is:
228 % int Exit(int status)
230 % A description of each parameter follows:
232 % o status: an integer value representing the status of the terminating
236 MagickPrivate int Exit(int status)
239 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
244 #if !defined(__MINGW32__) && !defined(__MINGW64__)
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % g e t t i m e o f d a y %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % The gettimeofday() method get the time of day.
258 % The format of the gettimeofday method is:
260 % int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
262 % A description of each parameter follows:
264 % o time_value: the time value.
266 % o time_zone: the time zone.
269 MagickPrivate int gettimeofday (struct timeval *time_value,
270 struct timezone *time_zone)
272 #define EpochFiletime MagickLLConstant(116444736000000000)
277 if (time_value != (struct timeval *) NULL)
288 GetSystemTimeAsFileTime(&file_time);
289 date_time.LowPart=file_time.dwLowDateTime;
290 date_time.HighPart=file_time.dwHighDateTime;
291 time=date_time.QuadPart;
294 time_value->tv_sec=(ssize_t) (time / 1000000);
295 time_value->tv_usec=(ssize_t) (time % 1000000);
297 if (time_zone != (struct timezone *) NULL)
304 time_zone->tz_minuteswest=_timezone/60;
305 time_zone->tz_dsttime=_daylight;
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % I s W i n d o w s 9 5 %
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % IsWindows95() returns true if the system is Windows 95.
324 % The format of the IsWindows95 method is:
329 MagickPrivate int IsWindows95()
334 version_info.dwOSVersionInfoSize=sizeof(version_info);
335 if (GetVersionEx(&version_info) &&
336 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % N T A r g v T o U T F 8 %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
353 % compatibility with Linux.
355 % The format of the NTArgvToUTF8 method is:
357 % char **NTArgvToUTF8(const int argc,wchar_t **argv)
359 % A description of each parameter follows:
361 % o argc: the number of command line arguments.
363 % o argv: the wide-character command line arguments.
366 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
374 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
375 if (utf8 == (char **) NULL)
376 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
377 for (i=0; i < (ssize_t) argc; i++)
382 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
385 utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
386 if (utf8[i] == (char *) NULL)
388 for (i--; i >= 0; i--)
389 utf8[i]=DestroyString(utf8[i]);
390 utf8=(char **) RelinquishMagickMemory(utf8);
391 ThrowFatalException(ResourceLimitFatalError,
392 "UnableToConvertStringToARGV");
394 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % N T C l o s e D i r e c t o r y %
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % NTCloseDirectory() closes the named directory stream and frees the DIR
414 % The format of the NTCloseDirectory method is:
416 % int NTCloseDirectory(DIR *entry)
418 % A description of each parameter follows:
420 % o entry: Specifies a pointer to a DIR structure.
423 MagickPrivate int NTCloseDirectory(DIR *entry)
425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
426 assert(entry != (DIR *) NULL);
427 FindClose(entry->hSearch);
428 entry=(DIR *) RelinquishMagickMemory(entry);
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % N T C l o s e L i b r a r y %
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443 % NTCloseLibrary() unloads the module associated with the passed handle.
445 % The format of the NTCloseLibrary method is:
447 % void NTCloseLibrary(void *handle)
449 % A description of each parameter follows:
451 % o handle: Specifies a handle to a previously loaded dynamic module.
454 MagickPrivate int NTCloseLibrary(void *handle)
457 return(FreeLibrary((HINSTANCE) handle));
458 return(!(FreeLibrary((HINSTANCE) handle)));
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % N T C o n t r o l H a n d l e r %
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % NTControlHandler() registers a control handler that is activated when, for
473 % example, a ctrl-c is received.
475 % The format of the NTControlHandler method is:
477 % int NTControlHandler(void)
481 static BOOL ControlHandler(DWORD type)
484 AsynchronousResourceComponentTerminus();
488 MagickPrivate int NTControlHandler(void)
490 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % N T E l a p s e d T i m e %
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % NTElapsedTime() returns the elapsed time (in seconds) since the last call to
507 % The format of the ElapsedTime method is:
509 % double NTElapsedTime(void)
512 MagickPrivate double NTElapsedTime(void)
526 GetSystemTime(&system_time);
527 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
528 return((double) 1.0e-7*elapsed_time.filetime64);
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 + N T E r r o r H a n d l e r %
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 % NTErrorHandler() displays an error reason and then terminates the program.
544 % The format of the NTErrorHandler method is:
546 % void NTErrorHandler(const ExceptionType severity,const char *reason,
547 % const char *description)
549 % A description of each parameter follows:
551 % o severity: Specifies the numeric error category.
553 % o reason: Specifies the reason to display before terminating the
556 % o description: Specifies any description to the reason.
559 MagickPrivate void NTErrorHandler(const ExceptionType severity,
560 const char *reason,const char *description)
563 buffer[3*MaxTextExtent],
567 if (reason == (char *) NULL)
569 MagickCoreTerminus();
572 message=GetExceptionMessage(errno);
573 if ((description != (char *) NULL) && errno)
574 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
575 GetClientName(),reason,description,message);
577 if (description != (char *) NULL)
578 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
579 GetClientName(),reason,description);
582 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
583 GetClientName(),reason,message);
585 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
586 GetClientName(),reason);
587 message=DestroyString(message);
588 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
589 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
590 MagickCoreTerminus();
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599 % N T E x i t L i b r a r y %
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 % NTExitLibrary() exits the dynamic module loading subsystem.
607 % The format of the NTExitLibrary method is:
609 % int NTExitLibrary(void)
612 MagickPrivate int NTExitLibrary(void)
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 % N T G a t h e r R a n d o m D a t a %
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 % NTGatherRandomData() gathers random data and returns it.
630 % The format of the GatherRandomData method is:
632 % MagickBooleanType NTGatherRandomData(const size_t length,
633 % unsigned char *random)
635 % A description of each parameter follows:
637 % length: the length of random data buffer
639 % random: the random data is returned here.
642 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
643 unsigned char *random)
645 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
652 handle=(HCRYPTPROV) NULL;
653 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
654 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
656 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
657 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
660 status=CryptGenRandom(handle,(DWORD) length,random);
663 status=CryptReleaseContext(handle,0);
666 status=CryptReleaseContext(handle,0);
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % N T G e t E x e c u t i o n P a t h %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % NTGetExecutionPath() returns the execution path of a program.
689 % The format of the GetExecutionPath method is:
691 % MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
693 % A description of each parameter follows:
695 % o path: the pathname of the executable that started the process.
697 % o extent: the maximum extent of the path.
700 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
703 GetModuleFileName(0,path,(DWORD) extent);
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712 % N T G e t L a s t E r r o r %
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 % NTGetLastError() returns the last error that occurred.
720 % The format of the NTGetLastError method is:
722 % char *NTGetLastError(void)
725 char *NTGetLastError(void)
736 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
737 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
738 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
740 reason=AcquireString("An unknown error occurred");
743 reason=AcquireString((const char *) buffer);
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % N T G e t L i b r a r y E r r o r %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % Lt_dlerror() returns a pointer to a string describing the last error
761 % associated with a lt_dl method. Note that this function is not thread
762 % safe so it should only be used under the protection of a lock.
764 % The format of the NTGetLibraryError method is:
766 % const char *NTGetLibraryError(void)
769 MagickPrivate const char *NTGetLibraryError(void)
772 last_error[MaxTextExtent];
778 error=NTGetLastError();
780 (void) CopyMagickString(last_error,error,MaxTextExtent);
781 error=DestroyString(error);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % N T G e t L i b r a r y S y m b o l %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % NTGetLibrarySymbol() retrieve the procedure address of the method
797 % specified by the passed character string.
799 % The format of the NTGetLibrarySymbol method is:
801 % void *NTGetLibrarySymbol(void *handle,const char *name)
803 % A description of each parameter follows:
805 % o handle: Specifies a handle to the previously loaded dynamic module.
807 % o name: Specifies the procedure entry point to be returned.
810 void *NTGetLibrarySymbol(void *handle,const char *name)
815 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
817 return((void *) NULL);
818 return((void *) lpfnDllFunc1);
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % N T G e t M o d u l e P a t h %
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 % NTGetModulePath() returns the path of the specified module.
834 % The format of the GetModulePath method is:
836 % MagickBooleanType NTGetModulePath(const char *module,char *path)
838 % A description of each parameter follows:
840 % modith: the module name.
842 % path: the module path is returned here.
845 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
848 module_path[MaxTextExtent];
857 handle=GetModuleHandle(module);
858 if (handle == (HMODULE) NULL)
860 length=GetModuleFileName(handle,module_path,MaxTextExtent);
862 GetPathComponent(module_path,HeadPath,path);
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 % N T G h o s t s c r i p t D L L %
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 % NTGhostscriptDLL() returns the path to the most recent Ghostscript version
878 % DLL. The method returns TRUE on success otherwise FALSE.
880 % The format of the NTGhostscriptDLL method is:
882 % int NTGhostscriptDLL(char *path,int length)
884 % A description of each parameter follows:
886 % o path: return the Ghostscript DLL path here.
888 % o length: the buffer length.
892 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
893 char *value,int *length)
910 Get a registry value: key = root\\key, named value = name.
912 if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
913 return(1); /* no match */
917 if (p == (BYTE *) NULL)
918 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
919 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
921 if (status == ERROR_SUCCESS)
924 return(0); /* return the match */
926 if (status == ERROR_MORE_DATA)
929 return(-1); /* buffer not large enough */
931 return(1); /* not found */
934 static int NTLocateGhostscript(DWORD flags,const char **product_family,int *major_version,
949 "Aladdin Ghostscript"
953 Find the most recent version of Ghostscript.
956 *product_family=NULL;
958 *minor_version=49; /* min version of Ghostscript is 5.50 */
959 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
971 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
972 root=HKEY_LOCAL_MACHINE;
973 mode=KEY_READ | flags;
974 if (RegOpenKeyExA(root,key,0,mode,&hkey) == ERROR_SUCCESS)
983 Now enumerate the keys.
985 extent=sizeof(key)/sizeof(char);
986 for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
994 if (sscanf(key,"%d.%d",&major,&minor) != 2)
996 if ((major > *major_version) || ((major == *major_version) &&
997 (minor > *minor_version)))
999 *product_family=products[i];
1000 *major_version=major;
1001 *minor_version=minor;
1005 (void) RegCloseKey(hkey);
1008 if (status == MagickFalse)
1013 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1014 "version %d.%02d",*product_family,*major_version,*minor_version);
1018 static BOOL NTIs64BitPlatform()
1020 #if defined(_WIN64) || !defined(KEY_WOW64_32KEY)
1024 return(IsWow64Process(GetCurrentProcess(), &is64) && is64);
1028 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,char *value,
1029 const size_t length)
1039 *product_family = (const char *) NULL;
1042 is_64_bit_version = FALSE;
1059 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
1060 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
1064 Get a string from the installed Ghostscript.
1066 if (is_64_bit!=NULL)
1069 if (product_family == NULL)
1072 #if defined(KEY_WOW64_32KEY)
1073 flags=NTIs64BitPlatform() ? KEY_WOW64_64KEY : 0;
1075 (void) NTLocateGhostscript(flags,&product_family,&major_version,
1077 if (product_family == NULL)
1082 We are running on a 64 bit platform - check for a 32 bit Ghostscript.
1085 #if defined(KEY_WOW64_32KEY)
1086 flags=KEY_WOW64_32KEY;
1088 (void) NTLocateGhostscript(flags,&product_family,&major_version,
1093 is_64_bit_version=NTIs64BitPlatform();
1095 if (is_64_bit != NULL)
1096 *is_64_bit=is_64_bit_version;
1097 if (product_family == NULL)
1099 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1100 product_family,major_version,minor_version);
1101 for (i=0; i < (ssize_t) (sizeof(hkeys)/sizeof(hkeys[0])); i++)
1103 extent=(int) length;
1104 if (NTGetRegistryValue(hkeys[i].hkey,key,flags,name,value,&extent) == 0)
1106 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1107 "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
1110 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1111 "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
1116 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1119 dll[MaxTextExtent] = { "" };
1125 if ((*dll == '\0') &&
1126 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1130 if (!is_64_bit_version)
1133 if (is_64_bit_version)
1136 (void) CopyMagickString(path,dll,length);
1141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145 % N T G h o s t s c r i p t D L L V e c t o r s %
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1151 % NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1152 % function vectors to invoke Ghostscript DLL functions. A null pointer is
1153 % returned if there is an error when loading the DLL or retrieving the
1156 % The format of the NTGhostscriptDLLVectors method is:
1158 % const GhostInfo *NTGhostscriptDLLVectors(void)
1161 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1163 if (NTGhostscriptLoadDLL() == FALSE)
1164 return((GhostInfo *) NULL);
1165 return(&ghost_info);
1169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1173 % N T G h o s t s c r i p t E X E %
1177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179 % NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1180 % The method returns FALSE if a full path value is not obtained and returns
1181 % a default path of gswin32c.exe.
1183 % The format of the NTGhostscriptEXE method is:
1185 % int NTGhostscriptEXE(char *path,int length)
1187 % A description of each parameter follows:
1189 % o path: return the Ghostscript executable path here.
1191 % o length: length of buffer.
1194 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1200 program[MaxTextExtent] = { "" };
1203 is_64_bit_version = FALSE;
1205 (void) CopyMagickString(path,"gswin32c.exe",length);
1206 if ((*program == '\0') &&
1207 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1209 p=strrchr(program,'\\');
1210 if (p != (char *) NULL)
1214 (void) ConcatenateMagickString(program,is_64_bit_version ?
1215 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1217 (void) CopyMagickString(path,program,length);
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 % N T G h o s t s c r i p t F o n t s %
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1233 % returns FALSE if it cannot determine the font path.
1235 % The format of the NTGhostscriptFonts method is:
1237 % int NTGhostscriptFonts(char *path, int length)
1239 % A description of each parameter follows:
1241 % o path: return the font path here.
1243 % o length: length of the path buffer.
1246 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1249 buffer[MaxTextExtent],
1250 filename[MaxTextExtent];
1257 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1259 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1261 (void) CopyMagickString(path,p+1,length+1);
1262 q=strchr(path,DirectoryListSeparator);
1263 if (q != (char *) NULL)
1265 (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1266 DirectorySeparator);
1267 if (IsPathAccessible(filename) != MagickFalse)
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1278 % N T G h o s t s c r i p t L o a d D L L %
1282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284 % NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1285 % TRUE if it succeeds.
1287 % The format of the NTGhostscriptLoadDLL method is:
1289 % int NTGhostscriptLoadDLL(void)
1292 MagickPrivate int NTGhostscriptLoadDLL(void)
1295 path[MaxTextExtent];
1297 if (ghost_handle != (void *) NULL)
1299 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1301 ghost_handle=lt_dlopen(path);
1302 if (ghost_handle == (void *) NULL)
1304 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1305 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1306 lt_dlsym(ghost_handle,"gsapi_exit");
1307 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1308 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1309 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1310 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1311 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1312 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1313 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1314 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1315 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1316 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1317 (ghost_info.delete_instance == NULL))
1323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327 % N T G h o s t s c r i p t U n L o a d D L L %
1331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1333 % NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1336 % The format of the NTGhostscriptUnLoadDLL method is:
1338 % int NTGhostscriptUnLoadDLL(void)
1341 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1346 if (ghost_handle == (void *) NULL)
1348 status=lt_dlclose(ghost_handle);
1349 ghost_handle=(void *) NULL;
1350 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1359 % N T I n i t i a l i z e L i b r a r y %
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 % NTInitializeLibrary() initializes the dynamic module loading subsystem.
1367 % The format of the NTInitializeLibrary method is:
1369 % int NTInitializeLibrary(void)
1372 MagickPrivate int NTInitializeLibrary(void)
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 + N T M a p M e m o r y %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % Mmap() emulates the Unix method of the same name.
1390 % The format of the NTMapMemory method is:
1392 % MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1393 % int access,int file,MagickOffsetType offset)
1396 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1397 int flags,int file,MagickOffsetType offset)
1416 file_handle=INVALID_HANDLE_VALUE;
1417 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1418 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1419 map_handle=INVALID_HANDLE_VALUE;
1421 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1422 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1424 if (protection & PROT_WRITE)
1426 access_mode=FILE_MAP_WRITE;
1427 if (!(flags & MAP_PRIVATE))
1428 protection_mode=PAGE_READWRITE;
1431 access_mode=FILE_MAP_COPY;
1432 protection_mode=PAGE_WRITECOPY;
1436 if (protection & PROT_READ)
1438 access_mode=FILE_MAP_READ;
1439 protection_mode=PAGE_READONLY;
1441 if ((file == -1) && (flags & MAP_ANONYMOUS))
1442 file_handle=INVALID_HANDLE_VALUE;
1444 file_handle=(HANDLE) _get_osfhandle(file);
1445 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1449 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1451 CloseHandle(map_handle);
1453 if (map == (void *) NULL)
1454 return((void *) MAP_FAILED);
1455 return((void *) ((char *) map));
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % N T O p e n D i r e c t o r y %
1467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469 % NTOpenDirectory() opens the directory named by filename and associates a
1470 % directory stream with it.
1472 % The format of the NTOpenDirectory method is:
1474 % DIR *NTOpenDirectory(const char *path)
1476 % A description of each parameter follows:
1478 % o entry: Specifies a pointer to a DIR structure.
1481 MagickPrivate DIR *NTOpenDirectory(const char *path)
1484 file_specification[MaxTextExtent];
1492 assert(path != (const char *) NULL);
1493 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1496 return((DIR *) NULL);
1497 if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1498 MaxTextExtent) == (wchar_t*)NULL)
1499 return((DIR *) NULL);
1500 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1501 if (entry != (DIR *) NULL)
1503 entry->firsttime=TRUE;
1504 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1506 if (entry->hSearch == INVALID_HANDLE_VALUE)
1508 if(wcsncat(file_specification,L"*.*",
1509 MaxTextExtent) == (wchar_t*)NULL)
1511 entry=(DIR *) RelinquishMagickMemory(entry);
1512 return((DIR *) NULL);
1514 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1515 if (entry->hSearch == INVALID_HANDLE_VALUE)
1517 entry=(DIR *) RelinquishMagickMemory(entry);
1518 return((DIR *) NULL);
1525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1529 % N T O p e n L i b r a r y %
1533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 % NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1536 % can be used to access the various procedures in the module.
1538 % The format of the NTOpenLibrary method is:
1540 % void *NTOpenLibrary(const char *filename)
1542 % A description of each parameter follows:
1544 % o path: Specifies a pointer to string representing dynamic module that
1549 static inline const char *GetSearchPath(void)
1551 #if defined(MAGICKCORE_LTDL_DELEGATE)
1552 return(lt_dlgetsearchpath());
1554 return(lt_slsearchpath);
1558 static UINT ChangeErrorMode(void)
1561 (CALLBACK *GETERRORMODE)(void);
1572 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1574 handle=GetModuleHandle("kernel32.dll");
1575 if (handle == (HMODULE) NULL)
1576 return SetErrorMode(mode);
1578 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1579 if (getErrorMode != (GETERRORMODE) NULL)
1580 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1582 return SetErrorMode(mode);
1585 MagickPrivate void *NTOpenLibrary(const char *filename)
1587 #define MaxPathElements 31
1590 buffer[MaxTextExtent];
1608 mode=ChangeErrorMode();
1609 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1610 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1615 p=(char *) GetSearchPath();
1617 while (index < MaxPathElements)
1619 q=strchr(p,DirectoryListSeparator);
1620 if (q == (char *) NULL)
1622 (void) CopyMagickString(buffer,p,MaxTextExtent);
1623 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1624 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1625 handle=(void *) LoadLibraryEx(buffer,NULL,
1626 LOAD_WITH_ALTERED_SEARCH_PATH);
1630 (void) CopyMagickString(buffer,p,i+1);
1631 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1632 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1633 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1634 if (handle != (void *) NULL)
1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1647 % N T R e a d D i r e c t o r y %
1651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1653 % NTReadDirectory() returns a pointer to a structure representing the
1654 % directory entry at the current position in the directory stream to which
1657 % The format of the NTReadDirectory
1659 % NTReadDirectory(entry)
1661 % A description of each parameter follows:
1663 % o entry: Specifies a pointer to a DIR structure.
1666 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1674 if (entry == (DIR *) NULL)
1675 return((struct dirent *) NULL);
1676 if (!entry->firsttime)
1678 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1680 return((struct dirent *) NULL);
1682 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1683 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1685 return((struct dirent *) NULL);
1686 entry->firsttime=FALSE;
1687 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1688 return(&entry->file_info);
1692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1696 % N T R e g i s t r y K e y L o o k u p %
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 % NTRegistryKeyLookup() returns ImageMagick installation path settings
1703 % stored in the Windows Registry. Path settings are specific to the
1704 % installed ImageMagick version so that multiple Image Magick installations
1707 % Values are stored in the registry under a base path path similar to
1708 % "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1709 % "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1710 % is appended to this base path to form the full key.
1712 % The format of the NTRegistryKeyLookup method is:
1714 % unsigned char *NTRegistryKeyLookup(const char *subkey)
1716 % A description of each parameter follows:
1718 % o subkey: Specifies a string that identifies the registry object.
1719 % Currently supported sub-keys include: "BinPath", "ConfigurePath",
1720 % "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1723 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1726 package_key[MaxTextExtent];
1744 (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1745 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1746 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1747 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1748 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,®istry_key);
1749 if (status != ERROR_SUCCESS)
1750 status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1752 if (status != ERROR_SUCCESS)
1754 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1755 return((unsigned char *) NULL);
1761 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1762 if (value == (unsigned char *) NULL)
1764 RegCloseKey(registry_key);
1765 return((unsigned char *) NULL);
1767 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1768 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1769 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1771 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1772 if (value == (BYTE *) NULL)
1774 RegCloseKey(registry_key);
1775 return((unsigned char *) NULL);
1777 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1779 RegCloseKey(registry_key);
1780 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1781 value=(unsigned char *) RelinquishMagickMemory(value);
1782 return((unsigned char *) value);
1786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790 % N T R e p o r t E v e n t %
1794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1796 % NTReportEvent() reports an event.
1798 % The format of the NTReportEvent method is:
1800 % MagickBooleanType NTReportEvent(const char *event,
1801 % const MagickBooleanType error)
1803 % A description of each parameter follows:
1805 % o event: the event.
1807 % o error: MagickTrue the event is an error.
1810 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1811 const MagickBooleanType error)
1822 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1824 return(MagickFalse);
1826 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1827 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1828 DeregisterEventSource(handle);
1833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837 % N T R e s o u r c e T o B l o b %
1841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1843 % NTResourceToBlob() returns a blob containing the contents of the resource
1844 % in the current executable specified by the id parameter. This currently
1845 % used to retrieve MGK files tha have been embedded into the various command
1848 % The format of the NTResourceToBlob method is:
1850 % unsigned char *NTResourceToBlob(const char *id)
1852 % A description of each parameter follows:
1854 % o id: Specifies a string that identifies the resource.
1857 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1860 #ifndef MAGICKCORE_LIBRARY_NAME
1862 path[MaxTextExtent];
1881 assert(id != (const char *) NULL);
1882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1883 #ifdef MAGICKCORE_LIBRARY_NAME
1884 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1886 (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1887 DirectorySeparator,GetClientName());
1888 if (IsPathAccessible(path) != MagickFalse)
1889 handle=GetModuleHandle(path);
1891 handle=GetModuleHandle(0);
1894 return((unsigned char *) NULL);
1895 resource=FindResource(handle,id,"IMAGEMAGICK");
1897 return((unsigned char *) NULL);
1898 global=LoadResource(handle,resource);
1900 return((unsigned char *) NULL);
1901 length=SizeofResource(handle,resource);
1902 value=(unsigned char *) LockResource(global);
1905 FreeResource(global);
1906 return((unsigned char *) NULL);
1908 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1910 if (blob != (unsigned char *) NULL)
1912 (void) CopyMagickMemory(blob,value,length);
1915 UnlockResource(global);
1916 FreeResource(global);
1921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1925 % N T S e e k D i r e c t o r y %
1929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1931 % NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1932 % on the directory stream.
1934 % The format of the NTSeekDirectory method is:
1936 % void NTSeekDirectory(DIR *entry,ssize_t position)
1938 % A description of each parameter follows:
1940 % o entry: Specifies a pointer to a DIR structure.
1942 % o position: specifies the position associated with the directory
1946 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1950 assert(entry != (DIR *) NULL);
1954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1958 % N T S e t S e a r c h P a t h %
1962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1964 % NTSetSearchPath() sets the current locations that the subsystem should
1965 % look at to find dynamically loadable modules.
1967 % The format of the NTSetSearchPath method is:
1969 % int NTSetSearchPath(const char *path)
1971 % A description of each parameter follows:
1973 % o path: Specifies a pointer to string representing the search path
1974 % for DLL's that can be dynamically loaded.
1977 MagickPrivate int NTSetSearchPath(const char *path)
1979 #if defined(MAGICKCORE_LTDL_DELEGATE)
1980 lt_dlsetsearchpath(path);
1982 if (lt_slsearchpath != (char *) NULL)
1983 lt_slsearchpath=DestroyString(lt_slsearchpath);
1984 if (path != (char *) NULL)
1985 lt_slsearchpath=AcquireString(path);
1991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995 + N T S y n c M e m o r y %
1999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001 % NTSyncMemory() emulates the Unix method of the same name.
2003 % The format of the NTSyncMemory method is:
2005 % int NTSyncMemory(void *address,size_t length,int flags)
2007 % A description of each parameter follows:
2009 % o address: the address of the binary large object.
2011 % o length: the length of the binary large object.
2013 % o flags: Option flags (ignored for Windows).
2016 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2019 if (FlushViewOfFile(address,length) == MagickFalse)
2025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029 % N T S y s t e m C o m m a n d %
2033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2035 % NTSystemCommand() executes the specified command and waits until it
2036 % terminates. The returned value is the exit status of the command.
2038 % The format of the NTSystemCommand method is:
2040 % int NTSystemCommand(MagickFalse,const char *command)
2042 % A description of each parameter follows:
2044 % o command: This string is the command to execute.
2047 MagickPrivate int NTSystemCommand(const char *command)
2050 local_command[MaxTextExtent];
2067 if (command == (char *) NULL)
2069 GetStartupInfo(&startup_info);
2070 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2071 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2072 (void) CopyMagickString(local_command,command,MaxTextExtent);
2073 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
2075 if (background_process != MagickFalse)
2076 local_command[strlen(command)-1]='\0';
2077 if (command[strlen(command)-1] == '|')
2078 local_command[strlen(command)-1]='\0';
2080 startup_info.wShowWindow=SW_SHOWDEFAULT;
2081 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2082 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2083 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2087 if (background_process != MagickFalse)
2088 return(status == 0);
2089 status=WaitForSingleObject(process_info.hProcess,INFINITE);
2090 if (status != WAIT_OBJECT_0)
2092 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2095 CloseHandle(process_info.hProcess);
2096 CloseHandle(process_info.hThread);
2097 return((int) child_status);
2101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105 % N T S y s t e m C o n i f i g u r a t i o n %
2109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2111 % NTSystemConfiguration() provides a way for the application to determine
2112 % values for system limits or options at runtime.
2114 % The format of the exit method is:
2116 % ssize_t NTSystemConfiguration(int name)
2118 % A description of each parameter follows:
2120 % o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2123 MagickPrivate ssize_t NTSystemConfiguration(int name)
2132 GetSystemInfo(&system_info);
2133 return(system_info.dwPageSize);
2135 case _SC_PHYS_PAGES:
2149 handle=GetModuleHandle("kernel32.dll");
2150 if (handle == (HMODULE) NULL)
2152 GetSystemInfo(&system_info);
2153 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2154 if (module == (LPFNDLLFUNC2) NULL)
2159 GlobalMemoryStatus(&status);
2160 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2162 status.dwLength=sizeof(status);
2163 if (module(&status) == 0)
2165 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2180 % N T T e l l D i r e c t o r y %
2184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2186 % NTTellDirectory() returns the current location associated with the named
2189 % The format of the NTTellDirectory method is:
2191 % ssize_t NTTellDirectory(DIR *entry)
2193 % A description of each parameter follows:
2195 % o entry: Specifies a pointer to a DIR structure.
2198 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2200 assert(entry != (DIR *) NULL);
2205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2209 % N T T r u n c a t e F i l e %
2213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2215 % NTTruncateFile() truncates a file to a specified length.
2217 % The format of the NTTruncateFile method is:
2219 % int NTTruncateFile(int file,off_t length)
2221 % A description of each parameter follows:
2225 % o length: the file length.
2228 MagickPrivate int NTTruncateFile(int file,off_t length)
2238 file_handle=_get_osfhandle(file);
2239 if (file_handle == -1L)
2241 low=(long) (length & 0xffffffffUL);
2242 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2243 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2244 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2246 if (SetEndOfFile((HANDLE) file_handle) == 0)
2252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2256 + N T U n m a p M e m o r y %
2260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262 % NTUnmapMemory() emulates the Unix munmap method.
2264 % The format of the NTUnmapMemory method is:
2266 % int NTUnmapMemory(void *map,size_t length)
2268 % A description of each parameter follows:
2270 % o map: the address of the binary large object.
2272 % o length: the length of the binary large object.
2275 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2278 if (UnmapViewOfFile(map) == 0)
2284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288 % N T U s e r T i m e %
2292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2294 % NTUserTime() returns the total time the process has been scheduled (e.g.
2295 % seconds) since the last call to StartTimer().
2297 % The format of the UserTime method is:
2299 % double NTUserTime(void)
2302 MagickPrivate double NTUserTime(void)
2332 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2333 GetVersionEx(&OsVersionInfo);
2334 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2335 return(NTElapsedTime());
2336 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2337 &kernel_time.filetime,&user_time.filetime);
2340 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2348 % N T W a r n i n g H a n d l e r %
2352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2354 % NTWarningHandler() displays a warning reason.
2356 % The format of the NTWarningHandler method is:
2358 % void NTWarningHandler(const ExceptionType severity,const char *reason,
2359 % const char *description)
2361 % A description of each parameter follows:
2363 % o severity: Specifies the numeric warning category.
2365 % o reason: Specifies the reason to display before terminating the
2368 % o description: Specifies any description to the reason.
2371 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2372 const char *reason,const char *description)
2375 buffer[2*MaxTextExtent];
2378 if (reason == (char *) NULL)
2380 if (description == (char *) NULL)
2381 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2384 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2385 GetClientName(),reason,description);
2386 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2387 MB_SETFOREGROUND | MB_ICONINFORMATION);