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