00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
00055 #include "wizard/nt-base.h"
00056 #include <NTSecAPI.h>
00057 #include <wincrypt.h>
00058
00059
00060
00061
00062 #if !defined(MAP_FAILED)
00063 #define MAP_FAILED ((void *) -1)
00064 #endif
00065
00066
00067
00068
00069 #if !defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
00070 static char
00071 *lt_slsearchpath = (char *) NULL;
00072 #endif
00073
00074
00075
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
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
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
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
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
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
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
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
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
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
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
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
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
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
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
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
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
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
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
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 WizardExport int NTExitLibrary(void)
00527 {
00528 return(0);
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
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
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
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
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
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
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
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
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
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
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
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
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
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
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840 WizardExport int NTInitializeLibrary(void)
00841 {
00842 return(0);
00843 }
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
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
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
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
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
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
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
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
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
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
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,®istry_key);
01188 if (status != ERROR_SUCCESS)
01189 {
01190 registry_key=(HKEY) INVALID_HANDLE_VALUE;
01191 return((unsigned char *) NULL);
01192 }
01193
01194
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
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
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
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
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
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
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
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
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
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
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
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
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
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
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
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
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
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
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
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
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
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
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