WizardsToolkit  1.0.7
nt-base.c
Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                                 N   N  TTTTT                                %
00007 %                                 NN  N    T                                  %
00008 %                                 N N N    T                                  %
00009 %                                 N  NN    T                                  %
00010 %                                 N   N    T                                  %
00011 %                                                                             %
00012 %                                                                             %
00013 %             Windows NT Utility Methods for the Wizard's Toolkit             %
00014 %                                                                             %
00015 %                               Software Design                               %
00016 %                                 John Cristy                                 %
00017 %                                December 1996                                %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.wizards-toolkit.org/script/license.php                        %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 */
00038 /*
00039   Include declarations.
00040 */
00041 #include "wizard/studio.h"
00042 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00043 #include "wizard/client.h"
00044 #include "wizard/locale_.h"
00045 #include "wizard/log.h"
00046 #include "wizard/memory_.h"
00047 #include "wizard/resource_.h"
00048 #include "wizard/string_.h"
00049 #include "wizard/timer.h"
00050 #include "wizard/utility.h"
00051 #include "wizard/wizard.h"
00052 #include "wizard/version.h"
00053 #if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
00054 #  include "ltdl.h"
00055 #endif /* defined(WIZARDSTOOLKIT_LTDL_DELEGATE) */
00056 #include "wizard/nt-base.h"
00057 #include <NTSecAPI.h>
00058 #include <wincrypt.h>
00059 
00060 /*
00061   Define declarations.
00062 */
00063 #if !defined(MAP_FAILED)
00064 #define MAP_FAILED      ((void *) -1)
00065 #endif
00066 
00067 /*
00068   Static declarations.
00069 */
00070 #if !defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
00071 static char
00072   *lt_slsearchpath = (char *) NULL;
00073 #endif
00074 
00075 /*
00076   External declarations.
00077 */
00078 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00079 extern "C" BOOL WINAPI
00080   DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
00081 #endif
00082 
00083 /*
00084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00085 %                                                                             %
00086 %                                                                             %
00087 %                                                                             %
00088 %   D l l M a i n                                                             %
00089 %                                                                             %
00090 %                                                                             %
00091 %                                                                             %
00092 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00093 %
00094 %  DllMain() is an entry point to the DLL which is called when processes and
00095 %  threads are initialized and terminated, or upon calls to the Windows
00096 %  LoadLibrary and FreeLibrary functions.
00097 %
00098 %  The function returns TRUE of it succeeds, or FALSE if initialization fails.
00099 %
00100 %  The format of the DllMain method is:
00101 %
00102 %    BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
00103 %
00104 %  A description of each parameter follows:
00105 %
00106 %    o handle: handle to the DLL module
00107 %
00108 %    o reason: reason for calling function:
00109 %
00110 %      DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
00111 %                           space of current process.
00112 %      DLL_THREAD_ATTACH - Indicates that the current process is
00113 %                          creating a new thread.  Called under the
00114 %                          context of the new thread.
00115 %      DLL_THREAD_DETACH - Indicates that the thread is exiting.
00116 %                          Called under the context of the exiting
00117 %                          thread.
00118 %      DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
00119 %                           from the virtual address space of the
00120 %                           current process.
00121 %
00122 %    o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
00123 %                   and DLL_PROCESS_DETACH.
00124 %
00125 */
00126 #if defined(_DLL) && defined( ProvideDllMain )
00127 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
00128 {
00129   switch (reason)
00130   {
00131     case DLL_PROCESS_ATTACH:
00132     {
00133       char
00134         *module_path,
00135         *new_path,
00136         *path;
00137 
00138       ssize_t
00139         count;
00140 
00141       module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
00142         sizeof(*module_path));
00143       new_path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
00144         sizeof(*new_path));
00145       path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
00146       if ((module_path == (char *) NULL) || (new_path == (char *) NULL) ||
00147           (path == (char *) NULL))
00148         return(FALSE);
00149       count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
00150       if (count != 0)
00151         {
00152           for ( ; count > 0; count--)
00153             if (module_path[count] == '\\')
00154               {
00155                 module_path[count+1]='\0';
00156                 break;
00157               }
00158           WizardsToolkitGenesis(module_path);
00159           count=GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
00160           if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
00161             {
00162               if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
00163                 {
00164                   (void) FormatLocaleString(new_path,16*MaxTextExtent,
00165                     "%s;%s",module_path,path);
00166                   SetEnvironmentVariable("PATH",new_path);
00167                 }
00168             }
00169         }
00170       path=(char *) RelinquishWizardMemory(path);
00171       new_path=(char *) RelinquishWizardMemory(new_path);
00172       module_path=(char *) RelinquishWizardMemory(module_path);
00173       break;
00174     }
00175     case DLL_PROCESS_DETACH:
00176     {
00177       WizardsToolkitTerminus();
00178       break;
00179     }
00180     default:
00181       break;
00182   }
00183   return(TRUE);
00184 }
00185 #endif
00186 
00187 /*
00188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00189 %                                                                             %
00190 %                                                                             %
00191 %                                                                             %
00192 %   E x i t                                                                   %
00193 %                                                                             %
00194 %                                                                             %
00195 %                                                                             %
00196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00197 %
00198 %  Exit() calls TerminateProcess for Win95.
00199 %
00200 %  The format of the exit method is:
00201 %
00202 %      int Exit(int status)
00203 %
00204 %  A description of each parameter follows:
00205 %
00206 %    o status: an integer value representing the status of the terminating
00207 %      process.
00208 %
00209 %
00210 */
00211 WizardExport int Exit(int status)
00212 {
00213   if (IsWindows95())
00214     TerminateProcess(GetCurrentProcess(),(unsigned int) status);
00215   exit(status);
00216   return(0);
00217 }
00218 
00219 #if !defined(__MINGW32__)
00220 /*
00221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00222 %                                                                             %
00223 %                                                                             %
00224 %                                                                             %
00225 %   g e t t i m e o f d a y                                                   %
00226 %                                                                             %
00227 %                                                                             %
00228 %                                                                             %
00229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00230 %
00231 %  The gettimeofday() method get the time of day.
00232 %
00233 %  The format of the gettimeofday method is:
00234 %
00235 %      int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
00236 %
00237 %  A description of each parameter follows:
00238 %
00239 %    o time_value: time value.
00240 %
00241 %    o time_zone: time zone.
00242 %
00243 */
00244 WizardExport int gettimeofday (struct timeval *time_value,
00245   struct timezone *time_zone)
00246 {
00247 #define EpochFiletime  WizardLLConstant(116444736000000000)
00248 
00249   static int
00250     is_tz_set;
00251 
00252   if (time_value != (struct timeval *) NULL)
00253     {
00254       FILETIME
00255         file_time;
00256 
00257       __int64
00258         time;
00259 
00260       LARGE_INTEGER
00261         date_time;
00262 
00263       GetSystemTimeAsFileTime(&file_time);
00264       date_time.LowPart=file_time.dwLowDateTime;
00265       date_time.HighPart=file_time.dwHighDateTime;
00266       time=date_time.QuadPart;
00267       time-=EpochFiletime;
00268       time/=10;
00269       time_value->tv_sec=(ssize_t) (time / 1000000);
00270       time_value->tv_usec=(ssize_t) (time % 1000000);
00271     }
00272   if (time_zone != (struct timezone *) NULL)
00273     {
00274       if (is_tz_set == 0)
00275         {
00276           _tzset();
00277           is_tz_set++;
00278         }
00279       time_zone->tz_minuteswest=_timezone/60;
00280       time_zone->tz_dsttime=_daylight;
00281     }
00282   return(0);
00283 }
00284 #endif
00285 
00286 /*
00287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00288 %                                                                             %
00289 %                                                                             %
00290 %                                                                             %
00291 %   I s W i n d o w s 9 5                                                     %
00292 %                                                                             %
00293 %                                                                             %
00294 %                                                                             %
00295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00296 %
00297 %  IsWindows95() returns true if the system is Windows 95.
00298 %
00299 %  The format of the IsWindows95 method is:
00300 %
00301 %      int IsWindows95()
00302 %
00303 %
00304 */
00305 WizardExport int IsWindows95()
00306 {
00307   OSVERSIONINFO
00308     version_info;
00309 
00310   version_info.dwOSVersionInfoSize=sizeof(version_info);
00311   if (GetVersionEx(&version_info) &&
00312       (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
00313     return(1);
00314   return(0);
00315 }
00316 
00317 /*
00318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00319 %                                                                             %
00320 %                                                                             %
00321 %                                                                             %
00322 %   N T C l o s e D i r e c t o r y                                           %
00323 %                                                                             %
00324 %                                                                             %
00325 %                                                                             %
00326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00327 %
00328 %  NTCloseDirectory() closes the named directory stream and frees the DIR
00329 %  structure.
00330 %
00331 %  The format of the NTCloseDirectory method is:
00332 %
00333 %      int NTCloseDirectory(DIR *entry)
00334 %
00335 %  A description of each parameter follows:
00336 %
00337 %    o entry: Specifies a pointer to a DIR structure.
00338 %
00339 */
00340 WizardExport int NTCloseDirectory(DIR *entry)
00341 {
00342   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00343   assert(entry != (DIR *) NULL);
00344   FindClose(entry->hSearch);
00345   entry=(DIR *) RelinquishWizardMemory(entry);
00346   return(0);
00347 }
00348 
00349 /*
00350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00351 %                                                                             %
00352 %                                                                             %
00353 %                                                                             %
00354 %   N T C l o s e L i b r a r y                                               %
00355 %                                                                             %
00356 %                                                                             %
00357 %                                                                             %
00358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00359 %
00360 %  NTCloseLibrary() unloads the module associated with the passed handle.
00361 %
00362 %  The format of the NTCloseLibrary method is:
00363 %
00364 %      void NTCloseLibrary(void *handle)
00365 %
00366 %  A description of each parameter follows:
00367 %
00368 %    o handle: Specifies a handle to a previously loaded dynamic module.
00369 %
00370 */
00371 WizardExport int NTCloseLibrary(void *handle)
00372 {
00373   if (IsWindows95())
00374     return(FreeLibrary(handle));
00375   return(!(FreeLibrary(handle)));
00376 }
00377 
00378 /*
00379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00380 %                                                                             %
00381 %                                                                             %
00382 %                                                                             %
00383 %   N T C o n t r o l H a n d l e r                                           %
00384 %                                                                             %
00385 %                                                                             %
00386 %                                                                             %
00387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00388 %
00389 %  NTControlHandler() registers a control handler that is activated when, for
00390 %  example, a ctrl-c is received.
00391 %
00392 %  The format of the NTControlHandler method is:
00393 %
00394 %      int NTControlHandler(void)
00395 %
00396 */
00397 
00398 static BOOL ControlHandler(DWORD type)
00399 {
00400   AsynchronousResourceComponentTerminus();
00401   return(FALSE);
00402 }
00403 
00404 WizardExport int NTControlHandler(void)
00405 {
00406   return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
00407 }
00408 
00409 /*
00410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00411 %                                                                             %
00412 %                                                                             %
00413 %                                                                             %
00414 %   N T E l a p s e d T i m e                                                 %
00415 %                                                                             %
00416 %                                                                             %
00417 %                                                                             %
00418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00419 %
00420 %  NTElapsedTime() returns the elapsed time (in seconds) since the last call to
00421 %  StartTimer().
00422 %
00423 %  The format of the ElapsedTime method is:
00424 %
00425 %      double NTElapsedTime(void)
00426 %
00427 */
00428 WizardExport double NTElapsedTime(void)
00429 {
00430   union
00431   {
00432     FILETIME
00433       filetime;
00434 
00435     __int64
00436       filetime64;
00437   } elapsed_time;
00438 
00439   SYSTEMTIME
00440     system_time;
00441 
00442   GetSystemTime(&system_time);
00443   SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
00444   return((double) 1.0e-7*elapsed_time.filetime64);
00445 }
00446 
00447 /*
00448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00449 %                                                                             %
00450 %                                                                             %
00451 %                                                                             %
00452 +   N T E r r o r H a n d l e r                                               %
00453 %                                                                             %
00454 %                                                                             %
00455 %                                                                             %
00456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00457 %
00458 %  NTErrorHandler() displays an error reason and then terminates the program.
00459 %
00460 %  The format of the NTErrorHandler method is:
00461 %
00462 %      void NTErrorHandler(const ExceptionType error,const char *reason,
00463 %        const char *description)
00464 %
00465 %  A description of each parameter follows:
00466 %
00467 %    o error: Specifies the numeric error category.
00468 %
00469 %    o reason: Specifies the reason to display before terminating the
00470 %      program.
00471 %
00472 %    o description: Specifies any description to the reason.
00473 %
00474 */
00475 WizardExport void NTErrorHandler(const ExceptionType error,const char *reason,
00476   const char *description)
00477 {
00478   char
00479     buffer[3*MaxTextExtent],
00480     *message;
00481 
00482   if (reason == (char *) NULL)
00483     {
00484       WizardsToolkitTerminus();
00485       exit(0);
00486     }
00487   message=GetExceptionMessage(errno);
00488   if ((description != (char *) NULL) && errno)
00489     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
00490       GetClientName(),reason,description,message);
00491   else
00492     if (description != (char *) NULL)
00493       (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
00494         GetClientName(),reason,description);
00495     else
00496       if (errno)
00497         (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
00498           GetClientName(),reason,message);
00499       else
00500         (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
00501           GetClientName(),reason);
00502   message=(char *) RelinquishWizardMemory(message);
00503   (void) MessageBox(NULL,buffer,"Wizard's Toolkit Exception",MB_OK |
00504     MB_TASKMODAL | MB_SETFOREGROUND | MB_ICONEXCLAMATION);
00505   WizardsToolkitTerminus();
00506   exit(0);
00507 }
00508 
00509 /*
00510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00511 %                                                                             %
00512 %                                                                             %
00513 %                                                                             %
00514 %   N T E x i t L i b r a r y                                                 %
00515 %                                                                             %
00516 %                                                                             %
00517 %                                                                             %
00518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00519 %
00520 %  NTExitLibrary() exits the dynamic module loading subsystem.
00521 %
00522 %  The format of the NTExitLibrary method is:
00523 %
00524 %      int NTExitLibrary(void)
00525 %
00526 */
00527 WizardExport int NTExitLibrary(void)
00528 {
00529   return(0);
00530 }
00531 
00532 /*
00533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00534 %                                                                             %
00535 %                                                                             %
00536 %                                                                             %
00537 %   N T F i l e T r u n c a t e                                               %
00538 %                                                                             %
00539 %                                                                             %
00540 %                                                                             %
00541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00542 %
00543 %  NTFileTruncate() truncates a file to a specified length.
00544 %
00545 %  The format of the NTFileTruncate method is:
00546 %
00547 %      int NTFileTruncate(int file,off_t length)
00548 %
00549 %  A description of each parameter follows:
00550 %
00551 %    o file: The file.
00552 %
00553 %    o length: The file length.
00554 %
00555 */
00556 WizardExport int NTFileTruncate(int file,off_t length)
00557 {
00558   DWORD
00559     file_pointer;
00560 
00561   long
00562     file_handle,
00563     high,
00564     low;
00565 
00566   file_handle=_get_osfhandle(file);
00567   if (file_handle == -1L)
00568     return(-1);
00569   low=(long) (length & 0xffffffffUL);
00570   high=(long) ((((WizardOffsetType) length) >> 32) & 0xffffffffUL);
00571   file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
00572   if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
00573     return(-1);
00574   if (SetEndOfFile((HANDLE) file_handle) == 0)
00575     return(-1);
00576   return(0);
00577 }
00578 
00579 /*
00580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00581 %                                                                             %
00582 %                                                                             %
00583 %                                                                             %
00584 %   N T G e t E x e c u t i o n P a t h                                       %
00585 %                                                                             %
00586 %                                                                             %
00587 %                                                                             %
00588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00589 %
00590 %  NTGetExecutionPath() returns the execution path of a program.
00591 %
00592 %  The format of the GetExecutionPath method is:
00593 %
00594 %      WizardBooleanType NTGetExecutionPath(char *path,const size_t extent)
00595 %
00596 %  A description of each parameter follows:
00597 %
00598 %    o path: the pathname of the executable that started the process.
00599 %
00600 %    o extent: the maximum extent of the path.
00601 %
00602 */
00603 WizardExport WizardBooleanType NTGetExecutionPath(char *path,
00604   const size_t extent)
00605 {
00606   GetModuleFileName(0,path,(DWORD) extent);
00607   return(WizardTrue);
00608 }
00609 
00610 /*
00611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00612 %                                                                             %
00613 %                                                                             %
00614 %                                                                             %
00615 %   N T G e t L a s t E r r o r                                               %
00616 %                                                                             %
00617 %                                                                             %
00618 %                                                                             %
00619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00620 %
00621 %  NTGetLastError() returns the last error that occurred.
00622 %
00623 %  The format of the NTGetLastError method is:
00624 %
00625 %      char *NTGetLastError(void)
00626 %
00627 */
00628 char *NTGetLastError(void)
00629 {
00630   char
00631     *reason;
00632 
00633   int
00634     status;
00635 
00636   LPVOID
00637     buffer;
00638 
00639   status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00640     FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
00641     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
00642   if (!status)
00643     reason=AcquireString("An unknown error occurred");
00644   else
00645     {
00646       reason=AcquireString((const char *) buffer);
00647       LocalFree(buffer);
00648     }
00649   return(reason);
00650 }
00651 
00652 /*
00653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00654 %                                                                             %
00655 %                                                                             %
00656 %                                                                             %
00657 %   N T G e t L i b r a r y E r r o r                                         %
00658 %                                                                             %
00659 %                                                                             %
00660 %                                                                             %
00661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00662 %
00663 %  Lt_dlerror() returns a pointer to a string describing the last error
00664 %  associated with a lt_dl method.  Note that this function is not thread
00665 %  safe so it should only be used under the protection of a lock.
00666 %
00667 %  The format of the NTGetLibraryError method is:
00668 %
00669 %      const char *NTGetLibraryError(void)
00670 %
00671 */
00672 WizardExport const char *NTGetLibraryError(void)
00673 {
00674   static char
00675     last_error[MaxTextExtent];
00676 
00677   char
00678     *error;
00679 
00680   *last_error='\0';
00681   error=NTGetLastError();
00682   if (error)
00683     (void) CopyWizardString(last_error,error,MaxTextExtent);
00684   error=(char *) RelinquishWizardMemory(error);
00685   return(last_error);
00686 }
00687 
00688 /*
00689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00690 %                                                                             %
00691 %                                                                             %
00692 %                                                                             %
00693 %   N T G e t L i b r a r y S y m b o l                                       %
00694 %                                                                             %
00695 %                                                                             %
00696 %                                                                             %
00697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00698 %
00699 %  NTGetLibrarySymbol() retrieve the procedure address of the method
00700 %  specified by the passed character string.
00701 %
00702 %  The format of the NTGetLibrarySymbol method is:
00703 %
00704 %      void *NTGetLibrarySymbol(void *handle,const char *name)
00705 %
00706 %  A description of each parameter follows:
00707 %
00708 %    o handle: Specifies a handle to the previously loaded dynamic module.
00709 %
00710 %    o name: Specifies the procedure entry point to be returned.
00711 %
00712 */
00713 void *NTGetLibrarySymbol(void *handle,const char *name)
00714 {
00715   LPFNDLLFUNC1
00716     lpfnDllFunc1;
00717 
00718   lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress(handle,name);
00719   if (!lpfnDllFunc1)
00720     return((void *) NULL);
00721   return((void *) lpfnDllFunc1);
00722 }
00723 
00724 /*
00725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00726 %                                                                             %
00727 %                                                                             %
00728 %                                                                             %
00729 %   N T G e t M o d u l e P a t h                                             %
00730 %                                                                             %
00731 %                                                                             %
00732 %                                                                             %
00733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00734 %
00735 %  NTGetModulePath() returns the path of the specified module.
00736 %
00737 %  The format of the GetModulePath method is:
00738 %
00739 %      WizardBooleanType NTGetModulePath(const char *module,char *path)
00740 %
00741 %  A description of each parameter follows:
00742 %
00743 %    module: The module name.
00744 %
00745 %    path: The module path is returned here.
00746 %
00747 */
00748 WizardExport WizardBooleanType NTGetModulePath(const char *module,char *path)
00749 {
00750   char
00751     module_path[MaxTextExtent];
00752 
00753   HMODULE
00754     handle;
00755 
00756   ssize_t
00757     length;
00758 
00759   *path='\0';
00760   handle=GetModuleHandle(module);
00761   if (handle == (HMODULE) NULL)
00762     return(WizardFalse);
00763   length=GetModuleFileName(handle,module_path,MaxTextExtent);
00764   if (length != 0)
00765     GetPathComponent(module_path,HeadPath,path);
00766   return(WizardTrue);
00767 }
00768 
00769 /*
00770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00771 %                                                                             %
00772 %                                                                             %
00773 %                                                                             %
00774 %   N T G a t h e r R a n d o m D a t a                                       %
00775 %                                                                             %
00776 %                                                                             %
00777 %                                                                             %
00778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00779 %
00780 %  NTGatherRandomData() gathers random data and returns it.
00781 %
00782 %  The format of the GatherRandomData method is:
00783 %
00784 %      WizardBooleanType NTGatherRandomData(const size_t length,
00785 %        unsigned char *random)
00786 %
00787 %  A description of each parameter follows:
00788 %
00789 %    length: the length of random data buffer
00790 %
00791 %    random: the random data is returned here.
00792 %
00793 */
00794 WizardExport WizardBooleanType NTGatherRandomData(const size_t length,
00795   unsigned char *random)
00796 {
00797   HCRYPTPROV
00798     handle;
00799 
00800   int
00801     status;
00802 
00803   handle=(HCRYPTPROV) NULL;
00804   status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
00805     (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
00806   if (status == 0)
00807     status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
00808       (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
00809   if (status == 0)
00810     return(WizardFalse);
00811   status=CryptGenRandom(handle,(DWORD) length,random);
00812   if (status == 0)
00813     {
00814       status=CryptReleaseContext(handle,0);
00815       return(WizardFalse);
00816     }
00817   status=CryptReleaseContext(handle,0);
00818   if (status == 0)
00819     return(WizardFalse);
00820   return(WizardTrue);
00821 }
00822 
00823 /*
00824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00825 %                                                                             %
00826 %                                                                             %
00827 %                                                                             %
00828 %   N T I n i t i a l i z e L i b r a r y                                     %
00829 %                                                                             %
00830 %                                                                             %
00831 %                                                                             %
00832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00833 %
00834 %  NTInitializeLibrary() initializes the dynamic module loading subsystem.
00835 %
00836 %  The format of the NTInitializeLibrary method is:
00837 %
00838 %      int NTInitializeLibrary(void)
00839 %
00840 */
00841 WizardExport int NTInitializeLibrary(void)
00842 {
00843   return(0);
00844 }
00845 
00846 /*
00847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00848 %                                                                             %
00849 %                                                                             %
00850 %                                                                             %
00851 +  N T M a p M e m o r y                                                      %
00852 %                                                                             %
00853 %                                                                             %
00854 %                                                                             %
00855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00856 %
00857 %  Mmap() emulates the Unix method of the same name.
00858 %
00859 %  The format of the NTMapMemory method is:
00860 %
00861 %    WizardExport void *NTMapMemory(char *address,size_t length,int protection,
00862 %      int access,int file,WizardOffsetType offset)
00863 %
00864 */
00865 WizardExport void *NTMapMemory(char *address,size_t length,int protection,
00866   int flags,int file,WizardOffsetType offset)
00867 {
00868   DWORD
00869     access_mode,
00870     high_length,
00871     high_offset,
00872     low_length,
00873     low_offset,
00874     protection_mode;
00875 
00876   HANDLE
00877     file_handle,
00878     map_handle;
00879 
00880   void
00881     *map;
00882 
00883   access_mode=0;
00884   file_handle=INVALID_HANDLE_VALUE;
00885   low_length=(DWORD) (length & 0xFFFFFFFFUL);
00886   high_length=(DWORD) ((((WizardOffsetType) length) >> 32) & 0xFFFFFFFFUL);
00887   map_handle=INVALID_HANDLE_VALUE;
00888   map=(void *) NULL;
00889   low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
00890   high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
00891   protection_mode=0;
00892   if (protection & PROT_WRITE)
00893     {
00894       access_mode=FILE_MAP_WRITE;
00895       if (!(flags & MAP_PRIVATE))
00896         protection_mode=PAGE_READWRITE;
00897       else
00898         {
00899           access_mode=FILE_MAP_COPY;
00900           protection_mode=PAGE_WRITECOPY;
00901         }
00902     }
00903   else
00904     if (protection & PROT_READ)
00905       {
00906         access_mode=FILE_MAP_READ;
00907         protection_mode=PAGE_READONLY;
00908       }
00909   if ((file == -1) && (flags & MAP_ANONYMOUS))
00910     file_handle=INVALID_HANDLE_VALUE;
00911   else
00912     file_handle=(HANDLE) _get_osfhandle(file);
00913   map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
00914     low_length,0);
00915   if (map_handle)
00916     {
00917       map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
00918         length);
00919       CloseHandle(map_handle);
00920     }
00921   if (map == (void *) NULL)
00922     return((void *) MAP_FAILED);
00923   return((void *) ((char *) map));
00924 }
00925 
00926 /*
00927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00928 %                                                                             %
00929 %                                                                             %
00930 %                                                                             %
00931 %   N T O p e n D i r e c t o r y                                             %
00932 %                                                                             %
00933 %                                                                             %
00934 %                                                                             %
00935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00936 %
00937 %  NTOpenDirectory() opens the directory named by filename and associates a
00938 %  directory stream with it.
00939 %
00940 %  The format of the NTOpenDirectory method is:
00941 %
00942 %      DIR *NTOpenDirectory(const char *path)
00943 %
00944 %  A description of each parameter follows:
00945 %
00946 %    o entry: Specifies a pointer to a DIR structure.
00947 %
00948 */
00949 WizardExport DIR *NTOpenDirectory(const char *path)
00950 {
00951   char
00952     file_specification[MaxTextExtent];
00953 
00954   DIR
00955     *entry;
00956 
00957   size_t
00958     length;
00959 
00960   assert(path != (const char *) NULL);
00961   length=CopyWizardString(file_specification,path,MaxTextExtent);
00962   if (length >= (MaxTextExtent-1))
00963     return((DIR *) NULL);
00964   length=ConcatenateWizardString(file_specification,DirectorySeparator,
00965     MaxTextExtent);
00966   if (length >= (MaxTextExtent-1))
00967     return((DIR *) NULL);
00968   entry=(DIR *) AcquireWizardMemory(sizeof(DIR));
00969   if (entry != (DIR *) NULL)
00970     {
00971       entry->firsttime=TRUE;
00972       entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
00973     }
00974   if (entry->hSearch == INVALID_HANDLE_VALUE)
00975     {
00976       length=ConcatenateWizardString(file_specification,"\\*.*",MaxTextExtent);
00977       if (length >= (MaxTextExtent-1))
00978         {
00979           entry=(DIR *) RelinquishWizardMemory(entry);
00980           return((DIR *) NULL);
00981         }
00982       entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
00983       if (entry->hSearch == INVALID_HANDLE_VALUE)
00984         {
00985           entry=(DIR *) RelinquishWizardMemory(entry);
00986           return((DIR *) NULL);
00987         }
00988     }
00989   return(entry);
00990 }
00991 
00992 /*
00993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00994 %                                                                             %
00995 %                                                                             %
00996 %                                                                             %
00997 %   N T O p e n L i b r a r y                                                 %
00998 %                                                                             %
00999 %                                                                             %
01000 %                                                                             %
01001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01002 %
01003 %  NTOpenLibrary() loads a dynamic module into memory and returns a handle that
01004 %  can be used to access the various procedures in the module.
01005 %
01006 %  The format of the NTOpenLibrary method is:
01007 %
01008 %      void *NTOpenLibrary(const char *filename)
01009 %
01010 %  A description of each parameter follows:
01011 %
01012 %    o path: Specifies a pointer to string representing dynamic module that
01013 %      is to be loaded.
01014 %
01015 */
01016 
01017 static const char *GetSearchPath( void )
01018 {
01019 #if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
01020   return(lt_dlgetsearchpath());
01021 #else
01022   return(lt_slsearchpath);
01023 #endif
01024 }
01025 
01026 WizardExport void *NTOpenLibrary(const char *filename)
01027 {
01028 #define MaxPathElements  31
01029 
01030   char
01031     buffer[MaxTextExtent];
01032 
01033   int
01034     index;
01035 
01036   register const char
01037     *p,
01038     *q;
01039 
01040   register int
01041     i;
01042 
01043   UINT
01044     mode;
01045 
01046   void
01047     *handle;
01048 
01049   mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
01050   handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
01051   if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
01052     {
01053       SetErrorMode(mode);
01054       return(handle);
01055     }
01056   p=(char *) GetSearchPath();
01057   index=0;
01058   while (index < MaxPathElements)
01059   {
01060     q=strchr(p,DirectoryListSeparator);
01061     if (q == (char *) NULL)
01062       {
01063         (void) CopyWizardString(buffer,p,MaxTextExtent);
01064         (void) ConcatenateWizardString(buffer,"\\",MaxTextExtent);
01065         (void) ConcatenateWizardString(buffer,filename,MaxTextExtent);
01066         handle=(void *) LoadLibraryEx(buffer,NULL,
01067           LOAD_WITH_ALTERED_SEARCH_PATH);
01068         break;
01069       }
01070     i=q-p;
01071     (void) CopyWizardString(buffer,p,i+1);
01072     (void) ConcatenateWizardString(buffer,"\\",MaxTextExtent);
01073     (void) ConcatenateWizardString(buffer,filename,MaxTextExtent);
01074     handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
01075     if (handle != (void *) NULL)
01076       break;
01077     p=q+1;
01078   }
01079   SetErrorMode(mode);
01080   return(handle);
01081 }
01082 
01083 /*
01084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01085 %                                                                             %
01086 %                                                                             %
01087 %                                                                             %
01088 %    N T R e a d D i r e c t o r y                                            %
01089 %                                                                             %
01090 %                                                                             %
01091 %                                                                             %
01092 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01093 %
01094 %  NTReadDirectory() returns a pointer to a structure representing the
01095 %  directory entry at the current position in the directory stream to which
01096 %  entry refers.
01097 %
01098 %  The format of the NTReadDirectory
01099 %
01100 %      NTReadDirectory(entry)
01101 %
01102 %  A description of each parameter follows:
01103 %
01104 %    o entry: Specifies a pointer to a DIR structure.
01105 %
01106 */
01107 WizardExport struct dirent *NTReadDirectory(DIR *entry)
01108 {
01109   int
01110     status;
01111 
01112   size_t
01113     length;
01114 
01115   if (entry == (DIR *) NULL)
01116     return((struct dirent *) NULL);
01117   if (!entry->firsttime)
01118     {
01119       status=FindNextFile(entry->hSearch,&entry->Win32FindData);
01120       if (status == 0)
01121         return((struct dirent *) NULL);
01122     }
01123   length=CopyWizardString(entry->file_info.d_name,
01124     entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
01125   if (length >= sizeof(entry->file_info.d_name))
01126     return((struct dirent *) NULL);
01127   entry->firsttime=FALSE;
01128   entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
01129   return(&entry->file_info);
01130 }
01131 
01132 /*
01133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01134 %                                                                             %
01135 %                                                                             %
01136 %                                                                             %
01137 %   N T R e g i s t r y K e y L o o k u p                                     %
01138 %                                                                             %
01139 %                                                                             %
01140 %                                                                             %
01141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01142 %
01143 %  NTRegistryKeyLookup() returns Wizard's Toolkit installation path settings
01144 %  stored in the Windows Registry.  Path settings are specific to the
01145 %  installed Wizard's Toolkit version so that multiple Image Wizard
01146 %  installations may coexist.
01147 %
01148 %  Values are stored in the registry under a base path path similar to
01149 %  "HKEY_LOCAL_MACHINE/SOFTWARE\Wizard's Toolkit\1.0.1". The provided subkey
01150 %  is appended to this base path to form the full key.
01151 %
01152 %  The format of the NTRegistryKeyLookup method is:
01153 %
01154 %      char *NTRegistryKeyLookup(const char *subkey)
01155 %
01156 %  A description of each parameter follows:
01157 %
01158 %    o subkey: Specifies a string that identifies the registry object.
01159 %      Currently supported sub-keys include: "BinPath", "ConfigurePath",
01160 %      "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
01161 %
01162 */
01163 WizardExport unsigned char *NTRegistryKeyLookup(const char *subkey)
01164 {
01165   char
01166     package_key[MaxTextExtent];
01167 
01168   DWORD
01169     size,
01170     type;
01171 
01172   HKEY
01173     registry_key;
01174 
01175   LONG
01176     status;
01177 
01178   unsigned char
01179     *value;
01180 
01181   /*
01182     Look-up base key.
01183   */
01184   (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s",
01185     WizardPackageName,WizardLibVersionText);
01186   (void) LogWizardEvent(ConfigureEvent,GetWizardModule(),"%s",package_key);
01187   registry_key=(HKEY) INVALID_HANDLE_VALUE;
01188   status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
01189   if (status != ERROR_SUCCESS)
01190     {
01191       registry_key=(HKEY) INVALID_HANDLE_VALUE;
01192       return((unsigned char *) NULL);
01193     }
01194   /*
01195     Look-up sub key.
01196   */
01197   size=32;
01198   value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
01199   if (value == (unsigned char *) NULL)
01200     {
01201       RegCloseKey(registry_key);
01202       return((unsigned char *) NULL);
01203     }
01204   (void) LogWizardEvent(ConfigureEvent,GetWizardModule(),"%s",subkey);
01205   status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
01206   if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
01207     {
01208       value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
01209       if (value == (BYTE *) NULL)
01210         {
01211           RegCloseKey(registry_key);
01212           return((unsigned char *) NULL);
01213         }
01214       status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
01215     }
01216   RegCloseKey(registry_key);
01217   if ((type != REG_SZ) || (status != ERROR_SUCCESS))
01218     value=(unsigned char *) RelinquishWizardMemory(value);
01219   return((unsigned char *) value);
01220 }
01221 
01222 /*
01223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01224 %                                                                             %
01225 %                                                                             %
01226 %                                                                             %
01227 %   N T R e p o r t E v e n t                                                 %
01228 %                                                                             %
01229 %                                                                             %
01230 %                                                                             %
01231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01232 %
01233 %  NTReportEvent() reports an event.
01234 %
01235 %  The format of the NTReportEvent method is:
01236 %
01237 %      WizardBooleanType NTReportEvent(const char *event,
01238 %        const WizardBooleanType error)
01239 %
01240 %  A description of each parameter follows:
01241 %
01242 %    o event: The event.
01243 %
01244 %    o error: WizardTrue the event is an error.
01245 %
01246 */
01247 WizardExport WizardBooleanType NTReportEvent(const char *event,
01248   const WizardBooleanType error)
01249 {
01250   const char
01251     *events[1];
01252 
01253   HANDLE
01254     handle;
01255 
01256   WORD
01257     type;
01258 
01259   handle=RegisterEventSource(NULL,"Wizard's Toolkit");
01260   if (handle == NULL)
01261     return(WizardFalse);
01262   events[0]=event;
01263   type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
01264   ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
01265   DeregisterEventSource(handle);
01266   return(WizardTrue);
01267 }
01268 
01269 /*
01270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01271 %                                                                             %
01272 %                                                                             %
01273 %                                                                             %
01274 %   N T R e s o u r c e T o B l o b                                           %
01275 %                                                                             %
01276 %                                                                             %
01277 %                                                                             %
01278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01279 %
01280 %  NTResourceToBlob() returns a blob containing the contents of the resource
01281 %  in the current executable specified by the id parameter. This is currently
01282 %  used to retrieve MGK files tha have been embedded into the various command
01283 %  line utilities.
01284 %
01285 %  The format of the NTResourceToBlob method is:
01286 %
01287 %      unsigned char *NTResourceToBlob(const char *id)
01288 %
01289 %  A description of each parameter follows:
01290 %
01291 %    o id: Specifies a string that identifies the resource.
01292 %
01293 */
01294 WizardExport unsigned char *NTResourceToBlob(const char *id)
01295 {
01296   char
01297     path[MaxTextExtent];
01298 
01299   DWORD
01300     length;
01301 
01302   HGLOBAL
01303     global;
01304 
01305   HMODULE
01306     handle;
01307 
01308   HRSRC
01309     resource;
01310 
01311   unsigned char
01312     *blob,
01313     *value;
01314 
01315   assert(id != (const char *) NULL);
01316   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",id);
01317   (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
01318     DirectorySeparator,GetClientName());
01319   if (IsAccessible(path) != WizardFalse)
01320     handle=GetModuleHandle(path);
01321   else
01322     handle=GetModuleHandle(0);
01323   if (!handle)
01324     return((char *) NULL);
01325   resource=FindResource(handle,id,"WizardsToolkit");
01326   if (!resource)
01327     return((char *) NULL);
01328   global=LoadResource(handle,resource);
01329   if (!global)
01330     return((char *) NULL);
01331   length=SizeofResource(handle,resource);
01332   value=(unsigned char *) LockResource(global);
01333   if (!value)
01334     {
01335       FreeResource(global);
01336       return((char *) NULL);
01337     }
01338   blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
01339     sizeof(*blob));
01340   if (blob != (unsigned char *) NULL)
01341     {
01342       (void) CopyWizardMemory(blob,value,length);
01343       blob[length]='\0';
01344     }
01345   UnlockResource(global);
01346   FreeResource(global);
01347   return(blob);
01348 }
01349 
01350 /*
01351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01352 %                                                                             %
01353 %                                                                             %
01354 %                                                                             %
01355 %   N T S e e k D i r e c t o r y                                             %
01356 %                                                                             %
01357 %                                                                             %
01358 %                                                                             %
01359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01360 %
01361 %  NTSeekDirectory() sets the position of the next NTReadDirectory() operation
01362 %  on the directory stream.
01363 %
01364 %  The format of the NTSeekDirectory method is:
01365 %
01366 %      void NTSeekDirectory(DIR *entry,ssize_t position)
01367 %
01368 %  A description of each parameter follows:
01369 %
01370 %    o entry: Specifies a pointer to a DIR structure.
01371 %
01372 %    o position: specifies the position associated with the directory
01373 %      stream.
01374 %
01375 */
01376 WizardExport void NTSeekDirectory(DIR *entry,ssize_t position)
01377 {
01378   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
01379   assert(entry != (DIR *) NULL);
01380 }
01381 
01382 /*
01383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01384 %                                                                             %
01385 %                                                                             %
01386 %                                                                             %
01387 %   N T S e t S e a r c h P a t h                                             %
01388 %                                                                             %
01389 %                                                                             %
01390 %                                                                             %
01391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01392 %
01393 %  NTSetSearchPath() sets the current locations that the subsystem should
01394 %  look at to find dynamically loadable modules.
01395 %
01396 %  The format of the NTSetSearchPath method is:
01397 %
01398 %      int NTSetSearchPath(const char *path)
01399 %
01400 %  A description of each parameter follows:
01401 %
01402 %    o path: Specifies a pointer to string representing the search path
01403 %      for DLL's that can be dynamically loaded.
01404 %
01405 */
01406 WizardExport int NTSetSearchPath(const char *path)
01407 {
01408 #if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
01409   lt_dlsetsearchpath(path);
01410 #else
01411   if (lt_slsearchpath != (char *) NULL)
01412     lt_slsearchpath=(char *) RelinquishWizardMemory(lt_slsearchpath);
01413   if (path != (char *) NULL)
01414     lt_slsearchpath=AcquireString(path);
01415 #endif
01416   return(0);
01417 }
01418 
01419 /*
01420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01421 %                                                                             %
01422 %                                                                             %
01423 %                                                                             %
01424 +  N T S y n c M e m o r y                                                    %
01425 %                                                                             %
01426 %                                                                             %
01427 %                                                                             %
01428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01429 %
01430 %  NTSyncMemory() emulates the Unix method of the same name.
01431 %
01432 %  The format of the NTSyncMemory method is:
01433 %
01434 %      int NTSyncMemory(void *address,size_t length,int flags)
01435 %
01436 %  A description of each parameter follows:
01437 %
01438 %    o address: The address of the binary large object.
01439 %
01440 %    o length: The length of the binary large object.
01441 %
01442 %    o flags: Option flags (ignored for Windows).
01443 %
01444 */
01445 WizardExport int NTSyncMemory(void *address,size_t length,int flags)
01446 {
01447   if (FlushViewOfFile(address,length) == WizardFalse)
01448     return(-1);
01449   return(0);
01450 }
01451 
01452 /*
01453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01454 %                                                                             %
01455 %                                                                             %
01456 %                                                                             %
01457 %   N T S y s t e m C o m m a n d                                             %
01458 %                                                                             %
01459 %                                                                             %
01460 %                                                                             %
01461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01462 %
01463 %  NTSystemCommand() executes the specified command and waits until it
01464 %  terminates.  The returned value is the exit status of the command.
01465 %
01466 %  The format of the NTSystemComman method is:
01467 %
01468 %      int NTSystemComman(const char *command)
01469 %
01470 %  A description of each parameter follows:
01471 %
01472 %    o command: This string is the command to execute.
01473 %
01474 */
01475 WizardExport int NTSystemCommand(const char *command)
01476 {
01477   char
01478     local_command[MaxTextExtent];
01479 
01480   DWORD
01481     child_status;
01482 
01483   int
01484     status;
01485 
01486   WizardBooleanType
01487     background_process;
01488 
01489   PROCESS_INFORMATION
01490     process_info;
01491 
01492   STARTUPINFO
01493     startup_info;
01494 
01495   if (command == (char *) NULL)
01496     return(-1);
01497   GetStartupInfo(&startup_info);
01498   startup_info.dwFlags=STARTF_USESHOWWINDOW;
01499   startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
01500   (void) CopyWizardString(local_command,command,MaxTextExtent);
01501   background_process=command[strlen(command)-1] == '&';
01502   if (background_process)
01503     local_command[strlen(command)-1]='\0';
01504   if (command[strlen(command)-1] == '|')
01505      local_command[strlen(command)-1]='\0';
01506    else
01507      startup_info.wShowWindow=SW_SHOWDEFAULT;
01508   status=CreateProcess((LPCTSTR) NULL,local_command,
01509     (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
01510     (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
01511     &process_info);
01512   if (status == 0)
01513     return(-1);
01514   if (background_process)
01515     return(status == 0);
01516   status=WaitForSingleObject(process_info.hProcess,INFINITE);
01517   if (status != WAIT_OBJECT_0)
01518     return (status);
01519   status=GetExitCodeProcess(process_info.hProcess,&child_status);
01520   if (status == 0)
01521     return(-1);
01522   CloseHandle(process_info.hProcess);
01523   CloseHandle(process_info.hThread);
01524   return((int) child_status);
01525 }
01526 
01527 /*
01528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01529 %                                                                             %
01530 %                                                                             %
01531 %                                                                             %
01532 %   N T S y s t e m C o n i f i g u r a t i o n                               %
01533 %                                                                             %
01534 %                                                                             %
01535 %                                                                             %
01536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01537 %
01538 %  Sysconf() provides  a  way for the application to determine values for
01539 %  system limits or options at runtime.
01540 %
01541 %  The format of the exit method is:
01542 %
01543 %      ssize_t NTSystemConfiguration(int name)
01544 %
01545 %  A description of each parameter follows:
01546 %
01547 %    o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
01548 %
01549 */
01550 WizardExport ssize_t NTSystemConfiguration(int name)
01551 {
01552   switch (name)
01553   {
01554     case _SC_PAGESIZE:
01555     {
01556       SYSTEM_INFO
01557         system_info;
01558 
01559       GetSystemInfo(&system_info);
01560       return(system_info.dwPageSize);
01561     }
01562     case _SC_PHYS_PAGES:
01563     {
01564       HMODULE
01565         handle;
01566 
01567       LPFNDLLFUNC2
01568         module;
01569 
01570       NTMEMORYSTATUSEX
01571         status;
01572 
01573       SYSTEM_INFO
01574         system_info;
01575 
01576       handle=GetModuleHandle("kernel32.dll");
01577       if (handle == (HMODULE) NULL)
01578         return(0L);
01579       GetSystemInfo(&system_info);
01580       module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
01581       if (module == (LPFNDLLFUNC2) NULL)
01582         {
01583           MEMORYSTATUS
01584             status;
01585 
01586           GlobalMemoryStatus(&status);
01587           return((ssize_t) status.dwAvailPhys/system_info.dwPageSize);
01588         }
01589       status.dwLength=sizeof(status);
01590       if (module(&status) == 0)
01591         return(0L);
01592       return((ssize_t) status.ullAvailPhys/system_info.dwPageSize);
01593     }
01594     case _SC_OPEN_MAX:
01595       return(2048);
01596     default:
01597       break;
01598   }
01599   return(-1);
01600 }
01601 
01602 /*
01603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01604 %                                                                             %
01605 %                                                                             %
01606 %                                                                             %
01607 %   N T T e l l D i r e c t o r y                                             %
01608 %                                                                             %
01609 %                                                                             %
01610 %                                                                             %
01611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01612 %
01613 %  NTTellDirectory() returns the current location associated with the named
01614 %  directory stream.
01615 %
01616 %  The format of the NTTellDirectory method is:
01617 %
01618 %      ssize_t NTTellDirectory(DIR *entry)
01619 %
01620 %  A description of each parameter follows:
01621 %
01622 %    o entry: Specifies a pointer to a DIR structure.
01623 %
01624 */
01625 WizardExport ssize_t NTTellDirectory(DIR *entry)
01626 {
01627   assert(entry != (DIR *) NULL);
01628   return(0);
01629 }
01630 
01631 /*
01632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01633 %                                                                             %
01634 %                                                                             %
01635 %                                                                             %
01636 +  N T U n m a p M e m o r y                                                  %
01637 %                                                                             %
01638 %                                                                             %
01639 %                                                                             %
01640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01641 %
01642 %  NTUnmapMemory() emulates the Unix munmap method.
01643 %
01644 %  The format of the NTUnmapMemory method is:
01645 %
01646 %      int NTUnmapMemory(void *map,size_t length)
01647 %
01648 %  A description of each parameter follows:
01649 %
01650 %    o map: The address of the binary large object.
01651 %
01652 %    o length: The length of the binary large object.
01653 %
01654 */
01655 WizardExport int NTUnmapMemory(void *map,size_t length)
01656 {
01657   if (!UnmapViewOfFile(map))
01658     return(-1);
01659   return(0);
01660 }
01661 
01662 /*
01663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01664 %                                                                             %
01665 %                                                                             %
01666 %                                                                             %
01667 %   N T U s e r T i m e                                                       %
01668 %                                                                             %
01669 %                                                                             %
01670 %                                                                             %
01671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01672 %
01673 %  NTUserTime() returns the total time the process has been scheduled (e.g.
01674 %  seconds) since the last call to StartTimer().
01675 %
01676 %  The format of the UserTime method is:
01677 %
01678 %      double NTUserTime(void)
01679 %
01680 */
01681 WizardExport double NTUserTime(void)
01682 {
01683   DWORD
01684     status;
01685 
01686   FILETIME
01687     create_time,
01688     exit_time;
01689 
01690   OSVERSIONINFO
01691     OsVersionInfo;
01692 
01693   union
01694   {
01695     FILETIME
01696       filetime;
01697 
01698     __int64
01699       filetime64;
01700   } kernel_time;
01701 
01702   union
01703   {
01704     FILETIME
01705       filetime;
01706 
01707     __int64
01708       filetime64;
01709   } user_time;
01710 
01711   OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
01712   GetVersionEx(&OsVersionInfo);
01713   if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
01714     return(NTElapsedTime());
01715   status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
01716     &kernel_time.filetime,&user_time.filetime);
01717   if (status != TRUE)
01718     return(0.0);
01719   return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
01720 }
01721 
01722 /*
01723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01724 %                                                                             %
01725 %                                                                             %
01726 %                                                                             %
01727 %   N T W a r n i n g H a n d l e r                                           %
01728 %                                                                             %
01729 %                                                                             %
01730 %                                                                             %
01731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01732 %
01733 %  NTWarningHandler() displays a warning reason.
01734 %
01735 %  The format of the NTWarningHandler method is:
01736 %
01737 %      void NTWarningHandler(const ExceptionType warning,const char *reason,
01738 %        const char *description)
01739 %
01740 %  A description of each parameter follows:
01741 %
01742 %    o warning: Specifies the numeric warning category.
01743 %
01744 %    o reason: Specifies the reason to display before terminating the
01745 %      program.
01746 %
01747 %    o description: Specifies any description to the reason.
01748 %
01749 */
01750 WizardExport void NTWarningHandler(const ExceptionType warning,
01751   const char *reason,const char *description)
01752 {
01753   char
01754     buffer[2*MaxTextExtent];
01755 
01756   if (reason == (char *) NULL)
01757     return;
01758   if (description == (char *) NULL)
01759     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
01760       reason);
01761   else
01762     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
01763       GetClientName(),reason,description);
01764   (void) MessageBox(NULL,buffer,"Wizard's Toolkit Warning",MB_OK |
01765     MB_TASKMODAL | MB_SETFOREGROUND | MB_ICONINFORMATION);
01766 }
01767 #endif