]> granicus.if.org Git - imagemagick/blob - MagickCore/nt-base.c
(no commit message)
[imagemagick] / MagickCore / nt-base.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                                 N   N  TTTTT                                %
7 %                                 NN  N    T                                  %
8 %                                 N N N    T                                  %
9 %                                 N  NN    T                                  %
10 %                                 N   N    T                                  %
11 %                                                                             %
12 %                                                                             %
13 %                   Windows NT Utility Methods for MagickCore                 %
14 %                                                                             %
15 %                               Software Design                               %
16 %                                    Cristy                                   %
17 %                                December 1996                                %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 /*
39   Include declarations.
40 */
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)
58 #  include "ltdl.h"
59 #endif
60 #include "MagickCore/nt-base-private.h"
61 #if defined(MAGICKCORE_CIPHER_SUPPORT)
62 #include <ntsecapi.h>
63 #include <wincrypt.h>
64 #endif
65 \f
66 /*
67   Define declarations.
68 */
69 #if !defined(MAP_FAILED)
70 #define MAP_FAILED      ((void *) -1)
71 #endif
72 \f
73 /*
74   Static declarations.
75 */
76 #if !defined(MAGICKCORE_LTDL_DELEGATE)
77 static char
78   *lt_slsearchpath = (char *) NULL;
79 #endif
80
81 static GhostInfo
82   ghost_info;
83
84 static void
85   *ghost_handle = (void *) NULL;
86 \f
87 /*
88   External declarations.
89 */
90 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
91 extern "C" BOOL WINAPI
92   DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
93 #endif
94 \f
95 /*
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 %                                                                             %
98 %                                                                             %
99 %                                                                             %
100 %   D l l M a i n                                                             %
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %
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.
109 %
110 %  The function returns TRUE of it succeeds, or FALSE if initialization fails.
111 %
112 %  The format of the DllMain method is:
113 %
114 %    BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
115 %
116 %  A description of each parameter follows:
117 %
118 %    o handle: handle to the DLL module
119 %
120 %    o reason: reason for calling function:
121 %
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
129 %                          thread.
130 %      DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
131 %                           from the virtual address space of the
132 %                           current process.
133 %
134 %    o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
135 %                   and DLL_PROCESS_DETACH.
136 %
137 */
138 #if defined(_DLL) && defined( ProvideDllMain )
139 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
140 {
141   switch (reason)
142   {
143     case DLL_PROCESS_ATTACH:
144     {
145       char
146         *module_path;
147
148       ssize_t
149         count;
150
151       module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
152         sizeof(*module_path));
153       if (module_path == (char *) NULL)
154         return(FALSE);
155       count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
156       if (count != 0)
157         {
158           char
159             *path;
160
161           for ( ; count > 0; count--)
162             if (module_path[count] == '\\')
163               {
164                 module_path[count+1]='\0';
165                 break;
166               }
167           MagickCoreGenesis(module_path,MagickFalse);
168           path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
169           if (path == (char *) NULL)
170             {
171               module_path=DestroyString(module_path);
172               return(FALSE);
173             }
174           count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
175           if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
176             {
177               if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
178                 {
179                   char
180                     *variable;
181
182                   variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
183                     sizeof(*variable));
184                   if (variable == (char *) NULL)
185                     {
186                       path=DestroyString(path);
187                       module_path=DestroyString(module_path);
188                       return(FALSE);
189                     }
190                   (void) FormatLocaleString(variable,16*MaxTextExtent,
191                     "%s;%s",module_path,path);
192                   SetEnvironmentVariable("PATH",variable);
193                   variable=DestroyString(variable);
194                 }
195             }
196           path=DestroyString(path);
197         }
198       module_path=DestroyString(module_path);
199       break;
200     }
201     case DLL_PROCESS_DETACH:
202     {
203       MagickCoreTerminus();
204       break;
205     }
206     default:
207       break;
208   }
209   return(TRUE);
210 }
211 #endif
212 \f
213 /*
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %                                                                             %
216 %                                                                             %
217 %                                                                             %
218 %   E x i t                                                                   %
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %
224 %  Exit() calls TerminateProcess for Win95.
225 %
226 %  The format of the exit method is:
227 %
228 %      int Exit(int status)
229 %
230 %  A description of each parameter follows:
231 %
232 %    o status: an integer value representing the status of the terminating
233 %      process.
234 %
235 */
236 MagickPrivate int Exit(int status)
237 {
238   if (IsWindows95())
239     {
240       TerminateProcess(GetCurrentProcess(),(unsigned int) status);
241       return(0);
242     }
243   exit(status);
244 }
245 \f
246 #if !defined(__MINGW32__) && !defined(__MINGW64__)
247 /*
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 %                                                                             %
250 %                                                                             %
251 %                                                                             %
252 %   g e t t i m e o f d a y                                                   %
253 %                                                                             %
254 %                                                                             %
255 %                                                                             %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 %
258 %  The gettimeofday() method get the time of day.
259 %
260 %  The format of the gettimeofday method is:
261 %
262 %      int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
263 %
264 %  A description of each parameter follows:
265 %
266 %    o time_value: the time value.
267 %
268 %    o time_zone: the time zone.
269 %
270 */
271 MagickPrivate int gettimeofday (struct timeval *time_value,
272   struct timezone *time_zone)
273 {
274 #define EpochFiletime  MagickLLConstant(116444736000000000)
275
276   static int
277     is_tz_set;
278
279   if (time_value != (struct timeval *) NULL)
280     {
281       FILETIME
282         file_time;
283
284       __int64
285         time;
286
287       LARGE_INTEGER
288         date_time;
289
290       GetSystemTimeAsFileTime(&file_time);
291       date_time.LowPart=file_time.dwLowDateTime;
292       date_time.HighPart=file_time.dwHighDateTime;
293       time=date_time.QuadPart;
294       time-=EpochFiletime;
295       time/=10;
296       time_value->tv_sec=(ssize_t) (time / 1000000);
297       time_value->tv_usec=(ssize_t) (time % 1000000);
298     }
299   if (time_zone != (struct timezone *) NULL)
300     {
301       if (is_tz_set == 0)
302         {
303           _tzset();
304           is_tz_set++;
305         }
306       time_zone->tz_minuteswest=_timezone/60;
307       time_zone->tz_dsttime=_daylight;
308     }
309   return(0);
310 }
311 #endif
312 \f
313 /*
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %                                                                             %
316 %                                                                             %
317 %                                                                             %
318 %   I s W i n d o w s 9 5                                                     %
319 %                                                                             %
320 %                                                                             %
321 %                                                                             %
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 %
324 %  IsWindows95() returns true if the system is Windows 95.
325 %
326 %  The format of the IsWindows95 method is:
327 %
328 %      int IsWindows95()
329 %
330 */
331 MagickPrivate int IsWindows95()
332 {
333   OSVERSIONINFO
334     version_info;
335
336   version_info.dwOSVersionInfoSize=sizeof(version_info);
337   if (GetVersionEx(&version_info) &&
338       (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
339     return(1);
340   return(0);
341 }
342 \f
343 /*
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 %                                                                             %
346 %                                                                             %
347 %                                                                             %
348 %   N T A r g v T o U T F 8                                                   %
349 %                                                                             %
350 %                                                                             %
351 %                                                                             %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 %
354 %  NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
355 %  compatibility with Linux.
356 %
357 %  The format of the NTArgvToUTF8 method is:
358 %
359 %      char **NTArgvToUTF8(const int argc,wchar_t **argv)
360 %
361 %  A description of each parameter follows:
362 %
363 %    o argc: the number of command line arguments.
364 %
365 %    o argv:  the  wide-character command line arguments.
366 %
367 */
368 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
369 {
370   char
371     **utf8;
372
373   ssize_t
374     i;
375
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++)
380   {
381     ssize_t
382       count;
383
384     count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
385     if (count < 0)
386       count=0;
387     utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
388     if (utf8[i] == (char *) NULL)
389       {
390         for (i--; i >= 0; i--)
391           utf8[i]=DestroyString(utf8[i]);
392         utf8=(char **) RelinquishMagickMemory(utf8);
393         ThrowFatalException(ResourceLimitFatalError,
394           "UnableToConvertStringToARGV");
395       }
396     count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
397     utf8[i][count]=0;
398   }
399   return(utf8);
400 }
401 \f
402 /*
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 %                                                                             %
405 %                                                                             %
406 %                                                                             %
407 %   N T C l o s e D i r e c t o r y                                           %
408 %                                                                             %
409 %                                                                             %
410 %                                                                             %
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 %
413 %  NTCloseDirectory() closes the named directory stream and frees the DIR
414 %  structure.
415 %
416 %  The format of the NTCloseDirectory method is:
417 %
418 %      int NTCloseDirectory(DIR *entry)
419 %
420 %  A description of each parameter follows:
421 %
422 %    o entry: Specifies a pointer to a DIR structure.
423 %
424 */
425 MagickPrivate int NTCloseDirectory(DIR *entry)
426 {
427   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
428   assert(entry != (DIR *) NULL);
429   FindClose(entry->hSearch);
430   entry=(DIR *) RelinquishMagickMemory(entry);
431   return(0);
432 }
433 \f
434 /*
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 %                                                                             %
437 %                                                                             %
438 %                                                                             %
439 %   N T C l o s e L i b r a r y                                               %
440 %                                                                             %
441 %                                                                             %
442 %                                                                             %
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 %
445 %  NTCloseLibrary() unloads the module associated with the passed handle.
446 %
447 %  The format of the NTCloseLibrary method is:
448 %
449 %      void NTCloseLibrary(void *handle)
450 %
451 %  A description of each parameter follows:
452 %
453 %    o handle: Specifies a handle to a previously loaded dynamic module.
454 %
455 */
456 MagickPrivate int NTCloseLibrary(void *handle)
457 {
458   if (IsWindows95())
459     return(FreeLibrary((HINSTANCE) handle));
460   return(!(FreeLibrary((HINSTANCE) handle)));
461 }
462 \f
463 /*
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 %                                                                             %
466 %                                                                             %
467 %                                                                             %
468 %   N T C o n t r o l H a n d l e r                                           %
469 %                                                                             %
470 %                                                                             %
471 %                                                                             %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 %
474 %  NTControlHandler() registers a control handler that is activated when, for
475 %  example, a ctrl-c is received.
476 %
477 %  The format of the NTControlHandler method is:
478 %
479 %      int NTControlHandler(void)
480 %
481 */
482
483 static BOOL ControlHandler(DWORD type)
484 {
485   (void) type;
486   AsynchronousResourceComponentTerminus();
487   return(FALSE);
488 }
489
490 MagickPrivate int NTControlHandler(void)
491 {
492   return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
493 }
494 \f
495 /*
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 %                                                                             %
498 %                                                                             %
499 %                                                                             %
500 %   N T E l a p s e d T i m e                                                 %
501 %                                                                             %
502 %                                                                             %
503 %                                                                             %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 %
506 %  NTElapsedTime() returns the elapsed time (in seconds) since the last call to
507 %  StartTimer().
508 %
509 %  The format of the ElapsedTime method is:
510 %
511 %      double NTElapsedTime(void)
512 %
513 */
514 MagickPrivate double NTElapsedTime(void)
515 {
516   union
517   {
518     FILETIME
519       filetime;
520
521     __int64
522       filetime64;
523   } elapsed_time;
524
525   SYSTEMTIME
526     system_time;
527
528   GetSystemTime(&system_time);
529   SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
530   return((double) 1.0e-7*elapsed_time.filetime64);
531 }
532 \f
533 /*
534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 %                                                                             %
536 %                                                                             %
537 %                                                                             %
538 +   N T E r r o r H a n d l e r                                               %
539 %                                                                             %
540 %                                                                             %
541 %                                                                             %
542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 %
544 %  NTErrorHandler() displays an error reason and then terminates the program.
545 %
546 %  The format of the NTErrorHandler method is:
547 %
548 %      void NTErrorHandler(const ExceptionType severity,const char *reason,
549 %        const char *description)
550 %
551 %  A description of each parameter follows:
552 %
553 %    o severity: Specifies the numeric error category.
554 %
555 %    o reason: Specifies the reason to display before terminating the
556 %      program.
557 %
558 %    o description: Specifies any description to the reason.
559 %
560 */
561 MagickPrivate void NTErrorHandler(const ExceptionType severity,
562   const char *reason,const char *description)
563 {
564   char
565     buffer[3*MaxTextExtent],
566     *message;
567
568   (void) severity;
569   if (reason == (char *) NULL)
570     {
571       MagickCoreTerminus();
572       exit(0);
573     }
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);
578   else
579     if (description != (char *) NULL)
580       (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
581         GetClientName(),reason,description);
582     else
583       if (errno != 0)
584         (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
585           GetClientName(),reason,message);
586       else
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();
593   exit(0);
594 }
595 \f
596 /*
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 %                                                                             %
599 %                                                                             %
600 %                                                                             %
601 %   N T E x i t L i b r a r y                                                 %
602 %                                                                             %
603 %                                                                             %
604 %                                                                             %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %
607 %  NTExitLibrary() exits the dynamic module loading subsystem.
608 %
609 %  The format of the NTExitLibrary method is:
610 %
611 %      int NTExitLibrary(void)
612 %
613 */
614 MagickPrivate int NTExitLibrary(void)
615 {
616   return(0);
617 }
618 \f
619 /*
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 %                                                                             %
622 %                                                                             %
623 %                                                                             %
624 %   N T G a t h e r R a n d o m D a t a                                       %
625 %                                                                             %
626 %                                                                             %
627 %                                                                             %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 %
630 %  NTGatherRandomData() gathers random data and returns it.
631 %
632 %  The format of the GatherRandomData method is:
633 %
634 %      MagickBooleanType NTGatherRandomData(const size_t length,
635 %        unsigned char *random)
636 %
637 %  A description of each parameter follows:
638 %
639 %    length: the length of random data buffer
640 %
641 %    random: the random data is returned here.
642 %
643 */
644 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
645   unsigned char *random)
646 {
647 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
648   HCRYPTPROV
649     handle;
650
651   int
652     status;
653
654   handle=(HCRYPTPROV) NULL;
655   status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
656     (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
657   if (status == 0)
658     status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
659       (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
660   if (status == 0)
661     return(MagickFalse);
662   status=CryptGenRandom(handle,(DWORD) length,random);
663   if (status == 0)
664     {
665       status=CryptReleaseContext(handle,0);
666       return(MagickFalse);
667     }
668   status=CryptReleaseContext(handle,0);
669   if (status == 0)
670     return(MagickFalse);
671 #else
672   (void) random;
673   (void) length;
674 #endif
675   return(MagickTrue);
676 }
677 \f
678 /*
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 %                                                                             %
681 %                                                                             %
682 %                                                                             %
683 %   N T G e t E x e c u t i o n P a t h                                       %
684 %                                                                             %
685 %                                                                             %
686 %                                                                             %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 %
689 %  NTGetExecutionPath() returns the execution path of a program.
690 %
691 %  The format of the GetExecutionPath method is:
692 %
693 %      MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
694 %
695 %  A description of each parameter follows:
696 %
697 %    o path: the pathname of the executable that started the process.
698 %
699 %    o extent: the maximum extent of the path.
700 %
701 */
702 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
703   const size_t extent)
704 {
705   GetModuleFileName(0,path,(DWORD) extent);
706   return(MagickTrue);
707 }
708 \f
709 /*
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 %                                                                             %
712 %                                                                             %
713 %                                                                             %
714 %   N T G e t L a s t E r r o r                                               %
715 %                                                                             %
716 %                                                                             %
717 %                                                                             %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 %
720 %  NTGetLastError() returns the last error that occurred.
721 %
722 %  The format of the NTGetLastError method is:
723 %
724 %      char *NTGetLastError(void)
725 %
726 */
727 char *NTGetLastError(void)
728 {
729   char
730     *reason;
731
732   int
733     status;
734
735   LPVOID
736     buffer;
737
738   status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
739     FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
740     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
741   if (!status)
742     reason=AcquireString("An unknown error occurred");
743   else
744     {
745       reason=AcquireString((const char *) buffer);
746       LocalFree(buffer);
747     }
748   return(reason);
749 }
750 \f
751 /*
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 %                                                                             %
754 %                                                                             %
755 %                                                                             %
756 %   N T G e t L i b r a r y E r r o r                                         %
757 %                                                                             %
758 %                                                                             %
759 %                                                                             %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 %
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.
765 %
766 %  The format of the NTGetLibraryError method is:
767 %
768 %      const char *NTGetLibraryError(void)
769 %
770 */
771 MagickPrivate const char *NTGetLibraryError(void)
772 {
773   static char
774     last_error[MaxTextExtent];
775
776   char
777     *error;
778
779   *last_error='\0';
780   error=NTGetLastError();
781   if (error)
782     (void) CopyMagickString(last_error,error,MaxTextExtent);
783   error=DestroyString(error);
784   return(last_error);
785 }
786 \f
787 /*
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 %                                                                             %
790 %                                                                             %
791 %                                                                             %
792 %   N T G e t L i b r a r y S y m b o l                                       %
793 %                                                                             %
794 %                                                                             %
795 %                                                                             %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 %
798 %  NTGetLibrarySymbol() retrieve the procedure address of the method
799 %  specified by the passed character string.
800 %
801 %  The format of the NTGetLibrarySymbol method is:
802 %
803 %      void *NTGetLibrarySymbol(void *handle,const char *name)
804 %
805 %  A description of each parameter follows:
806 %
807 %    o handle: Specifies a handle to the previously loaded dynamic module.
808 %
809 %    o name: Specifies the procedure entry point to be returned.
810 %
811 */
812 void *NTGetLibrarySymbol(void *handle,const char *name)
813 {
814   LPFNDLLFUNC1
815     lpfnDllFunc1;
816
817   lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
818   if (!lpfnDllFunc1)
819     return((void *) NULL);
820   return((void *) lpfnDllFunc1);
821 }
822 \f
823 /*
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 %                                                                             %
826 %                                                                             %
827 %                                                                             %
828 %   N T G e t M o d u l e P a t h                                             %
829 %                                                                             %
830 %                                                                             %
831 %                                                                             %
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 %
834 %  NTGetModulePath() returns the path of the specified module.
835 %
836 %  The format of the GetModulePath method is:
837 %
838 %      MagickBooleanType NTGetModulePath(const char *module,char *path)
839 %
840 %  A description of each parameter follows:
841 %
842 %    modith: the module name.
843 %
844 %    path: the module path is returned here.
845 %
846 */
847 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
848 {
849   char
850     module_path[MaxTextExtent];
851
852   HMODULE
853     handle;
854
855   ssize_t
856     length;
857
858   *path='\0';
859   handle=GetModuleHandle(module);
860   if (handle == (HMODULE) NULL)
861     return(MagickFalse);
862   length=GetModuleFileName(handle,module_path,MaxTextExtent);
863   if (length != 0)
864     GetPathComponent(module_path,HeadPath,path);
865   return(MagickTrue);
866 }
867 \f
868 /*
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 %                                                                             %
871 %                                                                             %
872 %                                                                             %
873 %   N T G h o s t s c r i p t D L L                                           %
874 %                                                                             %
875 %                                                                             %
876 %                                                                             %
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 %
879 %  NTGhostscriptDLL() returns the path to the most recent Ghostscript version
880 %  DLL.  The method returns TRUE on success otherwise FALSE.
881 %
882 %  The format of the NTGhostscriptDLL method is:
883 %
884 %      int NTGhostscriptDLL(char *path,int length)
885 %
886 %  A description of each parameter follows:
887 %
888 %    o path: return the Ghostscript DLL path here.
889 %
890 %    o length: the buffer length.
891 %
892 */
893
894 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
895   char *value,int *length)
896 {
897   BYTE
898     byte,
899     *p;
900
901   DWORD
902     extent,
903     type;
904
905   HKEY
906     hkey;
907
908   LONG
909     status;
910
911   /*
912     Get a registry value: key = root\\key, named value = name.
913   */
914   if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
915     return(1);  /* no match */
916   p=(BYTE *) value;
917   type=REG_SZ;
918   extent=(*length);
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);
922   RegCloseKey(hkey);
923   if (status == ERROR_SUCCESS)
924     {
925       *length=extent;
926       return(0);  /* return the match */
927     }
928   if (status == ERROR_MORE_DATA)
929     {
930       *length=extent;
931       return(-1);  /* buffer not large enough */
932     }
933   return(1);  /* not found */
934 }
935
936 static int NTLocateGhostscript(DWORD flags,const char **product_family,int *major_version,
937   int *minor_version)
938 {
939   int
940     i;
941
942   MagickBooleanType
943     status;
944
945   static const char
946     *products[4] =
947     {
948       "GPL Ghostscript",
949       "GNU Ghostscript",
950       "AFPL Ghostscript",
951       "Aladdin Ghostscript"
952     };
953
954   /*
955     Find the most recent version of Ghostscript.
956   */
957   status=MagickFalse;
958   *product_family=NULL;
959   *major_version=5;
960   *minor_version=49; /* min version of Ghostscript is 5.50 */
961   for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
962   {
963     char
964       key[MaxTextExtent];
965
966     HKEY
967       hkey,
968       root;
969
970     REGSAM
971       mode;
972
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)
977       {
978         DWORD
979           extent;
980
981         int
982           j;
983
984         /*
985           Now enumerate the keys.
986         */
987         extent=sizeof(key)/sizeof(char);
988         for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
989         {
990           int
991             major,
992             minor;
993
994           major=0;
995           minor=0;
996           if (sscanf(key,"%d.%d",&major,&minor) != 2)
997             continue;
998           if ((major > *major_version) || ((major == *major_version) &&
999               (minor > *minor_version)))
1000             {
1001               *product_family=products[i];
1002               *major_version=major;
1003               *minor_version=minor;
1004               status=MagickTrue;
1005             }
1006        }
1007        (void) RegCloseKey(hkey);
1008      }
1009   }
1010   if (status == MagickFalse)
1011     {
1012       *major_version=0;
1013       *minor_version=0;
1014     }
1015   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1016     "version %d.%02d",*product_family,*major_version,*minor_version);
1017   return(status);
1018 }
1019
1020 static BOOL NTIs64BitPlatform()
1021 {
1022 #if defined(_WIN64) || !defined(KEY_WOW64_32KEY)
1023   return(TRUE);
1024 #else
1025   BOOL is64=FALSE;
1026   return(IsWow64Process(GetCurrentProcess(), &is64) && is64);
1027 #endif
1028 }
1029
1030 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,char *value,
1031   const size_t length)
1032 {
1033   char
1034     key[MaxTextExtent];
1035
1036   int
1037     i,
1038     extent;
1039
1040   static const char
1041     *product_family = (const char *) NULL;
1042
1043   static BOOL
1044     is_64_bit_version = FALSE;
1045
1046   static int
1047     flags=0,
1048     major_version=0,
1049     minor_version=0;
1050
1051   struct
1052   {
1053     const HKEY
1054       hkey;
1055
1056     const char
1057       *name;
1058   }
1059   hkeys[2] =
1060   {
1061     { HKEY_CURRENT_USER,  "HKEY_CURRENT_USER"  },
1062     { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
1063   };
1064
1065   /*
1066     Get a string from the installed Ghostscript.
1067   */
1068   if (is_64_bit!=NULL)
1069     *is_64_bit=FALSE;
1070   *value='\0';
1071   if (product_family == NULL)
1072   {
1073     flags=0;
1074 #if defined(KEY_WOW64_32KEY)
1075     flags=NTIs64BitPlatform() ? KEY_WOW64_64KEY : 0;
1076 #endif
1077     (void) NTLocateGhostscript(flags,&product_family,&major_version,
1078       &minor_version);
1079     if (product_family == NULL)
1080     {
1081       if (flags!=0)
1082       {
1083         /*
1084           We are running on a 64 bit platform - check for a 32 bit Ghostscript.
1085         */
1086         flags=0;
1087 #if defined(KEY_WOW64_32KEY)
1088         flags=KEY_WOW64_32KEY;
1089 #endif
1090         (void) NTLocateGhostscript(flags,&product_family,&major_version,
1091           &minor_version);
1092       }
1093     }
1094     else
1095       is_64_bit_version=NTIs64BitPlatform();
1096   }
1097   if (is_64_bit != NULL)
1098     *is_64_bit=is_64_bit_version;
1099   if (product_family == NULL)
1100     return(FALSE);
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++)
1104   {
1105     extent=(int) length;
1106     if (NTGetRegistryValue(hkeys[i].hkey,key,flags,name,value,&extent) == 0)
1107       {
1108         (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1109           "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
1110         return(TRUE);
1111       }
1112     (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1113       "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
1114   }
1115   return(FALSE);
1116 }
1117
1118 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1119 {
1120   static char
1121     dll[MaxTextExtent] = { "" };
1122
1123   static BOOL
1124     is_64_bit_version;
1125
1126   *path='\0';
1127   if ((*dll == '\0') &&
1128       (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1129     return(FALSE);
1130
1131 #if defined(_WIN64)
1132   if (!is_64_bit_version)
1133     return(FALSE);
1134 #else
1135   if (is_64_bit_version)
1136     return(FALSE);
1137 #endif
1138   (void) CopyMagickString(path,dll,length);
1139   return(TRUE);
1140 }
1141 \f
1142 /*
1143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 %                                                                             %
1145 %                                                                             %
1146 %                                                                             %
1147 %   N T G h o s t s c r i p t D L L V e c t o r s                             %
1148 %                                                                             %
1149 %                                                                             %
1150 %                                                                             %
1151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152 %
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
1156 %  function vectors.
1157 %
1158 %  The format of the NTGhostscriptDLLVectors method is:
1159 %
1160 %      const GhostInfo *NTGhostscriptDLLVectors(void)
1161 %
1162 */
1163 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1164 {
1165   if (NTGhostscriptLoadDLL() == FALSE)
1166     return((GhostInfo *) NULL);
1167   return(&ghost_info);
1168 }
1169 \f
1170 /*
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 %                                                                             %
1173 %                                                                             %
1174 %                                                                             %
1175 %   N T G h o s t s c r i p t E X E                                           %
1176 %                                                                             %
1177 %                                                                             %
1178 %                                                                             %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 %
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.
1184 %
1185 %  The format of the NTGhostscriptEXE method is:
1186 %
1187 %      int NTGhostscriptEXE(char *path,int length)
1188 %
1189 %  A description of each parameter follows:
1190 %
1191 %    o path: return the Ghostscript executable path here.
1192 %
1193 %    o length: length of buffer.
1194 %
1195 */
1196 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1197 {
1198   register char
1199     *p;
1200
1201   static char
1202     program[MaxTextExtent] = { "" };
1203
1204   static BOOL
1205     is_64_bit_version = FALSE;
1206
1207   (void) CopyMagickString(path,"gswin32c.exe",length);
1208   if ((*program == '\0') &&
1209       (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1210     return(FALSE);
1211   p=strrchr(program,'\\');
1212   if (p != (char *) NULL)
1213     {
1214       p++;
1215       *p='\0';
1216       (void) ConcatenateMagickString(program,is_64_bit_version ?
1217         "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1218     }
1219   (void) CopyMagickString(path,program,length);
1220   return(TRUE);
1221 }
1222 \f
1223 /*
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225 %                                                                             %
1226 %                                                                             %
1227 %                                                                             %
1228 %   N T G h o s t s c r i p t F o n t s                                       %
1229 %                                                                             %
1230 %                                                                             %
1231 %                                                                             %
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 %
1234 %  NTGhostscriptFonts() obtains the path to the Ghostscript fonts.  The method
1235 %  returns FALSE if it cannot determine the font path.
1236 %
1237 %  The format of the NTGhostscriptFonts method is:
1238 %
1239 %      int NTGhostscriptFonts(char *path, int length)
1240 %
1241 %  A description of each parameter follows:
1242 %
1243 %    o path: return the font path here.
1244 %
1245 %    o length: length of the path buffer.
1246 %
1247 */
1248 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1249 {
1250   char
1251     buffer[MaxTextExtent],
1252     filename[MaxTextExtent];
1253
1254   register char
1255     *p,
1256     *q;
1257
1258   *path='\0';
1259   if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1260     return(FALSE);
1261   for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1262   {
1263     (void) CopyMagickString(path,p+1,length+1);
1264     q=strchr(path,DirectoryListSeparator);
1265     if (q != (char *) NULL)
1266       *q='\0';
1267     (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1268       DirectorySeparator);
1269     if (IsPathAccessible(filename) != MagickFalse)
1270       return(TRUE);
1271   }
1272   (void) FormatLocaleString(filename,MaxTextExtent,"c:%sgs%sfonts%sfonts.dir",
1273     DirectorySeparator,DirectorySeparator,DirectorySeparator);
1274   if (IsPathAccessible(filename) != MagickFalse)
1275     return(TRUE);
1276   return(FALSE);
1277 }
1278 \f
1279 /*
1280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1281 %                                                                             %
1282 %                                                                             %
1283 %                                                                             %
1284 %   N T G h o s t s c r i p t L o a d D L L                                   %
1285 %                                                                             %
1286 %                                                                             %
1287 %                                                                             %
1288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1289 %
1290 %  NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1291 %  TRUE if it succeeds.
1292 %
1293 %  The format of the NTGhostscriptLoadDLL method is:
1294 %
1295 %      int NTGhostscriptLoadDLL(void)
1296 %
1297 */
1298 MagickPrivate int NTGhostscriptLoadDLL(void)
1299 {
1300   char
1301     path[MaxTextExtent];
1302
1303   if (ghost_handle != (void *) NULL)
1304     return(TRUE);
1305   if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1306     return(FALSE);
1307   ghost_handle=lt_dlopen(path);
1308   if (ghost_handle == (void *) NULL)
1309     return(FALSE);
1310   (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1311   ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1312     lt_dlsym(ghost_handle,"gsapi_exit");
1313   ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1314     char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1315   ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1316     lt_dlsym(ghost_handle,"gsapi_new_instance"));
1317   ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1318     int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1319   ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1320     lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1321   if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1322       (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1323       (ghost_info.delete_instance == NULL))
1324     return(FALSE);
1325   return(TRUE);
1326 }
1327 \f
1328 /*
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330 %                                                                             %
1331 %                                                                             %
1332 %                                                                             %
1333 %   N T G h o s t s c r i p t U n L o a d D L L                               %
1334 %                                                                             %
1335 %                                                                             %
1336 %                                                                             %
1337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1338 %
1339 %  NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1340 %  it succeeds.
1341 %
1342 %  The format of the NTGhostscriptUnLoadDLL method is:
1343 %
1344 %      int NTGhostscriptUnLoadDLL(void)
1345 %
1346 */
1347 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1348 {
1349   int
1350     status;
1351
1352   if (ghost_handle == (void *) NULL)
1353     return(FALSE);
1354   status=lt_dlclose(ghost_handle);
1355   ghost_handle=(void *) NULL;
1356   (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1357   return(status);
1358 }
1359 \f
1360 /*
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362 %                                                                             %
1363 %                                                                             %
1364 %                                                                             %
1365 %   N T I n i t i a l i z e L i b r a r y                                     %
1366 %                                                                             %
1367 %                                                                             %
1368 %                                                                             %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 %
1371 %  NTInitializeLibrary() initializes the dynamic module loading subsystem.
1372 %
1373 %  The format of the NTInitializeLibrary method is:
1374 %
1375 %      int NTInitializeLibrary(void)
1376 %
1377 */
1378 MagickPrivate int NTInitializeLibrary(void)
1379 {
1380   return(0);
1381 }
1382 \f
1383 /*
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 %                                                                             %
1386 %                                                                             %
1387 %                                                                             %
1388 +  N T M a p M e m o r y                                                      %
1389 %                                                                             %
1390 %                                                                             %
1391 %                                                                             %
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 %
1394 %  Mmap() emulates the Unix method of the same name.
1395 %
1396 %  The format of the NTMapMemory method is:
1397 %
1398 %    MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1399 %      int access,int file,MagickOffsetType offset)
1400 %
1401 */
1402 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1403   int flags,int file,MagickOffsetType offset)
1404 {
1405   DWORD
1406     access_mode,
1407     high_length,
1408     high_offset,
1409     low_length,
1410     low_offset,
1411     protection_mode;
1412
1413   HANDLE
1414     file_handle,
1415     map_handle;
1416
1417   void
1418     *map;
1419
1420   (void) address;
1421   access_mode=0;
1422   file_handle=INVALID_HANDLE_VALUE;
1423   low_length=(DWORD) (length & 0xFFFFFFFFUL);
1424   high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1425   map_handle=INVALID_HANDLE_VALUE;
1426   map=(void *) NULL;
1427   low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1428   high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1429   protection_mode=0;
1430   if (protection & PROT_WRITE)
1431     {
1432       access_mode=FILE_MAP_WRITE;
1433       if (!(flags & MAP_PRIVATE))
1434         protection_mode=PAGE_READWRITE;
1435       else
1436         {
1437           access_mode=FILE_MAP_COPY;
1438           protection_mode=PAGE_WRITECOPY;
1439         }
1440     }
1441   else
1442     if (protection & PROT_READ)
1443       {
1444         access_mode=FILE_MAP_READ;
1445         protection_mode=PAGE_READONLY;
1446       }
1447   if ((file == -1) && (flags & MAP_ANONYMOUS))
1448     file_handle=INVALID_HANDLE_VALUE;
1449   else
1450     file_handle=(HANDLE) _get_osfhandle(file);
1451   map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1452     low_length,0);
1453   if (map_handle)
1454     {
1455       map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1456         length);
1457       CloseHandle(map_handle);
1458     }
1459   if (map == (void *) NULL)
1460     return((void *) ((char *) MAP_FAILED));
1461   return((void *) ((char *) map));
1462 }
1463 \f
1464 /*
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 %                                                                             %
1467 %                                                                             %
1468 %                                                                             %
1469 %   N T O p e n D i r e c t o r y                                             %
1470 %                                                                             %
1471 %                                                                             %
1472 %                                                                             %
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1474 %
1475 %  NTOpenDirectory() opens the directory named by filename and associates a
1476 %  directory stream with it.
1477 %
1478 %  The format of the NTOpenDirectory method is:
1479 %
1480 %      DIR *NTOpenDirectory(const char *path)
1481 %
1482 %  A description of each parameter follows:
1483 %
1484 %    o entry: Specifies a pointer to a DIR structure.
1485 %
1486 */
1487 MagickPrivate DIR *NTOpenDirectory(const char *path)
1488 {
1489   wchar_t
1490     file_specification[MaxTextExtent];
1491
1492   DIR
1493     *entry;
1494
1495   size_t
1496     length;
1497
1498   assert(path != (const char *) NULL);
1499   length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1500     MaxTextExtent);
1501   if (length == 0)
1502     return((DIR *) NULL);
1503   if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1504        MaxTextExtent) == (wchar_t*)NULL)
1505     return((DIR *) NULL);
1506   entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1507   if (entry != (DIR *) NULL)
1508     {
1509       entry->firsttime=TRUE;
1510       entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1511     }
1512   if (entry->hSearch == INVALID_HANDLE_VALUE)
1513     {
1514       if(wcsncat(file_specification,L"*.*",
1515         MaxTextExtent) == (wchar_t*)NULL)
1516         {
1517           entry=(DIR *) RelinquishMagickMemory(entry);
1518           return((DIR *) NULL);
1519         }
1520       entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1521       if (entry->hSearch == INVALID_HANDLE_VALUE)
1522         {
1523           entry=(DIR *) RelinquishMagickMemory(entry);
1524           return((DIR *) NULL);
1525         }
1526     }
1527   return(entry);
1528 }
1529 \f
1530 /*
1531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532 %                                                                             %
1533 %                                                                             %
1534 %                                                                             %
1535 %   N T O p e n L i b r a r y                                                 %
1536 %                                                                             %
1537 %                                                                             %
1538 %                                                                             %
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540 %
1541 %  NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1542 %  can be used to access the various procedures in the module.
1543 %
1544 %  The format of the NTOpenLibrary method is:
1545 %
1546 %      void *NTOpenLibrary(const char *filename)
1547 %
1548 %  A description of each parameter follows:
1549 %
1550 %    o path: Specifies a pointer to string representing dynamic module that
1551 %      is to be loaded.
1552 %
1553 */
1554
1555 static inline const char *GetSearchPath(void)
1556 {
1557 #if defined(MAGICKCORE_LTDL_DELEGATE)
1558   return(lt_dlgetsearchpath());
1559 #else
1560   return(lt_slsearchpath);
1561 #endif
1562 }
1563
1564 static UINT ChangeErrorMode(void)
1565 {
1566   typedef UINT
1567     (CALLBACK *GETERRORMODE)(void);
1568
1569   GETERRORMODE
1570     getErrorMode;
1571
1572   HMODULE
1573     handle;
1574
1575   UINT
1576     mode;
1577
1578   mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1579
1580   handle=GetModuleHandle("kernel32.dll");
1581   if (handle == (HMODULE) NULL)
1582     return SetErrorMode(mode);
1583
1584   getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1585   if (getErrorMode != (GETERRORMODE) NULL)
1586     mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1587
1588   return SetErrorMode(mode);
1589 }
1590
1591 MagickPrivate void *NTOpenLibrary(const char *filename)
1592 {
1593 #define MaxPathElements 31
1594
1595   char
1596     buffer[MaxTextExtent];
1597
1598   int
1599     index;
1600
1601   register const char
1602     *p,
1603     *q;
1604
1605   register int
1606     i;
1607
1608   UINT
1609     mode;
1610
1611   void
1612     *handle;
1613
1614   mode=ChangeErrorMode();
1615   handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1616   if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1617     {
1618       SetErrorMode(mode);
1619       return(handle);
1620     }
1621   p=(char *) GetSearchPath();
1622   index=0;
1623   while (index < MaxPathElements)
1624   {
1625     q=strchr(p,DirectoryListSeparator);
1626     if (q == (char *) NULL)
1627       {
1628         (void) CopyMagickString(buffer,p,MaxTextExtent);
1629         (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1630         (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1631         handle=(void *) LoadLibraryEx(buffer,NULL,
1632           LOAD_WITH_ALTERED_SEARCH_PATH);
1633         break;
1634       }
1635     i=q-p;
1636     (void) CopyMagickString(buffer,p,i+1);
1637     (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1638     (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1639     handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1640     if (handle != (void *) NULL)
1641       break;
1642     p=q+1;
1643   }
1644   SetErrorMode(mode);
1645   return(handle);
1646 }
1647 \f
1648 /*
1649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650 %                                                                             %
1651 %                                                                             %
1652 %                                                                             %
1653 %    N T R e a d D i r e c t o r y                                            %
1654 %                                                                             %
1655 %                                                                             %
1656 %                                                                             %
1657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658 %
1659 %  NTReadDirectory() returns a pointer to a structure representing the
1660 %  directory entry at the current position in the directory stream to which
1661 %  entry refers.
1662 %
1663 %  The format of the NTReadDirectory
1664 %
1665 %      NTReadDirectory(entry)
1666 %
1667 %  A description of each parameter follows:
1668 %
1669 %    o entry: Specifies a pointer to a DIR structure.
1670 %
1671 */
1672 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1673 {
1674   int
1675     status;
1676
1677   size_t
1678     length;
1679
1680   if (entry == (DIR *) NULL)
1681     return((struct dirent *) NULL);
1682   if (!entry->firsttime)
1683     {
1684       status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1685       if (status == 0)
1686         return((struct dirent *) NULL);
1687     }
1688   length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1689     entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1690   if (length == 0)
1691     return((struct dirent *) NULL);
1692   entry->firsttime=FALSE;
1693   entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1694   return(&entry->file_info);
1695 }
1696 \f
1697 /*
1698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1699 %                                                                             %
1700 %                                                                             %
1701 %                                                                             %
1702 %   N T R e g i s t r y K e y L o o k u p                                     %
1703 %                                                                             %
1704 %                                                                             %
1705 %                                                                             %
1706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1707 %
1708 %  NTRegistryKeyLookup() returns ImageMagick installation path settings
1709 %  stored in the Windows Registry.  Path settings are specific to the
1710 %  installed ImageMagick version so that multiple Image Magick installations
1711 %  may coexist.
1712 %
1713 %  Values are stored in the registry under a base path path similar to
1714 %  "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1715 %  "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1716 %  is appended to this base path to form the full key.
1717 %
1718 %  The format of the NTRegistryKeyLookup method is:
1719 %
1720 %      unsigned char *NTRegistryKeyLookup(const char *subkey)
1721 %
1722 %  A description of each parameter follows:
1723 %
1724 %    o subkey: Specifies a string that identifies the registry object.
1725 %      Currently supported sub-keys include: "BinPath", "ConfigurePath",
1726 %      "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1727 %
1728 */
1729 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1730 {
1731   char
1732     package_key[MaxTextExtent];
1733
1734   DWORD
1735     size,
1736     type;
1737
1738   HKEY
1739     registry_key;
1740
1741   LONG
1742     status;
1743
1744   unsigned char
1745     *value;
1746
1747   /*
1748     Look-up base key.
1749   */
1750   (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1751     MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1752   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1753   registry_key=(HKEY) INVALID_HANDLE_VALUE;
1754   status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1755   if (status != ERROR_SUCCESS)
1756     status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1757       &registry_key);
1758   if (status != ERROR_SUCCESS)
1759     {
1760       registry_key=(HKEY) INVALID_HANDLE_VALUE;
1761       return((unsigned char *) NULL);
1762     }
1763   /*
1764     Look-up sub key.
1765   */
1766   size=32;
1767   value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1768   if (value == (unsigned char *) NULL)
1769     {
1770       RegCloseKey(registry_key);
1771       return((unsigned char *) NULL);
1772     }
1773   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1774   status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1775   if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1776     {
1777       value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1778       if (value == (BYTE *) NULL)
1779         {
1780           RegCloseKey(registry_key);
1781           return((unsigned char *) NULL);
1782         }
1783       status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1784     }
1785   RegCloseKey(registry_key);
1786   if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1787     value=(unsigned char *) RelinquishMagickMemory(value);
1788   return((unsigned char *) value);
1789 }
1790 \f
1791 /*
1792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1793 %                                                                             %
1794 %                                                                             %
1795 %                                                                             %
1796 %   N T R e p o r t E v e n t                                                 %
1797 %                                                                             %
1798 %                                                                             %
1799 %                                                                             %
1800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1801 %
1802 %  NTReportEvent() reports an event.
1803 %
1804 %  The format of the NTReportEvent method is:
1805 %
1806 %      MagickBooleanType NTReportEvent(const char *event,
1807 %        const MagickBooleanType error)
1808 %
1809 %  A description of each parameter follows:
1810 %
1811 %    o event: the event.
1812 %
1813 %    o error: MagickTrue the event is an error.
1814 %
1815 */
1816 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1817   const MagickBooleanType error)
1818 {
1819   const char
1820     *events[1];
1821
1822   HANDLE
1823     handle;
1824
1825   WORD
1826     type;
1827
1828   handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1829   if (handle == NULL)
1830     return(MagickFalse);
1831   events[0]=event;
1832   type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1833   ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1834   DeregisterEventSource(handle);
1835   return(MagickTrue);
1836 }
1837 \f
1838 /*
1839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840 %                                                                             %
1841 %                                                                             %
1842 %                                                                             %
1843 %   N T R e s o u r c e T o B l o b                                           %
1844 %                                                                             %
1845 %                                                                             %
1846 %                                                                             %
1847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1848 %
1849 %  NTResourceToBlob() returns a blob containing the contents of the resource
1850 %  in the current executable specified by the id parameter. This currently
1851 %  used to retrieve MGK files tha have been embedded into the various command
1852 %  line utilities.
1853 %
1854 %  The format of the NTResourceToBlob method is:
1855 %
1856 %      unsigned char *NTResourceToBlob(const char *id)
1857 %
1858 %  A description of each parameter follows:
1859 %
1860 %    o id: Specifies a string that identifies the resource.
1861 %
1862 */
1863 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1864 {
1865
1866 #ifndef MAGICKCORE_LIBRARY_NAME
1867   char
1868     path[MaxTextExtent];
1869 #endif
1870
1871   DWORD
1872     length;
1873
1874   HGLOBAL
1875     global;
1876
1877   HMODULE
1878     handle;
1879
1880   HRSRC
1881     resource;
1882
1883   unsigned char
1884     *blob,
1885     *value;
1886
1887   assert(id != (const char *) NULL);
1888   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1889 #ifdef MAGICKCORE_LIBRARY_NAME
1890   handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1891 #else
1892   (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1893     DirectorySeparator,GetClientName());
1894   if (IsPathAccessible(path) != MagickFalse)
1895     handle=GetModuleHandle(path);
1896   else
1897     handle=GetModuleHandle(0);
1898 #endif
1899   if (!handle)
1900     return((unsigned char *) NULL);
1901   resource=FindResource(handle,id,"IMAGEMAGICK");
1902   if (!resource)
1903     return((unsigned char *) NULL);
1904   global=LoadResource(handle,resource);
1905   if (!global)
1906     return((unsigned char *) NULL);
1907   length=SizeofResource(handle,resource);
1908   value=(unsigned char *) LockResource(global);
1909   if (!value)
1910     {
1911       FreeResource(global);
1912       return((unsigned char *) NULL);
1913     }
1914   blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1915     sizeof(*blob));
1916   if (blob != (unsigned char *) NULL)
1917     {
1918       (void) CopyMagickMemory(blob,value,length);
1919       blob[length]='\0';
1920     }
1921   UnlockResource(global);
1922   FreeResource(global);
1923   return(blob);
1924 }
1925 \f
1926 /*
1927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1928 %                                                                             %
1929 %                                                                             %
1930 %                                                                             %
1931 %   N T S e e k D i r e c t o r y                                             %
1932 %                                                                             %
1933 %                                                                             %
1934 %                                                                             %
1935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1936 %
1937 %  NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1938 %  on the directory stream.
1939 %
1940 %  The format of the NTSeekDirectory method is:
1941 %
1942 %      void NTSeekDirectory(DIR *entry,ssize_t position)
1943 %
1944 %  A description of each parameter follows:
1945 %
1946 %    o entry: Specifies a pointer to a DIR structure.
1947 %
1948 %    o position: specifies the position associated with the directory
1949 %      stream.
1950 %
1951 */
1952 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1953 {
1954   (void) position;
1955   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1956   assert(entry != (DIR *) NULL);
1957 }
1958 \f
1959 /*
1960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1961 %                                                                             %
1962 %                                                                             %
1963 %                                                                             %
1964 %   N T S e t S e a r c h P a t h                                             %
1965 %                                                                             %
1966 %                                                                             %
1967 %                                                                             %
1968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1969 %
1970 %  NTSetSearchPath() sets the current locations that the subsystem should
1971 %  look at to find dynamically loadable modules.
1972 %
1973 %  The format of the NTSetSearchPath method is:
1974 %
1975 %      int NTSetSearchPath(const char *path)
1976 %
1977 %  A description of each parameter follows:
1978 %
1979 %    o path: Specifies a pointer to string representing the search path
1980 %      for DLL's that can be dynamically loaded.
1981 %
1982 */
1983 MagickPrivate int NTSetSearchPath(const char *path)
1984 {
1985 #if defined(MAGICKCORE_LTDL_DELEGATE)
1986   lt_dlsetsearchpath(path);
1987 #else
1988   if (lt_slsearchpath != (char *) NULL)
1989     lt_slsearchpath=DestroyString(lt_slsearchpath);
1990   if (path != (char *) NULL)
1991     lt_slsearchpath=AcquireString(path);
1992 #endif
1993   return(0);
1994 }
1995 \f
1996 /*
1997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1998 %                                                                             %
1999 %                                                                             %
2000 %                                                                             %
2001 +  N T S y n c M e m o r y                                                    %
2002 %                                                                             %
2003 %                                                                             %
2004 %                                                                             %
2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2006 %
2007 %  NTSyncMemory() emulates the Unix method of the same name.
2008 %
2009 %  The format of the NTSyncMemory method is:
2010 %
2011 %      int NTSyncMemory(void *address,size_t length,int flags)
2012 %
2013 %  A description of each parameter follows:
2014 %
2015 %    o address: the address of the binary large object.
2016 %
2017 %    o length: the length of the binary large object.
2018 %
2019 %    o flags: Option flags (ignored for Windows).
2020 %
2021 */
2022 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2023 {
2024   (void) flags;
2025   if (FlushViewOfFile(address,length) == MagickFalse)
2026     return(-1);
2027   return(0);
2028 }
2029 \f
2030 /*
2031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032 %                                                                             %
2033 %                                                                             %
2034 %                                                                             %
2035 %   N T S y s t e m C o m m a n d                                             %
2036 %                                                                             %
2037 %                                                                             %
2038 %                                                                             %
2039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2040 %
2041 %  NTSystemCommand() executes the specified command and waits until it
2042 %  terminates.  The returned value is the exit status of the command.
2043 %
2044 %  The format of the NTSystemCommand method is:
2045 %
2046 %      int NTSystemCommand(MagickFalse,const char *command)
2047 %
2048 %  A description of each parameter follows:
2049 %
2050 %    o command: This string is the command to execute.
2051 %
2052 */
2053 MagickPrivate int NTSystemCommand(const char *command)
2054 {
2055   char
2056     local_command[MaxTextExtent];
2057
2058   DWORD
2059     child_status;
2060
2061   int
2062     status;
2063
2064   MagickBooleanType
2065     background_process;
2066
2067   PROCESS_INFORMATION
2068     process_info;
2069
2070   STARTUPINFO
2071     startup_info;
2072
2073   if (command == (char *) NULL)
2074     return(-1);
2075   GetStartupInfo(&startup_info);
2076   startup_info.dwFlags=STARTF_USESHOWWINDOW;
2077   startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2078   (void) CopyMagickString(local_command,command,MaxTextExtent);
2079   background_process=command[strlen(command)-1] == '&' ? MagickTrue :
2080     MagickFalse;
2081   if (background_process != MagickFalse)
2082     local_command[strlen(command)-1]='\0';
2083   if (command[strlen(command)-1] == '|')
2084     local_command[strlen(command)-1]='\0';
2085   else
2086     startup_info.wShowWindow=SW_HIDE;
2087   status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2088     NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2089     NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2090     &process_info);
2091   if (status == 0)
2092     return(-1);
2093   if (background_process != MagickFalse)
2094     return(status == 0);
2095   status=WaitForSingleObject(process_info.hProcess,INFINITE);
2096   if (status != WAIT_OBJECT_0)
2097     return(status);
2098   status=GetExitCodeProcess(process_info.hProcess,&child_status);
2099   if (status == 0)
2100     return(-1);
2101   CloseHandle(process_info.hProcess);
2102   CloseHandle(process_info.hThread);
2103   return((int) child_status);
2104 }
2105 \f
2106 /*
2107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2108 %                                                                             %
2109 %                                                                             %
2110 %                                                                             %
2111 %   N T S y s t e m C o n i f i g u r a t i o n                               %
2112 %                                                                             %
2113 %                                                                             %
2114 %                                                                             %
2115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2116 %
2117 %  NTSystemConfiguration() provides a way for the application to determine
2118 %  values for system limits or options at runtime.
2119 %
2120 %  The format of the exit method is:
2121 %
2122 %      ssize_t NTSystemConfiguration(int name)
2123 %
2124 %  A description of each parameter follows:
2125 %
2126 %    o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2127 %
2128 */
2129 MagickPrivate ssize_t NTSystemConfiguration(int name)
2130 {
2131   switch (name)
2132   {
2133     case _SC_PAGESIZE:
2134     {
2135       SYSTEM_INFO
2136         system_info;
2137
2138       GetSystemInfo(&system_info);
2139       return(system_info.dwPageSize);
2140     }
2141     case _SC_PHYS_PAGES:
2142     {
2143       HMODULE
2144         handle;
2145
2146       LPFNDLLFUNC2
2147         module;
2148
2149       NTMEMORYSTATUSEX
2150         status;
2151
2152       SYSTEM_INFO
2153         system_info;
2154
2155       handle=GetModuleHandle("kernel32.dll");
2156       if (handle == (HMODULE) NULL)
2157         return(0L);
2158       GetSystemInfo(&system_info);
2159       module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2160       if (module == (LPFNDLLFUNC2) NULL)
2161         {
2162           MEMORYSTATUS
2163             status;
2164
2165           GlobalMemoryStatus(&status);
2166           return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2167         }
2168       status.dwLength=sizeof(status);
2169       if (module(&status) == 0)
2170         return(0L);
2171       return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2172     }
2173     case _SC_OPEN_MAX:
2174       return(2048);
2175     default:
2176       break;
2177   }
2178   return(-1);
2179 }
2180 \f
2181 /*
2182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2183 %                                                                             %
2184 %                                                                             %
2185 %                                                                             %
2186 %   N T T e l l D i r e c t o r y                                             %
2187 %                                                                             %
2188 %                                                                             %
2189 %                                                                             %
2190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2191 %
2192 %  NTTellDirectory() returns the current location associated with the named
2193 %  directory stream.
2194 %
2195 %  The format of the NTTellDirectory method is:
2196 %
2197 %      ssize_t NTTellDirectory(DIR *entry)
2198 %
2199 %  A description of each parameter follows:
2200 %
2201 %    o entry: Specifies a pointer to a DIR structure.
2202 %
2203 */
2204 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2205 {
2206   assert(entry != (DIR *) NULL);
2207   return(0);
2208 }
2209 \f
2210 /*
2211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212 %                                                                             %
2213 %                                                                             %
2214 %                                                                             %
2215 %   N T T r u n c a t e F i l e                                               %
2216 %                                                                             %
2217 %                                                                             %
2218 %                                                                             %
2219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220 %
2221 %  NTTruncateFile() truncates a file to a specified length.
2222 %
2223 %  The format of the NTTruncateFile method is:
2224 %
2225 %      int NTTruncateFile(int file,off_t length)
2226 %
2227 %  A description of each parameter follows:
2228 %
2229 %    o file: the file.
2230 %
2231 %    o length: the file length.
2232 %
2233 */
2234 MagickPrivate int NTTruncateFile(int file,off_t length)
2235 {
2236   DWORD
2237     file_pointer;
2238
2239   HANDLE
2240     file_handle;
2241
2242   long
2243     high,
2244     low;
2245
2246   file_handle=(HANDLE)_get_osfhandle(file);
2247   if (file_handle == (HANDLE)-1L)
2248     return(-1);
2249   low=(long) (length & 0xffffffffUL);
2250   high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2251   file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2252   if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2253     return(-1);
2254   if (SetEndOfFile(file_handle) == 0)
2255     return(-1);
2256   return(0);
2257 }
2258 \f
2259 /*
2260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2261 %                                                                             %
2262 %                                                                             %
2263 %                                                                             %
2264 +  N T U n m a p M e m o r y                                                  %
2265 %                                                                             %
2266 %                                                                             %
2267 %                                                                             %
2268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2269 %
2270 %  NTUnmapMemory() emulates the Unix munmap method.
2271 %
2272 %  The format of the NTUnmapMemory method is:
2273 %
2274 %      int NTUnmapMemory(void *map,size_t length)
2275 %
2276 %  A description of each parameter follows:
2277 %
2278 %    o map: the address of the binary large object.
2279 %
2280 %    o length: the length of the binary large object.
2281 %
2282 */
2283 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2284 {
2285   (void) length;
2286   if (UnmapViewOfFile(map) == 0)
2287     return(-1);
2288   return(0);
2289 }
2290 \f
2291 /*
2292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293 %                                                                             %
2294 %                                                                             %
2295 %                                                                             %
2296 %   N T U s e r T i m e                                                       %
2297 %                                                                             %
2298 %                                                                             %
2299 %                                                                             %
2300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2301 %
2302 %  NTUserTime() returns the total time the process has been scheduled (e.g.
2303 %  seconds) since the last call to StartTimer().
2304 %
2305 %  The format of the UserTime method is:
2306 %
2307 %      double NTUserTime(void)
2308 %
2309 */
2310 MagickPrivate double NTUserTime(void)
2311 {
2312   DWORD
2313     status;
2314
2315   FILETIME
2316     create_time,
2317     exit_time;
2318
2319   OSVERSIONINFO
2320     OsVersionInfo;
2321
2322   union
2323   {
2324     FILETIME
2325       filetime;
2326
2327     __int64
2328       filetime64;
2329   } kernel_time;
2330
2331   union
2332   {
2333     FILETIME
2334       filetime;
2335
2336     __int64
2337       filetime64;
2338   } user_time;
2339
2340   OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2341   GetVersionEx(&OsVersionInfo);
2342   if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2343     return(NTElapsedTime());
2344   status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2345     &kernel_time.filetime,&user_time.filetime);
2346   if (status != TRUE)
2347     return(0.0);
2348   return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2349 }
2350 \f
2351 /*
2352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2353 %                                                                             %
2354 %                                                                             %
2355 %                                                                             %
2356 %   N T W a r n i n g H a n d l e r                                           %
2357 %                                                                             %
2358 %                                                                             %
2359 %                                                                             %
2360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2361 %
2362 %  NTWarningHandler() displays a warning reason.
2363 %
2364 %  The format of the NTWarningHandler method is:
2365 %
2366 %      void NTWarningHandler(const ExceptionType severity,const char *reason,
2367 %        const char *description)
2368 %
2369 %  A description of each parameter follows:
2370 %
2371 %    o severity: Specifies the numeric warning category.
2372 %
2373 %    o reason: Specifies the reason to display before terminating the
2374 %      program.
2375 %
2376 %    o description: Specifies any description to the reason.
2377 %
2378 */
2379 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2380   const char *reason,const char *description)
2381 {
2382   char
2383     buffer[2*MaxTextExtent];
2384
2385   (void) severity;
2386   if (reason == (char *) NULL)
2387     return;
2388   if (description == (char *) NULL)
2389     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2390       reason);
2391   else
2392     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2393       GetClientName(),reason,description);
2394   (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2395     MB_SETFOREGROUND | MB_ICONINFORMATION);
2396 }
2397 #endif