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 #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)
240 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
246 #if !defined(__MINGW32__) && !defined(__MINGW64__)
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % g e t t i m e o f d a y %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % The gettimeofday() method get the time of day.
260 % The format of the gettimeofday method is:
262 % int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
264 % A description of each parameter follows:
266 % o time_value: the time value.
268 % o time_zone: the time zone.
271 MagickPrivate int gettimeofday (struct timeval *time_value,
272 struct timezone *time_zone)
274 #define EpochFiletime MagickLLConstant(116444736000000000)
279 if (time_value != (struct timeval *) NULL)
290 GetSystemTimeAsFileTime(&file_time);
291 date_time.LowPart=file_time.dwLowDateTime;
292 date_time.HighPart=file_time.dwHighDateTime;
293 time=date_time.QuadPart;
296 time_value->tv_sec=(ssize_t) (time / 1000000);
297 time_value->tv_usec=(ssize_t) (time % 1000000);
299 if (time_zone != (struct timezone *) NULL)
306 time_zone->tz_minuteswest=_timezone/60;
307 time_zone->tz_dsttime=_daylight;
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % I s W i n d o w s 9 5 %
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 % IsWindows95() returns true if the system is Windows 95.
326 % The format of the IsWindows95 method is:
331 MagickPrivate int IsWindows95()
336 version_info.dwOSVersionInfoSize=sizeof(version_info);
337 if (GetVersionEx(&version_info) &&
338 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % N T A r g v T o U T F 8 %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
355 % compatibility with Linux.
357 % The format of the NTArgvToUTF8 method is:
359 % char **NTArgvToUTF8(const int argc,wchar_t **argv)
361 % A description of each parameter follows:
363 % o argc: the number of command line arguments.
365 % o argv: the wide-character command line arguments.
368 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
376 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
377 if (utf8 == (char **) NULL)
378 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
379 for (i=0; i < (ssize_t) argc; i++)
384 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
387 utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
388 if (utf8[i] == (char *) NULL)
390 for (i--; i >= 0; i--)
391 utf8[i]=DestroyString(utf8[i]);
392 utf8=(char **) RelinquishMagickMemory(utf8);
393 ThrowFatalException(ResourceLimitFatalError,
394 "UnableToConvertStringToARGV");
396 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % N T C l o s e D i r e c t o r y %
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 % NTCloseDirectory() closes the named directory stream and frees the DIR
416 % The format of the NTCloseDirectory method is:
418 % int NTCloseDirectory(DIR *entry)
420 % A description of each parameter follows:
422 % o entry: Specifies a pointer to a DIR structure.
425 MagickPrivate int NTCloseDirectory(DIR *entry)
427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
428 assert(entry != (DIR *) NULL);
429 FindClose(entry->hSearch);
430 entry=(DIR *) RelinquishMagickMemory(entry);
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 % N T C l o s e L i b r a r y %
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 % NTCloseLibrary() unloads the module associated with the passed handle.
447 % The format of the NTCloseLibrary method is:
449 % void NTCloseLibrary(void *handle)
451 % A description of each parameter follows:
453 % o handle: Specifies a handle to a previously loaded dynamic module.
456 MagickPrivate int NTCloseLibrary(void *handle)
459 return(FreeLibrary((HINSTANCE) handle));
460 return(!(FreeLibrary((HINSTANCE) handle)));
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % N T C o n t r o l H a n d l e r %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % NTControlHandler() registers a control handler that is activated when, for
475 % example, a ctrl-c is received.
477 % The format of the NTControlHandler method is:
479 % int NTControlHandler(void)
483 static BOOL ControlHandler(DWORD type)
486 AsynchronousResourceComponentTerminus();
490 MagickPrivate int NTControlHandler(void)
492 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % N T E l a p s e d T i m e %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % NTElapsedTime() returns the elapsed time (in seconds) since the last call to
509 % The format of the ElapsedTime method is:
511 % double NTElapsedTime(void)
514 MagickPrivate double NTElapsedTime(void)
528 GetSystemTime(&system_time);
529 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
530 return((double) 1.0e-7*elapsed_time.filetime64);
534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 + N T E r r o r H a n d l e r %
542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 % NTErrorHandler() displays an error reason and then terminates the program.
546 % The format of the NTErrorHandler method is:
548 % void NTErrorHandler(const ExceptionType severity,const char *reason,
549 % const char *description)
551 % A description of each parameter follows:
553 % o severity: Specifies the numeric error category.
555 % o reason: Specifies the reason to display before terminating the
558 % o description: Specifies any description to the reason.
561 MagickPrivate void NTErrorHandler(const ExceptionType severity,
562 const char *reason,const char *description)
565 buffer[3*MaxTextExtent],
569 if (reason == (char *) NULL)
571 MagickCoreTerminus();
574 message=GetExceptionMessage(errno);
575 if ((description != (char *) NULL) && errno)
576 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
577 GetClientName(),reason,description,message);
579 if (description != (char *) NULL)
580 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
581 GetClientName(),reason,description);
584 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
585 GetClientName(),reason,message);
587 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
588 GetClientName(),reason);
589 message=DestroyString(message);
590 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
591 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
592 MagickCoreTerminus();
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 % N T E x i t L i b r a r y %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % NTExitLibrary() exits the dynamic module loading subsystem.
609 % The format of the NTExitLibrary method is:
611 % int NTExitLibrary(void)
614 MagickPrivate int NTExitLibrary(void)
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % N T G a t h e r R a n d o m D a t a %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % NTGatherRandomData() gathers random data and returns it.
632 % The format of the GatherRandomData method is:
634 % MagickBooleanType NTGatherRandomData(const size_t length,
635 % unsigned char *random)
637 % A description of each parameter follows:
639 % length: the length of random data buffer
641 % random: the random data is returned here.
644 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
645 unsigned char *random)
647 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
654 handle=(HCRYPTPROV) NULL;
655 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
656 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
658 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
659 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
662 status=CryptGenRandom(handle,(DWORD) length,random);
665 status=CryptReleaseContext(handle,0);
668 status=CryptReleaseContext(handle,0);
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % N T G e t E x e c u t i o n P a t h %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 % NTGetExecutionPath() returns the execution path of a program.
691 % The format of the GetExecutionPath method is:
693 % MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
695 % A description of each parameter follows:
697 % o path: the pathname of the executable that started the process.
699 % o extent: the maximum extent of the path.
702 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
705 GetModuleFileName(0,path,(DWORD) extent);
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714 % N T G e t L a s t E r r o r %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % NTGetLastError() returns the last error that occurred.
722 % The format of the NTGetLastError method is:
724 % char *NTGetLastError(void)
727 char *NTGetLastError(void)
738 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
739 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
740 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
742 reason=AcquireString("An unknown error occurred");
745 reason=AcquireString((const char *) buffer);
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % N T G e t L i b r a r y E r r o r %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % Lt_dlerror() returns a pointer to a string describing the last error
763 % associated with a lt_dl method. Note that this function is not thread
764 % safe so it should only be used under the protection of a lock.
766 % The format of the NTGetLibraryError method is:
768 % const char *NTGetLibraryError(void)
771 MagickPrivate const char *NTGetLibraryError(void)
774 last_error[MaxTextExtent];
780 error=NTGetLastError();
782 (void) CopyMagickString(last_error,error,MaxTextExtent);
783 error=DestroyString(error);
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 % N T G e t L i b r a r y S y m b o l %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % NTGetLibrarySymbol() retrieve the procedure address of the method
799 % specified by the passed character string.
801 % The format of the NTGetLibrarySymbol method is:
803 % void *NTGetLibrarySymbol(void *handle,const char *name)
805 % A description of each parameter follows:
807 % o handle: Specifies a handle to the previously loaded dynamic module.
809 % o name: Specifies the procedure entry point to be returned.
812 void *NTGetLibrarySymbol(void *handle,const char *name)
817 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
819 return((void *) NULL);
820 return((void *) lpfnDllFunc1);
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 % N T G e t M o d u l e P a t h %
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % NTGetModulePath() returns the path of the specified module.
836 % The format of the GetModulePath method is:
838 % MagickBooleanType NTGetModulePath(const char *module,char *path)
840 % A description of each parameter follows:
842 % modith: the module name.
844 % path: the module path is returned here.
847 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
850 module_path[MaxTextExtent];
859 handle=GetModuleHandle(module);
860 if (handle == (HMODULE) NULL)
862 length=GetModuleFileName(handle,module_path,MaxTextExtent);
864 GetPathComponent(module_path,HeadPath,path);
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 % N T G h o s t s c r i p t D L L %
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 % NTGhostscriptDLL() returns the path to the most recent Ghostscript version
880 % DLL. The method returns TRUE on success otherwise FALSE.
882 % The format of the NTGhostscriptDLL method is:
884 % int NTGhostscriptDLL(char *path,int length)
886 % A description of each parameter follows:
888 % o path: return the Ghostscript DLL path here.
890 % o length: the buffer length.
894 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
895 char *value,int *length)
912 Get a registry value: key = root\\key, named value = name.
914 if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
915 return(1); /* no match */
919 if (p == (BYTE *) NULL)
920 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
921 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
923 if (status == ERROR_SUCCESS)
926 return(0); /* return the match */
928 if (status == ERROR_MORE_DATA)
931 return(-1); /* buffer not large enough */
933 return(1); /* not found */
936 static int NTLocateGhostscript(DWORD flags,const char **product_family,int *major_version,
951 "Aladdin Ghostscript"
955 Find the most recent version of Ghostscript.
958 *product_family=NULL;
960 *minor_version=49; /* min version of Ghostscript is 5.50 */
961 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
973 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
974 root=HKEY_LOCAL_MACHINE;
975 mode=KEY_READ | flags;
976 if (RegOpenKeyExA(root,key,0,mode,&hkey) == ERROR_SUCCESS)
985 Now enumerate the keys.
987 extent=sizeof(key)/sizeof(char);
988 for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
996 if (sscanf(key,"%d.%d",&major,&minor) != 2)
998 if ((major > *major_version) || ((major == *major_version) &&
999 (minor > *minor_version)))
1001 *product_family=products[i];
1002 *major_version=major;
1003 *minor_version=minor;
1007 (void) RegCloseKey(hkey);
1010 if (status == MagickFalse)
1015 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1016 "version %d.%02d",*product_family,*major_version,*minor_version);
1020 static BOOL NTIs64BitPlatform()
1022 #if defined(_WIN64) || !defined(KEY_WOW64_32KEY)
1026 return(IsWow64Process(GetCurrentProcess(), &is64) && is64);
1030 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,char *value,
1031 const size_t length)
1041 *product_family = (const char *) NULL;
1044 is_64_bit_version = FALSE;
1061 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
1062 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
1066 Get a string from the installed Ghostscript.
1068 if (is_64_bit!=NULL)
1071 if (product_family == NULL)
1074 #if defined(KEY_WOW64_32KEY)
1075 flags=NTIs64BitPlatform() ? KEY_WOW64_64KEY : 0;
1077 (void) NTLocateGhostscript(flags,&product_family,&major_version,
1079 if (product_family == NULL)
1084 We are running on a 64 bit platform - check for a 32 bit Ghostscript.
1087 #if defined(KEY_WOW64_32KEY)
1088 flags=KEY_WOW64_32KEY;
1090 (void) NTLocateGhostscript(flags,&product_family,&major_version,
1095 is_64_bit_version=NTIs64BitPlatform();
1097 if (is_64_bit != NULL)
1098 *is_64_bit=is_64_bit_version;
1099 if (product_family == NULL)
1101 (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1102 product_family,major_version,minor_version);
1103 for (i=0; i < (ssize_t) (sizeof(hkeys)/sizeof(hkeys[0])); i++)
1105 extent=(int) length;
1106 if (NTGetRegistryValue(hkeys[i].hkey,key,flags,name,value,&extent) == 0)
1108 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1109 "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
1112 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1113 "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
1118 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1121 dll[MaxTextExtent] = { "" };
1127 if ((*dll == '\0') &&
1128 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1132 if (!is_64_bit_version)
1135 if (is_64_bit_version)
1138 (void) CopyMagickString(path,dll,length);
1143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1147 % N T G h o s t s c r i p t D L L V e c t o r s %
1151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153 % NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1154 % function vectors to invoke Ghostscript DLL functions. A null pointer is
1155 % returned if there is an error when loading the DLL or retrieving the
1158 % The format of the NTGhostscriptDLLVectors method is:
1160 % const GhostInfo *NTGhostscriptDLLVectors(void)
1163 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1165 if (NTGhostscriptLoadDLL() == FALSE)
1166 return((GhostInfo *) NULL);
1167 return(&ghost_info);
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175 % N T G h o s t s c r i p t E X E %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1181 % NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1182 % The method returns FALSE if a full path value is not obtained and returns
1183 % a default path of gswin32c.exe.
1185 % The format of the NTGhostscriptEXE method is:
1187 % int NTGhostscriptEXE(char *path,int length)
1189 % A description of each parameter follows:
1191 % o path: return the Ghostscript executable path here.
1193 % o length: length of buffer.
1196 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1202 program[MaxTextExtent] = { "" };
1205 is_64_bit_version = FALSE;
1207 (void) CopyMagickString(path,"gswin32c.exe",length);
1208 if ((*program == '\0') &&
1209 (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1211 p=strrchr(program,'\\');
1212 if (p != (char *) NULL)
1216 (void) ConcatenateMagickString(program,is_64_bit_version ?
1217 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1219 (void) CopyMagickString(path,program,length);
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228 % N T G h o s t s c r i p t F o n t s %
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1234 % NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1235 % returns FALSE if it cannot determine the font path.
1237 % The format of the NTGhostscriptFonts method is:
1239 % int NTGhostscriptFonts(char *path, int length)
1241 % A description of each parameter follows:
1243 % o path: return the font path here.
1245 % o length: length of the path buffer.
1248 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1251 buffer[MaxTextExtent],
1252 filename[MaxTextExtent];
1259 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1261 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1263 (void) CopyMagickString(path,p+1,length+1);
1264 q=strchr(path,DirectoryListSeparator);
1265 if (q != (char *) NULL)
1267 (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1268 DirectorySeparator);
1269 if (IsPathAccessible(filename) != MagickFalse)
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 % N T G h o s t s c r i p t L o a d D L L %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1287 % TRUE if it succeeds.
1289 % The format of the NTGhostscriptLoadDLL method is:
1291 % int NTGhostscriptLoadDLL(void)
1294 MagickPrivate int NTGhostscriptLoadDLL(void)
1297 path[MaxTextExtent];
1299 if (ghost_handle != (void *) NULL)
1301 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1303 ghost_handle=lt_dlopen(path);
1304 if (ghost_handle == (void *) NULL)
1306 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1307 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1308 lt_dlsym(ghost_handle,"gsapi_exit");
1309 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1310 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1311 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1312 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1313 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1314 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1315 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1316 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1317 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1318 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1319 (ghost_info.delete_instance == NULL))
1325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1329 % N T G h o s t s c r i p t U n L o a d D L L %
1333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335 % NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1338 % The format of the NTGhostscriptUnLoadDLL method is:
1340 % int NTGhostscriptUnLoadDLL(void)
1343 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1348 if (ghost_handle == (void *) NULL)
1350 status=lt_dlclose(ghost_handle);
1351 ghost_handle=(void *) NULL;
1352 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1361 % N T I n i t i a l i z e L i b r a r y %
1365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1367 % NTInitializeLibrary() initializes the dynamic module loading subsystem.
1369 % The format of the NTInitializeLibrary method is:
1371 % int NTInitializeLibrary(void)
1374 MagickPrivate int NTInitializeLibrary(void)
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 + N T M a p M e m o r y %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 % Mmap() emulates the Unix method of the same name.
1392 % The format of the NTMapMemory method is:
1394 % MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1395 % int access,int file,MagickOffsetType offset)
1398 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1399 int flags,int file,MagickOffsetType offset)
1418 file_handle=INVALID_HANDLE_VALUE;
1419 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1420 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1421 map_handle=INVALID_HANDLE_VALUE;
1423 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1424 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1426 if (protection & PROT_WRITE)
1428 access_mode=FILE_MAP_WRITE;
1429 if (!(flags & MAP_PRIVATE))
1430 protection_mode=PAGE_READWRITE;
1433 access_mode=FILE_MAP_COPY;
1434 protection_mode=PAGE_WRITECOPY;
1438 if (protection & PROT_READ)
1440 access_mode=FILE_MAP_READ;
1441 protection_mode=PAGE_READONLY;
1443 if ((file == -1) && (flags & MAP_ANONYMOUS))
1444 file_handle=INVALID_HANDLE_VALUE;
1446 file_handle=(HANDLE) _get_osfhandle(file);
1447 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1451 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1453 CloseHandle(map_handle);
1455 if (map == (void *) NULL)
1456 return((void *) MAP_FAILED);
1457 return((void *) ((char *) map));
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % N T O p e n D i r e c t o r y %
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 % NTOpenDirectory() opens the directory named by filename and associates a
1472 % directory stream with it.
1474 % The format of the NTOpenDirectory method is:
1476 % DIR *NTOpenDirectory(const char *path)
1478 % A description of each parameter follows:
1480 % o entry: Specifies a pointer to a DIR structure.
1483 MagickPrivate DIR *NTOpenDirectory(const char *path)
1486 file_specification[MaxTextExtent];
1494 assert(path != (const char *) NULL);
1495 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1498 return((DIR *) NULL);
1499 if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1500 MaxTextExtent) == (wchar_t*)NULL)
1501 return((DIR *) NULL);
1502 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1503 if (entry != (DIR *) NULL)
1505 entry->firsttime=TRUE;
1506 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1508 if (entry->hSearch == INVALID_HANDLE_VALUE)
1510 if(wcsncat(file_specification,L"*.*",
1511 MaxTextExtent) == (wchar_t*)NULL)
1513 entry=(DIR *) RelinquishMagickMemory(entry);
1514 return((DIR *) NULL);
1516 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1517 if (entry->hSearch == INVALID_HANDLE_VALUE)
1519 entry=(DIR *) RelinquishMagickMemory(entry);
1520 return((DIR *) NULL);
1527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % N T O p e n L i b r a r y %
1535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1537 % NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1538 % can be used to access the various procedures in the module.
1540 % The format of the NTOpenLibrary method is:
1542 % void *NTOpenLibrary(const char *filename)
1544 % A description of each parameter follows:
1546 % o path: Specifies a pointer to string representing dynamic module that
1551 static inline const char *GetSearchPath(void)
1553 #if defined(MAGICKCORE_LTDL_DELEGATE)
1554 return(lt_dlgetsearchpath());
1556 return(lt_slsearchpath);
1560 static UINT ChangeErrorMode(void)
1563 (CALLBACK *GETERRORMODE)(void);
1574 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1576 handle=GetModuleHandle("kernel32.dll");
1577 if (handle == (HMODULE) NULL)
1578 return SetErrorMode(mode);
1580 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1581 if (getErrorMode != (GETERRORMODE) NULL)
1582 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1584 return SetErrorMode(mode);
1587 MagickPrivate void *NTOpenLibrary(const char *filename)
1589 #define MaxPathElements 31
1592 buffer[MaxTextExtent];
1610 mode=ChangeErrorMode();
1611 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1612 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1617 p=(char *) GetSearchPath();
1619 while (index < MaxPathElements)
1621 q=strchr(p,DirectoryListSeparator);
1622 if (q == (char *) NULL)
1624 (void) CopyMagickString(buffer,p,MaxTextExtent);
1625 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1626 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1627 handle=(void *) LoadLibraryEx(buffer,NULL,
1628 LOAD_WITH_ALTERED_SEARCH_PATH);
1632 (void) CopyMagickString(buffer,p,i+1);
1633 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1634 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1635 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1636 if (handle != (void *) NULL)
1645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1649 % N T R e a d D i r e c t o r y %
1653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655 % NTReadDirectory() returns a pointer to a structure representing the
1656 % directory entry at the current position in the directory stream to which
1659 % The format of the NTReadDirectory
1661 % NTReadDirectory(entry)
1663 % A description of each parameter follows:
1665 % o entry: Specifies a pointer to a DIR structure.
1668 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1676 if (entry == (DIR *) NULL)
1677 return((struct dirent *) NULL);
1678 if (!entry->firsttime)
1680 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1682 return((struct dirent *) NULL);
1684 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1685 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1687 return((struct dirent *) NULL);
1688 entry->firsttime=FALSE;
1689 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1690 return(&entry->file_info);
1694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1698 % N T R e g i s t r y K e y L o o k u p %
1702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1704 % NTRegistryKeyLookup() returns ImageMagick installation path settings
1705 % stored in the Windows Registry. Path settings are specific to the
1706 % installed ImageMagick version so that multiple Image Magick installations
1709 % Values are stored in the registry under a base path path similar to
1710 % "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1711 % "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1712 % is appended to this base path to form the full key.
1714 % The format of the NTRegistryKeyLookup method is:
1716 % unsigned char *NTRegistryKeyLookup(const char *subkey)
1718 % A description of each parameter follows:
1720 % o subkey: Specifies a string that identifies the registry object.
1721 % Currently supported sub-keys include: "BinPath", "ConfigurePath",
1722 % "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1725 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1728 package_key[MaxTextExtent];
1746 (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1747 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1748 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1749 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1750 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,®istry_key);
1751 if (status != ERROR_SUCCESS)
1752 status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1754 if (status != ERROR_SUCCESS)
1756 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1757 return((unsigned char *) NULL);
1763 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1764 if (value == (unsigned char *) NULL)
1766 RegCloseKey(registry_key);
1767 return((unsigned char *) NULL);
1769 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1770 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1771 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1773 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1774 if (value == (BYTE *) NULL)
1776 RegCloseKey(registry_key);
1777 return((unsigned char *) NULL);
1779 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1781 RegCloseKey(registry_key);
1782 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1783 value=(unsigned char *) RelinquishMagickMemory(value);
1784 return((unsigned char *) value);
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1792 % N T R e p o r t E v e n t %
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1798 % NTReportEvent() reports an event.
1800 % The format of the NTReportEvent method is:
1802 % MagickBooleanType NTReportEvent(const char *event,
1803 % const MagickBooleanType error)
1805 % A description of each parameter follows:
1807 % o event: the event.
1809 % o error: MagickTrue the event is an error.
1812 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1813 const MagickBooleanType error)
1824 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1826 return(MagickFalse);
1828 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1829 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1830 DeregisterEventSource(handle);
1835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1839 % N T R e s o u r c e T o B l o b %
1843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1845 % NTResourceToBlob() returns a blob containing the contents of the resource
1846 % in the current executable specified by the id parameter. This currently
1847 % used to retrieve MGK files tha have been embedded into the various command
1850 % The format of the NTResourceToBlob method is:
1852 % unsigned char *NTResourceToBlob(const char *id)
1854 % A description of each parameter follows:
1856 % o id: Specifies a string that identifies the resource.
1859 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1862 #ifndef MAGICKCORE_LIBRARY_NAME
1864 path[MaxTextExtent];
1883 assert(id != (const char *) NULL);
1884 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1885 #ifdef MAGICKCORE_LIBRARY_NAME
1886 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1888 (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1889 DirectorySeparator,GetClientName());
1890 if (IsPathAccessible(path) != MagickFalse)
1891 handle=GetModuleHandle(path);
1893 handle=GetModuleHandle(0);
1896 return((unsigned char *) NULL);
1897 resource=FindResource(handle,id,"IMAGEMAGICK");
1899 return((unsigned char *) NULL);
1900 global=LoadResource(handle,resource);
1902 return((unsigned char *) NULL);
1903 length=SizeofResource(handle,resource);
1904 value=(unsigned char *) LockResource(global);
1907 FreeResource(global);
1908 return((unsigned char *) NULL);
1910 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1912 if (blob != (unsigned char *) NULL)
1914 (void) CopyMagickMemory(blob,value,length);
1917 UnlockResource(global);
1918 FreeResource(global);
1923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1927 % N T S e e k D i r e c t o r y %
1931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1933 % NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1934 % on the directory stream.
1936 % The format of the NTSeekDirectory method is:
1938 % void NTSeekDirectory(DIR *entry,ssize_t position)
1940 % A description of each parameter follows:
1942 % o entry: Specifies a pointer to a DIR structure.
1944 % o position: specifies the position associated with the directory
1948 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1952 assert(entry != (DIR *) NULL);
1956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960 % N T S e t S e a r c h P a t h %
1964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1966 % NTSetSearchPath() sets the current locations that the subsystem should
1967 % look at to find dynamically loadable modules.
1969 % The format of the NTSetSearchPath method is:
1971 % int NTSetSearchPath(const char *path)
1973 % A description of each parameter follows:
1975 % o path: Specifies a pointer to string representing the search path
1976 % for DLL's that can be dynamically loaded.
1979 MagickPrivate int NTSetSearchPath(const char *path)
1981 #if defined(MAGICKCORE_LTDL_DELEGATE)
1982 lt_dlsetsearchpath(path);
1984 if (lt_slsearchpath != (char *) NULL)
1985 lt_slsearchpath=DestroyString(lt_slsearchpath);
1986 if (path != (char *) NULL)
1987 lt_slsearchpath=AcquireString(path);
1993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1997 + N T S y n c M e m o r y %
2001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2003 % NTSyncMemory() emulates the Unix method of the same name.
2005 % The format of the NTSyncMemory method is:
2007 % int NTSyncMemory(void *address,size_t length,int flags)
2009 % A description of each parameter follows:
2011 % o address: the address of the binary large object.
2013 % o length: the length of the binary large object.
2015 % o flags: Option flags (ignored for Windows).
2018 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2021 if (FlushViewOfFile(address,length) == MagickFalse)
2027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2031 % N T S y s t e m C o m m a n d %
2035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2037 % NTSystemCommand() executes the specified command and waits until it
2038 % terminates. The returned value is the exit status of the command.
2040 % The format of the NTSystemCommand method is:
2042 % int NTSystemCommand(MagickFalse,const char *command)
2044 % A description of each parameter follows:
2046 % o command: This string is the command to execute.
2049 MagickPrivate int NTSystemCommand(const char *command)
2052 local_command[MaxTextExtent];
2069 if (command == (char *) NULL)
2071 GetStartupInfo(&startup_info);
2072 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2073 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2074 (void) CopyMagickString(local_command,command,MaxTextExtent);
2075 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
2077 if (background_process != MagickFalse)
2078 local_command[strlen(command)-1]='\0';
2079 if (command[strlen(command)-1] == '|')
2080 local_command[strlen(command)-1]='\0';
2082 startup_info.wShowWindow=SW_HIDE;
2083 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2084 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2085 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2089 if (background_process != MagickFalse)
2090 return(status == 0);
2091 status=WaitForSingleObject(process_info.hProcess,INFINITE);
2092 if (status != WAIT_OBJECT_0)
2094 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2097 CloseHandle(process_info.hProcess);
2098 CloseHandle(process_info.hThread);
2099 return((int) child_status);
2103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2107 % N T S y s t e m C o n i f i g u r a t i o n %
2111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113 % NTSystemConfiguration() provides a way for the application to determine
2114 % values for system limits or options at runtime.
2116 % The format of the exit method is:
2118 % ssize_t NTSystemConfiguration(int name)
2120 % A description of each parameter follows:
2122 % o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2125 MagickPrivate ssize_t NTSystemConfiguration(int name)
2134 GetSystemInfo(&system_info);
2135 return(system_info.dwPageSize);
2137 case _SC_PHYS_PAGES:
2151 handle=GetModuleHandle("kernel32.dll");
2152 if (handle == (HMODULE) NULL)
2154 GetSystemInfo(&system_info);
2155 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2156 if (module == (LPFNDLLFUNC2) NULL)
2161 GlobalMemoryStatus(&status);
2162 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2164 status.dwLength=sizeof(status);
2165 if (module(&status) == 0)
2167 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2182 % N T T e l l D i r e c t o r y %
2186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2188 % NTTellDirectory() returns the current location associated with the named
2191 % The format of the NTTellDirectory method is:
2193 % ssize_t NTTellDirectory(DIR *entry)
2195 % A description of each parameter follows:
2197 % o entry: Specifies a pointer to a DIR structure.
2200 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2202 assert(entry != (DIR *) NULL);
2207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2211 % N T T r u n c a t e F i l e %
2215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2217 % NTTruncateFile() truncates a file to a specified length.
2219 % The format of the NTTruncateFile method is:
2221 % int NTTruncateFile(int file,off_t length)
2223 % A description of each parameter follows:
2227 % o length: the file length.
2230 MagickPrivate int NTTruncateFile(int file,off_t length)
2240 file_handle=_get_osfhandle(file);
2241 if (file_handle == -1L)
2243 low=(long) (length & 0xffffffffUL);
2244 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2245 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2246 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2248 if (SetEndOfFile((HANDLE) file_handle) == 0)
2254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2258 + N T U n m a p M e m o r y %
2262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2264 % NTUnmapMemory() emulates the Unix munmap method.
2266 % The format of the NTUnmapMemory method is:
2268 % int NTUnmapMemory(void *map,size_t length)
2270 % A description of each parameter follows:
2272 % o map: the address of the binary large object.
2274 % o length: the length of the binary large object.
2277 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2280 if (UnmapViewOfFile(map) == 0)
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2290 % N T U s e r T i m e %
2294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2296 % NTUserTime() returns the total time the process has been scheduled (e.g.
2297 % seconds) since the last call to StartTimer().
2299 % The format of the UserTime method is:
2301 % double NTUserTime(void)
2304 MagickPrivate double NTUserTime(void)
2334 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2335 GetVersionEx(&OsVersionInfo);
2336 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2337 return(NTElapsedTime());
2338 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2339 &kernel_time.filetime,&user_time.filetime);
2342 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2350 % N T W a r n i n g H a n d l e r %
2354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2356 % NTWarningHandler() displays a warning reason.
2358 % The format of the NTWarningHandler method is:
2360 % void NTWarningHandler(const ExceptionType severity,const char *reason,
2361 % const char *description)
2363 % A description of each parameter follows:
2365 % o severity: Specifies the numeric warning category.
2367 % o reason: Specifies the reason to display before terminating the
2370 % o description: Specifies any description to the reason.
2373 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2374 const char *reason,const char *description)
2377 buffer[2*MaxTextExtent];
2380 if (reason == (char *) NULL)
2382 if (description == (char *) NULL)
2383 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2386 (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2387 GetClientName(),reason,description);
2388 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2389 MB_SETFOREGROUND | MB_ICONINFORMATION);