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/utility.h"
56 #include "MagickCore/version.h"
57 #if defined(MAGICKCORE_LTDL_DELEGATE)
60 #if defined(MAGICKCORE_CIPHER_SUPPORT)
68 #if !defined(MAP_FAILED)
69 #define MAP_FAILED ((void *) -1)
75 #if !defined(MAGICKCORE_LTDL_DELEGATE)
77 *lt_slsearchpath = (char *) NULL;
84 *ghost_handle = (void *) NULL;
94 const registry_roots[2] =
96 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
97 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
101 External declarations.
103 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
104 extern "C" BOOL WINAPI
105 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % DllMain() is an entry point to the DLL which is called when processes and
120 % threads are initialized and terminated, or upon calls to the Windows
121 % LoadLibrary and FreeLibrary functions.
123 % The function returns TRUE of it succeeds, or FALSE if initialization fails.
125 % The format of the DllMain method is:
127 % BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
129 % A description of each parameter follows:
131 % o handle: handle to the DLL module
133 % o reason: reason for calling function:
135 % DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
136 % space of current process.
137 % DLL_THREAD_ATTACH - Indicates that the current process is
138 % creating a new thread. Called under the
139 % context of the new thread.
140 % DLL_THREAD_DETACH - Indicates that the thread is exiting.
141 % Called under the context of the exiting
143 % DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
144 % from the virtual address space of the
147 % o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
148 % and DLL_PROCESS_DETACH.
151 #if defined(_DLL) && defined( ProvideDllMain )
152 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
156 case DLL_PROCESS_ATTACH:
164 module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
165 sizeof(*module_path));
166 if (module_path == (char *) NULL)
168 count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
174 for ( ; count > 0; count--)
175 if (module_path[count] == '\\')
177 module_path[count+1]='\0';
180 MagickCoreGenesis(module_path,MagickFalse);
181 path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
182 if (path == (char *) NULL)
184 module_path=DestroyString(module_path);
187 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
188 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
190 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
195 variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
197 if (variable == (char *) NULL)
199 path=DestroyString(path);
200 module_path=DestroyString(module_path);
203 (void) FormatLocaleString(variable,16*MaxTextExtent,
204 "%s;%s",module_path,path);
205 SetEnvironmentVariable("PATH",variable);
206 variable=DestroyString(variable);
209 path=DestroyString(path);
211 module_path=DestroyString(module_path);
214 case DLL_PROCESS_DETACH:
216 MagickCoreTerminus();
227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % Exit() calls TerminateProcess for Win95.
239 % The format of the exit method is:
241 % int Exit(int status)
243 % A description of each parameter follows:
245 % o status: an integer value representing the status of the terminating
249 MagickPrivate int Exit(int status)
253 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
259 #if !defined(__MINGW32__) && !defined(__MINGW64__)
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % g e t t i m e o f d a y %
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % The gettimeofday() method get the time of day.
273 % The format of the gettimeofday method is:
275 % int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
277 % A description of each parameter follows:
279 % o time_value: the time value.
281 % o time_zone: the time zone.
284 MagickPrivate int gettimeofday (struct timeval *time_value,
285 struct timezone *time_zone)
287 #define EpochFiletime MagickLLConstant(116444736000000000)
292 if (time_value != (struct timeval *) NULL)
303 GetSystemTimeAsFileTime(&file_time);
304 date_time.LowPart=file_time.dwLowDateTime;
305 date_time.HighPart=file_time.dwHighDateTime;
306 time=date_time.QuadPart;
309 time_value->tv_sec=(ssize_t) (time / 1000000);
310 time_value->tv_usec=(ssize_t) (time % 1000000);
312 if (time_zone != (struct timezone *) NULL)
319 time_zone->tz_minuteswest=_timezone/60;
320 time_zone->tz_dsttime=_daylight;
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 % I s W i n d o w s 9 5 %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % IsWindows95() returns true if the system is Windows 95.
339 % The format of the IsWindows95 method is:
344 MagickPrivate int IsWindows95()
349 version_info.dwOSVersionInfoSize=sizeof(version_info);
350 if (GetVersionEx(&version_info) &&
351 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 % N T A r g v T o U T F 8 %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
368 % compatibility with Linux.
370 % The format of the NTArgvToUTF8 method is:
372 % char **NTArgvToUTF8(const int argc,wchar_t **argv)
374 % A description of each parameter follows:
376 % o argc: the number of command line arguments.
378 % o argv: the wide-character command line arguments.
381 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
389 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
390 if (utf8 == (char **) NULL)
391 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
392 for (i=0; i < (ssize_t) argc; i++)
397 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
400 utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
401 if (utf8[i] == (char *) NULL)
403 for (i--; i >= 0; i--)
404 utf8[i]=DestroyString(utf8[i]);
405 utf8=(char **) RelinquishMagickMemory(utf8);
406 ThrowFatalException(ResourceLimitFatalError,
407 "UnableToConvertStringToARGV");
409 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 % N T C l o s e D i r e c t o r y %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % NTCloseDirectory() closes the named directory stream and frees the DIR
429 % The format of the NTCloseDirectory method is:
431 % int NTCloseDirectory(DIR *entry)
433 % A description of each parameter follows:
435 % o entry: Specifies a pointer to a DIR structure.
438 MagickPrivate int NTCloseDirectory(DIR *entry)
440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
441 assert(entry != (DIR *) NULL);
442 FindClose(entry->hSearch);
443 entry=(DIR *) RelinquishMagickMemory(entry);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % N T C l o s e L i b r a r y %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % NTCloseLibrary() unloads the module associated with the passed handle.
460 % The format of the NTCloseLibrary method is:
462 % void NTCloseLibrary(void *handle)
464 % A description of each parameter follows:
466 % o handle: Specifies a handle to a previously loaded dynamic module.
469 MagickPrivate int NTCloseLibrary(void *handle)
472 return(FreeLibrary((HINSTANCE) handle));
473 return(!(FreeLibrary((HINSTANCE) handle)));
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % N T C o n t r o l H a n d l e r %
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 % NTControlHandler() registers a control handler that is activated when, for
488 % example, a ctrl-c is received.
490 % The format of the NTControlHandler method is:
492 % int NTControlHandler(void)
496 static BOOL ControlHandler(DWORD type)
499 AsynchronousResourceComponentTerminus();
503 MagickPrivate int NTControlHandler(void)
505 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 % N T E l a p s e d T i m e %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % NTElapsedTime() returns the elapsed time (in seconds) since the last call to
522 % The format of the ElapsedTime method is:
524 % double NTElapsedTime(void)
527 MagickPrivate double NTElapsedTime(void)
541 GetSystemTime(&system_time);
542 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
543 return((double) 1.0e-7*elapsed_time.filetime64);
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 + N T E r r o r H a n d l e r %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % NTErrorHandler() displays an error reason and then terminates the program.
559 % The format of the NTErrorHandler method is:
561 % void NTErrorHandler(const ExceptionType severity,const char *reason,
562 % const char *description)
564 % A description of each parameter follows:
566 % o severity: Specifies the numeric error category.
568 % o reason: Specifies the reason to display before terminating the
571 % o description: Specifies any description to the reason.
574 MagickPrivate void NTErrorHandler(const ExceptionType severity,
575 const char *reason,const char *description)
578 buffer[3*MaxTextExtent],
582 if (reason == (char *) NULL)
584 MagickCoreTerminus();
587 message=GetExceptionMessage(errno);
588 if ((description != (char *) NULL) && errno)
589 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
590 GetClientName(),reason,description,message);
592 if (description != (char *) NULL)
593 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
594 GetClientName(),reason,description);
597 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
598 GetClientName(),reason,message);
600 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
601 GetClientName(),reason);
602 message=DestroyString(message);
603 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
604 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
605 MagickCoreTerminus();
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % N T E x i t L i b r a r y %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % NTExitLibrary() exits the dynamic module loading subsystem.
622 % The format of the NTExitLibrary method is:
624 % int NTExitLibrary(void)
627 MagickPrivate int NTExitLibrary(void)
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % N T G a t h e r R a n d o m D a t a %
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % NTGatherRandomData() gathers random data and returns it.
645 % The format of the GatherRandomData method is:
647 % MagickBooleanType NTGatherRandomData(const size_t length,
648 % unsigned char *random)
650 % A description of each parameter follows:
652 % length: the length of random data buffer
654 % random: the random data is returned here.
657 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
658 unsigned char *random)
660 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
667 handle=(HCRYPTPROV) NULL;
668 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
669 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
671 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
672 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
675 status=CryptGenRandom(handle,(DWORD) length,random);
678 status=CryptReleaseContext(handle,0);
681 status=CryptReleaseContext(handle,0);
692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 % N T G e t E x e c u t i o n P a t h %
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % NTGetExecutionPath() returns the execution path of a program.
704 % The format of the GetExecutionPath method is:
706 % MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
708 % A description of each parameter follows:
710 % o path: the pathname of the executable that started the process.
712 % o extent: the maximum extent of the path.
715 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
718 GetModuleFileName(0,path,(DWORD) extent);
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % N T G e t L a s t E r r o r %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % NTGetLastError() returns the last error that occurred.
735 % The format of the NTGetLastError method is:
737 % char *NTGetLastError(void)
740 char *NTGetLastError(void)
751 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
752 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
753 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
755 reason=AcquireString("An unknown error occurred");
758 reason=AcquireString((const char *) buffer);
765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % N T G e t L i b r a r y E r r o r %
773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 % Lt_dlerror() returns a pointer to a string describing the last error
776 % associated with a lt_dl method. Note that this function is not thread
777 % safe so it should only be used under the protection of a lock.
779 % The format of the NTGetLibraryError method is:
781 % const char *NTGetLibraryError(void)
784 MagickPrivate const char *NTGetLibraryError(void)
787 last_error[MaxTextExtent];
793 error=NTGetLastError();
795 (void) CopyMagickString(last_error,error,MaxTextExtent);
796 error=DestroyString(error);
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
805 % N T G e t L i b r a r y S y m b o l %
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % NTGetLibrarySymbol() retrieve the procedure address of the method
812 % specified by the passed character string.
814 % The format of the NTGetLibrarySymbol method is:
816 % void *NTGetLibrarySymbol(void *handle,const char *name)
818 % A description of each parameter follows:
820 % o handle: Specifies a handle to the previously loaded dynamic module.
822 % o name: Specifies the procedure entry point to be returned.
825 void *NTGetLibrarySymbol(void *handle,const char *name)
830 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
832 return((void *) NULL);
833 return((void *) lpfnDllFunc1);
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 % N T G e t M o d u l e P a t h %
845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 % NTGetModulePath() returns the path of the specified module.
849 % The format of the GetModulePath method is:
851 % MagickBooleanType NTGetModulePath(const char *module,char *path)
853 % A description of each parameter follows:
855 % modith: the module name.
857 % path: the module path is returned here.
860 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
863 module_path[MaxTextExtent];
872 handle=GetModuleHandle(module);
873 if (handle == (HMODULE) NULL)
875 length=GetModuleFileName(handle,module_path,MaxTextExtent);
877 GetPathComponent(module_path,HeadPath,path);
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % N T G h o s t s c r i p t D L L %
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 % NTGhostscriptDLL() returns the path to the most recent Ghostscript version
893 % DLL. The method returns TRUE on success otherwise FALSE.
895 % The format of the NTGhostscriptDLL method is:
897 % int NTGhostscriptDLL(char *path,int length)
899 % A description of each parameter follows:
901 % o path: return the Ghostscript DLL path here.
903 % o length: the buffer length.
907 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
908 char *value,int *length)
925 Get a registry value: key = root\\key, named value = name.
927 if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
928 return(1); /* no match */
932 if (p == (BYTE *) NULL)
933 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
934 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
936 if (status == ERROR_SUCCESS)
939 return(0); /* return the match */
941 if (status == ERROR_MORE_DATA)
944 return(-1); /* buffer not large enough */
946 return(1); /* not found */
949 static int NTLocateGhostscript(DWORD flags,int *root_index,
950 const char **product_family,int *major_version,int *minor_version)
964 "Aladdin Ghostscript"
968 Find the most recent version of Ghostscript.
972 *product_family=NULL;
974 *minor_version=49; /* min version of Ghostscript is 5.50 */
975 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
989 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
990 for (j=0; j < 2; j++)
992 mode=KEY_READ | flags;
993 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1003 Now enumerate the keys.
1005 extent=sizeof(key)/sizeof(char);
1006 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1014 if (sscanf(key,"%d.%d",&major,&minor) != 2)
1016 if ((major > *major_version) || ((major == *major_version) &&
1017 (minor > *minor_version)))
1020 *product_family=products[i];
1021 *major_version=major;
1022 *minor_version=minor;
1026 (void) RegCloseKey(hkey);
1030 if (status == MagickFalse)
1035 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1036 "version %d.%02d",*product_family,*major_version,*minor_version);
1040 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,
1041 char *value,const size_t length)
1050 *product_family=(const char *) NULL;
1053 is_64_bit_version=FALSE;
1062 Get a string from the installed Ghostscript.
1065 if (product_family == NULL)
1068 #if defined(KEY_WOW64_32KEY)
1070 flags=KEY_WOW64_64KEY;
1072 flags=KEY_WOW64_32KEY;
1074 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1075 &major_version,&minor_version);
1076 if (product_family == NULL)
1078 flags=KEY_WOW64_32KEY;
1080 is_64_bit_version=TRUE;
1082 flags=KEY_WOW64_64KEY;
1086 if (product_family == NULL)
1088 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1089 &major_version,&minor_version);
1091 if (product_family == NULL)
1093 if (is_64_bit != NULL)
1094 *is_64_bit=is_64_bit_version;
1095 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1096 product_family,major_version,minor_version);
1097 extent=(int) length;
1098 if (NTGetRegistryValue(registry_roots[root_index].hkey,key,flags,name,value,
1101 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1102 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,key,
1109 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1112 dll[MaxTextExtent] = { "" };
1118 if ((*dll == '\0') &&
1119 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1123 if (!is_64_bit_version)
1126 if (is_64_bit_version)
1129 (void) CopyMagickString(path,dll,length);
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1138 % N T G h o s t s c r i p t D L L V e c t o r s %
1142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 % NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1145 % function vectors to invoke Ghostscript DLL functions. A null pointer is
1146 % returned if there is an error when loading the DLL or retrieving the
1149 % The format of the NTGhostscriptDLLVectors method is:
1151 % const GhostInfo *NTGhostscriptDLLVectors(void)
1154 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1156 if (NTGhostscriptLoadDLL() == FALSE)
1157 return((GhostInfo *) NULL);
1158 return(&ghost_info);
1162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166 % N T G h o s t s c r i p t E X E %
1170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 % NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1173 % The method returns FALSE if a full path value is not obtained and returns
1174 % a default path of gswin32c.exe.
1176 % The format of the NTGhostscriptEXE method is:
1178 % int NTGhostscriptEXE(char *path,int length)
1180 % A description of each parameter follows:
1182 % o path: return the Ghostscript executable path here.
1184 % o length: length of buffer.
1187 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1193 program[MaxTextExtent] = { "" };
1196 is_64_bit_version = FALSE;
1198 (void) CopyMagickString(path,"gswin32c.exe",length);
1199 if ((*program == '\0') &&
1200 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1202 p=strrchr(program,'\\');
1203 if (p != (char *) NULL)
1207 (void) ConcatenateMagickString(program,is_64_bit_version ?
1208 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1210 (void) CopyMagickString(path,program,length);
1215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1219 % N T G h o s t s c r i p t F o n t s %
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225 % NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1226 % returns FALSE if it cannot determine the font path.
1228 % The format of the NTGhostscriptFonts method is:
1230 % int NTGhostscriptFonts(char *path, int length)
1232 % A description of each parameter follows:
1234 % o path: return the font path here.
1236 % o length: length of the path buffer.
1239 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1242 buffer[MaxTextExtent],
1243 filename[MaxTextExtent];
1250 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1252 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1254 (void) CopyMagickString(path,p+1,length+1);
1255 q=strchr(path,DirectoryListSeparator);
1256 if (q != (char *) NULL)
1258 (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1259 DirectorySeparator);
1260 if (IsPathAccessible(filename) != MagickFalse)
1263 (void) FormatLocaleString(filename,MaxTextExtent,"c:%sgs%sfonts%sfonts.dir",
1264 DirectorySeparator,DirectorySeparator,DirectorySeparator);
1265 if (IsPathAccessible(filename) != MagickFalse)
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 % N T G h o s t s c r i p t L o a d D L L %
1279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1281 % NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1282 % TRUE if it succeeds.
1284 % The format of the NTGhostscriptLoadDLL method is:
1286 % int NTGhostscriptLoadDLL(void)
1289 MagickPrivate int NTGhostscriptLoadDLL(void)
1292 path[MaxTextExtent];
1294 if (ghost_handle != (void *) NULL)
1296 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1298 ghost_handle=lt_dlopen(path);
1299 if (ghost_handle == (void *) NULL)
1301 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1302 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1303 lt_dlsym(ghost_handle,"gsapi_exit");
1304 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1305 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1306 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1307 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1308 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1309 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1310 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1311 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1312 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1313 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1314 (ghost_info.delete_instance == NULL))
1320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1324 % N T G h o s t s c r i p t U n L o a d D L L %
1328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330 % NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1333 % The format of the NTGhostscriptUnLoadDLL method is:
1335 % int NTGhostscriptUnLoadDLL(void)
1338 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1343 if (ghost_handle == (void *) NULL)
1345 status=lt_dlclose(ghost_handle);
1346 ghost_handle=(void *) NULL;
1347 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1356 % N T I n i t i a l i z e L i b r a r y %
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362 % NTInitializeLibrary() initializes the dynamic module loading subsystem.
1364 % The format of the NTInitializeLibrary method is:
1366 % int NTInitializeLibrary(void)
1369 MagickPrivate int NTInitializeLibrary(void)
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 + N T M a p M e m o r y %
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 % Mmap() emulates the Unix method of the same name.
1387 % The format of the NTMapMemory method is:
1389 % MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1390 % int access,int file,MagickOffsetType offset)
1393 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1394 int flags,int file,MagickOffsetType offset)
1413 file_handle=INVALID_HANDLE_VALUE;
1414 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1415 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1416 map_handle=INVALID_HANDLE_VALUE;
1418 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1419 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1421 if (protection & PROT_WRITE)
1423 access_mode=FILE_MAP_WRITE;
1424 if (!(flags & MAP_PRIVATE))
1425 protection_mode=PAGE_READWRITE;
1428 access_mode=FILE_MAP_COPY;
1429 protection_mode=PAGE_WRITECOPY;
1433 if (protection & PROT_READ)
1435 access_mode=FILE_MAP_READ;
1436 protection_mode=PAGE_READONLY;
1438 if ((file == -1) && (flags & MAP_ANONYMOUS))
1439 file_handle=INVALID_HANDLE_VALUE;
1441 file_handle=(HANDLE) _get_osfhandle(file);
1442 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1446 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1448 CloseHandle(map_handle);
1450 if (map == (void *) NULL)
1451 return((void *) ((char *) MAP_FAILED));
1452 return((void *) ((char *) map));
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460 % N T O p e n D i r e c t o r y %
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 % NTOpenDirectory() opens the directory named by filename and associates a
1467 % directory stream with it.
1469 % The format of the NTOpenDirectory method is:
1471 % DIR *NTOpenDirectory(const char *path)
1473 % A description of each parameter follows:
1475 % o entry: Specifies a pointer to a DIR structure.
1478 MagickPrivate DIR *NTOpenDirectory(const char *path)
1481 file_specification[MaxTextExtent];
1489 assert(path != (const char *) NULL);
1490 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1493 return((DIR *) NULL);
1494 if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1495 MaxTextExtent) == (wchar_t*)NULL)
1496 return((DIR *) NULL);
1497 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1498 if (entry != (DIR *) NULL)
1500 entry->firsttime=TRUE;
1501 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1503 if (entry->hSearch == INVALID_HANDLE_VALUE)
1505 if(wcsncat(file_specification,L"*.*",
1506 MaxTextExtent) == (wchar_t*)NULL)
1508 entry=(DIR *) RelinquishMagickMemory(entry);
1509 return((DIR *) NULL);
1511 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1512 if (entry->hSearch == INVALID_HANDLE_VALUE)
1514 entry=(DIR *) RelinquishMagickMemory(entry);
1515 return((DIR *) NULL);
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1526 % N T O p e n L i b r a r y %
1530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532 % NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1533 % can be used to access the various procedures in the module.
1535 % The format of the NTOpenLibrary method is:
1537 % void *NTOpenLibrary(const char *filename)
1539 % A description of each parameter follows:
1541 % o path: Specifies a pointer to string representing dynamic module that
1546 static inline const char *GetSearchPath(void)
1548 #if defined(MAGICKCORE_LTDL_DELEGATE)
1549 return(lt_dlgetsearchpath());
1551 return(lt_slsearchpath);
1555 static UINT ChangeErrorMode(void)
1558 (CALLBACK *GETERRORMODE)(void);
1569 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1571 handle=GetModuleHandle("kernel32.dll");
1572 if (handle == (HMODULE) NULL)
1573 return SetErrorMode(mode);
1575 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1576 if (getErrorMode != (GETERRORMODE) NULL)
1577 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1579 return SetErrorMode(mode);
1582 MagickPrivate void *NTOpenLibrary(const char *filename)
1584 #define MaxPathElements 31
1587 buffer[MaxTextExtent];
1605 mode=ChangeErrorMode();
1606 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1607 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1612 p=(char *) GetSearchPath();
1614 while (index < MaxPathElements)
1616 q=strchr(p,DirectoryListSeparator);
1617 if (q == (char *) NULL)
1619 (void) CopyMagickString(buffer,p,MaxTextExtent);
1620 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1621 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1622 handle=(void *) LoadLibraryEx(buffer,NULL,
1623 LOAD_WITH_ALTERED_SEARCH_PATH);
1627 (void) CopyMagickString(buffer,p,i+1);
1628 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1629 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1630 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1631 if (handle != (void *) NULL)
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % N T R e a d D i r e c t o r y %
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650 % NTReadDirectory() returns a pointer to a structure representing the
1651 % directory entry at the current position in the directory stream to which
1654 % The format of the NTReadDirectory
1656 % NTReadDirectory(entry)
1658 % A description of each parameter follows:
1660 % o entry: Specifies a pointer to a DIR structure.
1663 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1671 if (entry == (DIR *) NULL)
1672 return((struct dirent *) NULL);
1673 if (!entry->firsttime)
1675 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1677 return((struct dirent *) NULL);
1679 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1680 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1682 return((struct dirent *) NULL);
1683 entry->firsttime=FALSE;
1684 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1685 return(&entry->file_info);
1689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1693 % N T R e g i s t r y K e y L o o k u p %
1697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1699 % NTRegistryKeyLookup() returns ImageMagick installation path settings
1700 % stored in the Windows Registry. Path settings are specific to the
1701 % installed ImageMagick version so that multiple Image Magick installations
1704 % Values are stored in the registry under a base path path similar to
1705 % "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1706 % "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1707 % is appended to this base path to form the full key.
1709 % The format of the NTRegistryKeyLookup method is:
1711 % unsigned char *NTRegistryKeyLookup(const char *subkey)
1713 % A description of each parameter follows:
1715 % o subkey: Specifies a string that identifies the registry object.
1716 % Currently supported sub-keys include: "BinPath", "ConfigurePath",
1717 % "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1720 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1723 package_key[MaxTextExtent];
1741 (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1742 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1743 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1744 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1745 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,®istry_key);
1746 if (status != ERROR_SUCCESS)
1747 status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1749 if (status != ERROR_SUCCESS)
1751 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1752 return((unsigned char *) NULL);
1758 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1759 if (value == (unsigned char *) NULL)
1761 RegCloseKey(registry_key);
1762 return((unsigned char *) NULL);
1764 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1765 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1766 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1768 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1769 if (value == (BYTE *) NULL)
1771 RegCloseKey(registry_key);
1772 return((unsigned char *) NULL);
1774 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1776 RegCloseKey(registry_key);
1777 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1778 value=(unsigned char *) RelinquishMagickMemory(value);
1779 return((unsigned char *) value);
1783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1787 % N T R e p o r t E v e n t %
1791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1793 % NTReportEvent() reports an event.
1795 % The format of the NTReportEvent method is:
1797 % MagickBooleanType NTReportEvent(const char *event,
1798 % const MagickBooleanType error)
1800 % A description of each parameter follows:
1802 % o event: the event.
1804 % o error: MagickTrue the event is an error.
1807 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1808 const MagickBooleanType error)
1819 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1821 return(MagickFalse);
1823 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1824 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1825 DeregisterEventSource(handle);
1830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834 % N T R e s o u r c e T o B l o b %
1838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840 % NTResourceToBlob() returns a blob containing the contents of the resource
1841 % in the current executable specified by the id parameter. This currently
1842 % used to retrieve MGK files tha have been embedded into the various command
1845 % The format of the NTResourceToBlob method is:
1847 % unsigned char *NTResourceToBlob(const char *id)
1849 % A description of each parameter follows:
1851 % o id: Specifies a string that identifies the resource.
1854 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1857 #ifndef MAGICKCORE_LIBRARY_NAME
1859 path[MaxTextExtent];
1878 assert(id != (const char *) NULL);
1879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1880 #ifdef MAGICKCORE_LIBRARY_NAME
1881 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1883 (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1884 DirectorySeparator,GetClientName());
1885 if (IsPathAccessible(path) != MagickFalse)
1886 handle=GetModuleHandle(path);
1888 handle=GetModuleHandle(0);
1891 return((unsigned char *) NULL);
1892 resource=FindResource(handle,id,"IMAGEMAGICK");
1894 return((unsigned char *) NULL);
1895 global=LoadResource(handle,resource);
1897 return((unsigned char *) NULL);
1898 length=SizeofResource(handle,resource);
1899 value=(unsigned char *) LockResource(global);
1902 FreeResource(global);
1903 return((unsigned char *) NULL);
1905 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1907 if (blob != (unsigned char *) NULL)
1909 (void) CopyMagickMemory(blob,value,length);
1912 UnlockResource(global);
1913 FreeResource(global);
1918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1922 % N T S e e k D i r e c t o r y %
1926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1928 % NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1929 % on the directory stream.
1931 % The format of the NTSeekDirectory method is:
1933 % void NTSeekDirectory(DIR *entry,ssize_t position)
1935 % A description of each parameter follows:
1937 % o entry: Specifies a pointer to a DIR structure.
1939 % o position: specifies the position associated with the directory
1943 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1946 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1947 assert(entry != (DIR *) NULL);
1951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955 % N T S e t S e a r c h P a t h %
1959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1961 % NTSetSearchPath() sets the current locations that the subsystem should
1962 % look at to find dynamically loadable modules.
1964 % The format of the NTSetSearchPath method is:
1966 % int NTSetSearchPath(const char *path)
1968 % A description of each parameter follows:
1970 % o path: Specifies a pointer to string representing the search path
1971 % for DLL's that can be dynamically loaded.
1974 MagickPrivate int NTSetSearchPath(const char *path)
1976 #if defined(MAGICKCORE_LTDL_DELEGATE)
1977 lt_dlsetsearchpath(path);
1979 if (lt_slsearchpath != (char *) NULL)
1980 lt_slsearchpath=DestroyString(lt_slsearchpath);
1981 if (path != (char *) NULL)
1982 lt_slsearchpath=AcquireString(path);
1988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1992 + N T S y n c M e m o r y %
1996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1998 % NTSyncMemory() emulates the Unix method of the same name.
2000 % The format of the NTSyncMemory method is:
2002 % int NTSyncMemory(void *address,size_t length,int flags)
2004 % A description of each parameter follows:
2006 % o address: the address of the binary large object.
2008 % o length: the length of the binary large object.
2010 % o flags: Option flags (ignored for Windows).
2013 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2016 if (FlushViewOfFile(address,length) == MagickFalse)
2022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2026 % N T S y s t e m C o m m a n d %
2030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032 % NTSystemCommand() executes the specified command and waits until it
2033 % terminates. The returned value is the exit status of the command.
2035 % The format of the NTSystemCommand method is:
2037 % int NTSystemCommand(MagickFalse,const char *command)
2039 % A description of each parameter follows:
2041 % o command: This string is the command to execute.
2044 MagickPrivate int NTSystemCommand(const char *command)
2047 local_command[MaxTextExtent];
2064 if (command == (char *) NULL)
2066 GetStartupInfo(&startup_info);
2067 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2068 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2069 (void) CopyMagickString(local_command,command,MaxTextExtent);
2070 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
2072 if (background_process != MagickFalse)
2073 local_command[strlen(command)-1]='\0';
2074 if (command[strlen(command)-1] == '|')
2075 local_command[strlen(command)-1]='\0';
2077 startup_info.wShowWindow=SW_HIDE;
2078 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2079 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2080 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2084 if (background_process != MagickFalse)
2085 return(status == 0);
2086 status=WaitForSingleObject(process_info.hProcess,INFINITE);
2087 if (status != WAIT_OBJECT_0)
2089 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2092 CloseHandle(process_info.hProcess);
2093 CloseHandle(process_info.hThread);
2094 return((int) child_status);
2098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2102 % N T S y s t e m C o n i f i g u r a t i o n %
2106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2108 % NTSystemConfiguration() provides a way for the application to determine
2109 % values for system limits or options at runtime.
2111 % The format of the exit method is:
2113 % ssize_t NTSystemConfiguration(int name)
2115 % A description of each parameter follows:
2117 % o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2120 MagickPrivate ssize_t NTSystemConfiguration(int name)
2129 GetSystemInfo(&system_info);
2130 return(system_info.dwPageSize);
2132 case _SC_PHYS_PAGES:
2146 handle=GetModuleHandle("kernel32.dll");
2147 if (handle == (HMODULE) NULL)
2149 GetSystemInfo(&system_info);
2150 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2151 if (module == (LPFNDLLFUNC2) NULL)
2156 GlobalMemoryStatus(&status);
2157 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2159 status.dwLength=sizeof(status);
2160 if (module(&status) == 0)
2162 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2177 % N T T e l l D i r e c t o r y %
2181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2183 % NTTellDirectory() returns the current location associated with the named
2186 % The format of the NTTellDirectory method is:
2188 % ssize_t NTTellDirectory(DIR *entry)
2190 % A description of each parameter follows:
2192 % o entry: Specifies a pointer to a DIR structure.
2195 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2197 assert(entry != (DIR *) NULL);
2202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2206 % N T T r u n c a t e F i l e %
2210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212 % NTTruncateFile() truncates a file to a specified length.
2214 % The format of the NTTruncateFile method is:
2216 % int NTTruncateFile(int file,off_t length)
2218 % A description of each parameter follows:
2222 % o length: the file length.
2225 MagickPrivate int NTTruncateFile(int file,off_t length)
2237 file_handle=(HANDLE) _get_osfhandle(file);
2238 if (file_handle == (HANDLE) -1L)
2240 low=(long) (length & 0xffffffffUL);
2241 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2242 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2243 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2245 if (SetEndOfFile(file_handle) == 0)
2251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2255 + N T U n m a p M e m o r y %
2259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2261 % NTUnmapMemory() emulates the Unix munmap method.
2263 % The format of the NTUnmapMemory method is:
2265 % int NTUnmapMemory(void *map,size_t length)
2267 % A description of each parameter follows:
2269 % o map: the address of the binary large object.
2271 % o length: the length of the binary large object.
2274 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2277 if (UnmapViewOfFile(map) == 0)
2283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2287 % N T U s e r T i m e %
2291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293 % NTUserTime() returns the total time the process has been scheduled (e.g.
2294 % seconds) since the last call to StartTimer().
2296 % The format of the UserTime method is:
2298 % double NTUserTime(void)
2301 MagickPrivate double NTUserTime(void)
2331 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2332 GetVersionEx(&OsVersionInfo);
2333 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2334 return(NTElapsedTime());
2335 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2336 &kernel_time.filetime,&user_time.filetime);
2339 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2347 % N T W a r n i n g H a n d l e r %
2351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2353 % NTWarningHandler() displays a warning reason.
2355 % The format of the NTWarningHandler method is:
2357 % void NTWarningHandler(const ExceptionType severity,const char *reason,
2358 % const char *description)
2360 % A description of each parameter follows:
2362 % o severity: Specifies the numeric warning category.
2364 % o reason: Specifies the reason to display before terminating the
2367 % o description: Specifies any description to the reason.
2370 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2371 const char *reason,const char *description)
2374 buffer[2*MaxTextExtent];
2377 if (reason == (char *) NULL)
2379 if (description == (char *) NULL)
2380 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2383 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2384 GetClientName(),reason,description);
2385 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2386 MB_SETFOREGROUND | MB_ICONINFORMATION);