sysutils.pp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for win32
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. uses
  18. windows;
  19. {$DEFINE HAS_SLEEP}
  20. {$DEFINE HAS_OSERROR}
  21. {$DEFINE HAS_OSCONFIG}
  22. {$DEFINE HAS_OSUSERDIR}
  23. {$DEFINE HAS_CREATEGUID}
  24. { Include platform independent interface part }
  25. {$i sysutilh.inc}
  26. type
  27. TSystemTime = Windows.TSystemTime;
  28. EWin32Error = class(Exception)
  29. public
  30. ErrorCode : DWORD;
  31. end;
  32. Var
  33. Win32Platform : Longint;
  34. Win32MajorVersion,
  35. Win32MinorVersion,
  36. Win32BuildNumber : dword;
  37. Win32CSDVersion : ShortString; // CSD record is 128 bytes only?
  38. { Compatibility with Delphi }
  39. function Win32Check(res:boolean):boolean;inline;
  40. function WinCheck(res:boolean):boolean;
  41. function CheckWin32Version(Major,Minor : Integer ): Boolean;
  42. function CheckWin32Version(Major : Integer): Boolean;
  43. Procedure RaiseLastWin32Error;
  44. function GetFileVersion(const AFileName: string): Cardinal;
  45. procedure GetFormatSettings;
  46. // OS specific versions for windows, to keep redir working
  47. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;ExecInheritsHandles:boolean):integer;
  48. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;ExecInheritsHandles:boolean):integer;
  49. implementation
  50. uses
  51. sysconst;
  52. function WinCheck(res:boolean):boolean;
  53. begin
  54. if not res then
  55. RaiseLastOSError;
  56. result:=res;
  57. end;
  58. function Win32Check(res:boolean):boolean;inline;
  59. begin
  60. result:=WinCheck(res);
  61. end;
  62. procedure RaiseLastWin32Error;
  63. begin
  64. RaiseLastOSError;
  65. end;
  66. function CheckWin32Version(Major : Integer): Boolean;
  67. begin
  68. Result:=CheckWin32Version(Major,0)
  69. end;
  70. function CheckWin32Version(Major,Minor: Integer): Boolean;
  71. begin
  72. Result:=(Win32MajorVersion>dword(Major)) or
  73. ((Win32MajorVersion=dword(Major)) and (Win32MinorVersion>=dword(Minor)));
  74. end;
  75. function GetFileVersion(const AFileName:string):Cardinal;
  76. var
  77. { usefull only as long as we don't need to touch different stack pages }
  78. buf : array[0..3071] of byte;
  79. bufp : pointer;
  80. fn : string;
  81. valsize,
  82. size : DWORD;
  83. h : DWORD;
  84. valrec : PVSFixedFileInfo;
  85. begin
  86. result:=$fffffff;
  87. fn:=AFileName;
  88. UniqueString(fn);
  89. size:=GetFileVersionInfoSize(pchar(fn),@h);
  90. if size>sizeof(buf) then
  91. begin
  92. getmem(bufp,size);
  93. try
  94. if GetFileVersionInfo(pchar(fn),h,size,bufp) then
  95. if VerQueryValue(bufp,'\',valrec,valsize) then
  96. result:=valrec^.dwFileVersionMS;
  97. finally
  98. freemem(bufp);
  99. end;
  100. end
  101. else
  102. begin
  103. if GetFileVersionInfo(pchar(fn),h,size,@buf) then
  104. if VerQueryValue(@buf,'\',valrec,valsize) then
  105. result:=valrec^.dwFileVersionMS;
  106. end;
  107. end;
  108. {$define HASCREATEGUID}
  109. {$define HASEXPANDUNCFILENAME}
  110. {$DEFINE FPC_NOGENERICANSIROUTINES}
  111. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  112. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  113. { Include platform independent implementation part }
  114. {$i sysutils.inc}
  115. function SysGetTempFileName(lpPathName:LPCSTR;
  116. lpPrefixString:LPCSTR;
  117. uUnique:UINT;
  118. lpTempFileName:LPSTR):UINT;stdcall;external 'kernel32' name 'GetTempFileNameA';
  119. function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
  120. begin
  121. Result:=SysGetTempFileName(Dir,Prefix,uUnique,TempFileName);
  122. end;
  123. { UUID generation. }
  124. function CoCreateGuid(out guid: TGUID): HResult; stdcall; external 'ole32.dll' name 'CoCreateGuid';
  125. function SysCreateGUID(out Guid: TGUID): Integer;
  126. begin
  127. Result := Integer(CoCreateGuid(Guid));
  128. end;
  129. function ExpandUNCFileName (const filename:string) : string;
  130. { returns empty string on errors }
  131. var
  132. s : ansistring;
  133. size : dword;
  134. rc : dword;
  135. buf : pchar;
  136. begin
  137. s := ExpandFileName (filename);
  138. s := s + #0;
  139. size := max_path;
  140. getmem(buf,size);
  141. try
  142. rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  143. if rc=ERROR_MORE_DATA then
  144. begin
  145. buf:=reallocmem(buf,size);
  146. rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  147. end;
  148. if rc = NO_ERROR then
  149. Result := PRemoteNameInfo(buf)^.lpUniversalName
  150. else if rc = ERROR_NOT_CONNECTED then
  151. Result := filename
  152. else
  153. Result := '';
  154. finally
  155. freemem(buf);
  156. end;
  157. end;
  158. {****************************************************************************
  159. File Functions
  160. ****************************************************************************}
  161. var
  162. SetFilePointerEx : function(hFile : THandle;
  163. liDistanceToMove : int64;lpNewFilePointer : pint64;
  164. dwMoveMethod : DWord) : ByteBool;stdcall;
  165. Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
  166. const
  167. AccessMode: array[0..2] of Cardinal = (
  168. GENERIC_READ,
  169. GENERIC_WRITE,
  170. GENERIC_READ or GENERIC_WRITE);
  171. ShareMode: array[0..4] of Integer = (
  172. 0,
  173. 0,
  174. FILE_SHARE_READ,
  175. FILE_SHARE_WRITE,
  176. FILE_SHARE_READ or FILE_SHARE_WRITE);
  177. Var
  178. FN : string;
  179. begin
  180. FN:=FileName+#0;
  181. result := CreateFile(@FN[1], dword(AccessMode[Mode and 3]),
  182. dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
  183. FILE_ATTRIBUTE_NORMAL, 0);
  184. //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
  185. end;
  186. Function FileCreate (Const FileName : String) : THandle;
  187. Var
  188. FN : string;
  189. begin
  190. FN:=FileName+#0;
  191. Result := CreateFile(@FN[1], GENERIC_READ or GENERIC_WRITE,
  192. 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  193. end;
  194. Function FileCreate (Const FileName : String; Mode:longint) : THandle;
  195. begin
  196. FileCreate:=FileCreate(FileName);
  197. end;
  198. Function FileRead (Handle : THandle; Var Buffer; Count : longint) : Longint;
  199. Var
  200. res : dword;
  201. begin
  202. if ReadFile(Handle, Buffer, Count, res, nil) then
  203. FileRead:=Res
  204. else
  205. FileRead:=-1;
  206. end;
  207. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  208. Var
  209. Res : dword;
  210. begin
  211. if WriteFile(Handle, Buffer, Count, Res, nil) then
  212. FileWrite:=Res
  213. else
  214. FileWrite:=-1;
  215. end;
  216. Function FileSeek (Handle : THandle;FOffset,Origin : Longint) : Longint;
  217. begin
  218. Result := longint(SetFilePointer(Handle, FOffset, nil, Origin));
  219. end;
  220. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  221. var
  222. rslt: Int64Rec;
  223. begin
  224. rslt := Int64Rec(FOffset);
  225. rslt.lo := SetFilePointer(Handle, rslt.lo, @rslt.hi, Origin);
  226. if (rslt.lo = $FFFFFFFF) and (GetLastError <> 0) then
  227. rslt.hi := $FFFFFFFF;
  228. Result := Int64(rslt);
  229. end;
  230. Procedure FileClose (Handle : THandle);
  231. begin
  232. if Handle<=4 then
  233. exit;
  234. CloseHandle(Handle);
  235. end;
  236. Function FileTruncate (Handle : THandle;Size: Int64) : boolean;
  237. begin
  238. {
  239. Result:=longint(SetFilePointer(handle,Size,nil,FILE_BEGIN))<>-1;
  240. }
  241. if FileSeek (Handle, Size, FILE_BEGIN) = Size then
  242. Result:=SetEndOfFile(handle)
  243. else
  244. Result := false;
  245. end;
  246. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  247. var
  248. lft : TFileTime;
  249. begin
  250. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,@lft) and
  251. LocalFileTimeToFileTime(lft,Wtime);
  252. end;
  253. Function WinToDosTime (Var Wtime : TFileTime;var DTime:longint):longbool;
  254. var
  255. lft : TFileTime;
  256. begin
  257. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  258. FileTimeToDosDateTime(lft,Longrec(Dtime).Hi,LongRec(DTIME).lo);
  259. end;
  260. Function FileAge (Const FileName : String): Longint;
  261. var
  262. Handle: THandle;
  263. FindData: TWin32FindData;
  264. begin
  265. Handle := FindFirstFile(Pchar(FileName), FindData);
  266. if Handle <> INVALID_HANDLE_VALUE then
  267. begin
  268. Windows.FindClose(Handle);
  269. if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  270. If WinToDosTime(FindData.ftLastWriteTime,Result) then
  271. exit;
  272. end;
  273. Result := -1;
  274. end;
  275. Function FileExists (Const FileName : String) : Boolean;
  276. var
  277. Attr:Dword;
  278. begin
  279. Attr:=GetFileAttributes(PChar(FileName));
  280. if Attr <> $ffffffff then
  281. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0
  282. else
  283. Result:=False;
  284. end;
  285. Function DirectoryExists (Const Directory : String) : Boolean;
  286. var
  287. Attr:Dword;
  288. begin
  289. Attr:=GetFileAttributes(PChar(Directory));
  290. if Attr <> $ffffffff then
  291. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) > 0
  292. else
  293. Result:=False;
  294. end;
  295. Function FindMatch(var f: TSearchRec) : Longint;
  296. begin
  297. { Find file with correct attribute }
  298. While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
  299. begin
  300. if not FindNextFile (F.FindHandle,F.FindData) then
  301. begin
  302. Result:=GetLastError;
  303. exit;
  304. end;
  305. end;
  306. { Convert some attributes back }
  307. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  308. f.size:=F.FindData.NFileSizeLow+(qword(maxdword)+1)*F.FindData.NFileSizeHigh;
  309. f.attr:=F.FindData.dwFileAttributes;
  310. f.Name:=StrPas(@F.FindData.cFileName[0]);
  311. Result:=0;
  312. end;
  313. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  314. begin
  315. Rslt.Name:=Path;
  316. Rslt.Attr:=attr;
  317. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  318. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  319. { FindFirstFile is a Win32 Call }
  320. Rslt.FindHandle:=FindFirstFile (PChar(Path),Rslt.FindData);
  321. If Rslt.FindHandle=Invalid_Handle_value then
  322. begin
  323. Result:=GetLastError;
  324. exit;
  325. end;
  326. { Find file with correct attribute }
  327. Result:=FindMatch(Rslt);
  328. end;
  329. Function FindNext (Var Rslt : TSearchRec) : Longint;
  330. begin
  331. if FindNextFile(Rslt.FindHandle, Rslt.FindData) then
  332. Result := FindMatch(Rslt)
  333. else
  334. Result := GetLastError;
  335. end;
  336. Procedure FindClose (Var F : TSearchrec);
  337. begin
  338. if F.FindHandle <> INVALID_HANDLE_VALUE then
  339. Windows.FindClose(F.FindHandle);
  340. end;
  341. Function FileGetDate (Handle : THandle) : Longint;
  342. Var
  343. FT : TFileTime;
  344. begin
  345. If GetFileTime(Handle,nil,nil,@ft) and
  346. WinToDosTime(FT,Result) then
  347. exit;
  348. Result:=-1;
  349. end;
  350. Function FileSetDate (Handle : THandle;Age : Longint) : Longint;
  351. Var
  352. FT: TFileTime;
  353. begin
  354. Result := 0;
  355. if DosToWinTime(Age,FT) and
  356. SetFileTime(Handle, nil, nil, @FT) then
  357. Exit;
  358. Result := GetLastError;
  359. end;
  360. Function FileGetAttr (Const FileName : String) : Longint;
  361. begin
  362. Result:=GetFileAttributes(PChar(FileName));
  363. end;
  364. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  365. begin
  366. if SetFileAttributes(PChar(FileName), Attr) then
  367. Result:=0
  368. else
  369. Result := GetLastError;
  370. end;
  371. Function DeleteFile (Const FileName : String) : Boolean;
  372. begin
  373. Result:=Windows.DeleteFile(Pchar(FileName));
  374. end;
  375. Function RenameFile (Const OldName, NewName : String) : Boolean;
  376. begin
  377. Result := MoveFile(PChar(OldName), PChar(NewName));
  378. end;
  379. {****************************************************************************
  380. Disk Functions
  381. ****************************************************************************}
  382. function GetDiskFreeSpace(drive:pchar;var sector_cluster,bytes_sector,
  383. freeclusters,totalclusters:longint):longbool;
  384. stdcall;external 'kernel32' name 'GetDiskFreeSpaceA';
  385. type
  386. TGetDiskFreeSpaceEx = function(drive:pchar;var availableforcaller,total,free):longbool;stdcall;
  387. var
  388. GetDiskFreeSpaceEx : TGetDiskFreeSpaceEx;
  389. function diskfree(drive : byte) : int64;
  390. var
  391. disk : array[1..4] of char;
  392. secs,bytes,
  393. free,total : longint;
  394. qwtotal,qwfree,qwcaller : int64;
  395. begin
  396. if drive=0 then
  397. begin
  398. disk[1]:='\';
  399. disk[2]:=#0;
  400. end
  401. else
  402. begin
  403. disk[1]:=chr(drive+64);
  404. disk[2]:=':';
  405. disk[3]:='\';
  406. disk[4]:=#0;
  407. end;
  408. if assigned(GetDiskFreeSpaceEx) then
  409. begin
  410. if GetDiskFreeSpaceEx(@disk[1],qwcaller,qwtotal,qwfree) then
  411. diskfree:=qwfree
  412. else
  413. diskfree:=-1;
  414. end
  415. else
  416. begin
  417. if GetDiskFreeSpace(@disk[1],secs,bytes,free,total) then
  418. diskfree:=int64(free)*secs*bytes
  419. else
  420. diskfree:=-1;
  421. end;
  422. end;
  423. function disksize(drive : byte) : int64;
  424. var
  425. disk : array[1..4] of char;
  426. secs,bytes,
  427. free,total : longint;
  428. qwtotal,qwfree,qwcaller : int64;
  429. begin
  430. if drive=0 then
  431. begin
  432. disk[1]:='\';
  433. disk[2]:=#0;
  434. end
  435. else
  436. begin
  437. disk[1]:=chr(drive+64);
  438. disk[2]:=':';
  439. disk[3]:='\';
  440. disk[4]:=#0;
  441. end;
  442. if assigned(GetDiskFreeSpaceEx) then
  443. begin
  444. if GetDiskFreeSpaceEx(@disk[1],qwcaller,qwtotal,qwfree) then
  445. disksize:=qwtotal
  446. else
  447. disksize:=-1;
  448. end
  449. else
  450. begin
  451. if GetDiskFreeSpace(@disk[1],secs,bytes,free,total) then
  452. disksize:=int64(total)*secs*bytes
  453. else
  454. disksize:=-1;
  455. end;
  456. end;
  457. Function GetCurrentDir : String;
  458. begin
  459. GetDir(0, result);
  460. end;
  461. Function SetCurrentDir (Const NewDir : String) : Boolean;
  462. begin
  463. Result:=SetCurrentDirectory(PChar(NewDir));
  464. end;
  465. Function CreateDir (Const NewDir : String) : Boolean;
  466. begin
  467. Result:=CreateDirectory(PChar(NewDir),nil);
  468. end;
  469. Function RemoveDir (Const Dir : String) : Boolean;
  470. begin
  471. Result:=RemoveDirectory(PChar(Dir));
  472. end;
  473. {****************************************************************************
  474. Time Functions
  475. ****************************************************************************}
  476. Procedure GetLocalTime(var SystemTime: TSystemTime);
  477. Var
  478. Syst : Windows.TSystemtime;
  479. begin
  480. windows.Getlocaltime(@syst);
  481. SystemTime.year:=syst.wYear;
  482. SystemTime.month:=syst.wMonth;
  483. SystemTime.day:=syst.wDay;
  484. SystemTime.hour:=syst.wHour;
  485. SystemTime.minute:=syst.wMinute;
  486. SystemTime.second:=syst.wSecond;
  487. SystemTime.millisecond:=syst.wMilliSeconds;
  488. end;
  489. {****************************************************************************
  490. Misc Functions
  491. ****************************************************************************}
  492. procedure sysbeep;
  493. begin
  494. MessageBeep(0);
  495. end;
  496. {****************************************************************************
  497. Locale Functions
  498. ****************************************************************************}
  499. function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
  500. var
  501. L: Integer;
  502. Buf: array[0..255] of Char;
  503. begin
  504. L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
  505. if L > 0 then
  506. SetString(Result, @Buf[0], L - 1)
  507. else
  508. Result := Def;
  509. end;
  510. function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
  511. var
  512. Buf: array[0..1] of Char;
  513. begin
  514. if GetLocaleInfo(LID, LT, Buf, 2) > 0 then
  515. Result := Buf[0]
  516. else
  517. Result := Def;
  518. end;
  519. Function GetLocaleInt(LID,TP,Def: LongInt): LongInt;
  520. Var
  521. S: String;
  522. C: Integer;
  523. Begin
  524. S:=GetLocaleStr(LID,TP,'0');
  525. Val(S,Result,C);
  526. If C<>0 Then
  527. Result:=Def;
  528. End;
  529. procedure GetFormatSettings;
  530. var
  531. HF : Shortstring;
  532. LID : LCID;
  533. I,Day : longint;
  534. begin
  535. LID := GetThreadLocale;
  536. { Date stuff }
  537. for I := 1 to 12 do
  538. begin
  539. ShortMonthNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVMONTHNAME1+I-1,ShortMonthNames[i]);
  540. LongMonthNames[I]:=GetLocaleStr(LID,LOCALE_SMONTHNAME1+I-1,LongMonthNames[i]);
  541. end;
  542. for I := 1 to 7 do
  543. begin
  544. Day := (I + 5) mod 7;
  545. ShortDayNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVDAYNAME1+Day,ShortDayNames[i]);
  546. LongDayNames[I]:=GetLocaleStr(LID,LOCALE_SDAYNAME1+Day,LongDayNames[i]);
  547. end;
  548. DateSeparator := GetLocaleChar(LID, LOCALE_SDATE, '/');
  549. ShortDateFormat := GetLocaleStr(LID, LOCALE_SSHORTDATE, 'm/d/yy');
  550. LongDateFormat := GetLocaleStr(LID, LOCALE_SLONGDATE, 'mmmm d, yyyy');
  551. { Time stuff }
  552. TimeSeparator := GetLocaleChar(LID, LOCALE_STIME, ':');
  553. TimeAMString := GetLocaleStr(LID, LOCALE_S1159, 'AM');
  554. TimePMString := GetLocaleStr(LID, LOCALE_S2359, 'PM');
  555. if StrToIntDef(GetLocaleStr(LID, LOCALE_ITLZERO, '0'), 0) = 0 then
  556. HF:='h'
  557. else
  558. HF:='hh';
  559. // No support for 12 hour stuff at the moment...
  560. ShortTimeFormat := HF+':nn';
  561. LongTimeFormat := HF + ':nn:ss';
  562. { Currency stuff }
  563. CurrencyString:=GetLocaleStr(LID, LOCALE_SCURRENCY, '');
  564. CurrencyFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRENCY, '0'), 0);
  565. NegCurrFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_INEGCURR, '0'), 0);
  566. { Number stuff }
  567. ThousandSeparator:=GetLocaleChar(LID, LOCALE_STHOUSAND, ',');
  568. DecimalSeparator:=GetLocaleChar(LID, LOCALE_SDECIMAL, '.');
  569. CurrencyDecimals:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRDIGITS, '0'), 0);
  570. end;
  571. Procedure InitInternational;
  572. var
  573. { A call to GetSystemMetrics changes the value of the 8087 Control Word on
  574. Pentium4 with WinXP SP2 }
  575. old8087CW: word;
  576. begin
  577. InitInternationalGeneric;
  578. old8087CW:=Get8087CW;
  579. SysLocale.MBCS:=GetSystemMetrics(SM_DBCSENABLED)<>0;
  580. SysLocale.RightToLeft:=GetSystemMetrics(SM_MIDEASTENABLED)<>0;
  581. SysLocale.DefaultLCID := $0409;
  582. SysLocale.PriLangID := LANG_ENGLISH;
  583. SysLocale.SubLangID := SUBLANG_ENGLISH_US;
  584. // probably needs update with getthreadlocale. post 2.0.2
  585. Set8087CW(old8087CW);
  586. GetFormatSettings;
  587. end;
  588. {****************************************************************************
  589. Target Dependent
  590. ****************************************************************************}
  591. function FormatMessageA(dwFlags : DWORD;
  592. lpSource : Pointer;
  593. dwMessageId : DWORD;
  594. dwLanguageId: DWORD;
  595. lpBuffer : PCHAR;
  596. nSize : DWORD;
  597. Arguments : Pointer): DWORD; stdcall;external 'kernel32' name 'FormatMessageA';
  598. function SysErrorMessage(ErrorCode: Integer): String;
  599. const
  600. MaxMsgSize = Format_Message_Max_Width_Mask;
  601. var
  602. MsgBuffer: pChar;
  603. begin
  604. GetMem(MsgBuffer, MaxMsgSize);
  605. FillChar(MsgBuffer^, MaxMsgSize, #0);
  606. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
  607. nil,
  608. ErrorCode,
  609. MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
  610. MsgBuffer, { This function allocs the memory }
  611. MaxMsgSize, { Maximum message size }
  612. nil);
  613. SysErrorMessage := StrPas(MsgBuffer);
  614. FreeMem(MsgBuffer, MaxMsgSize);
  615. end;
  616. {****************************************************************************
  617. Initialization code
  618. ****************************************************************************}
  619. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  620. var
  621. s : string;
  622. i : longint;
  623. hp,p : pchar;
  624. begin
  625. Result:='';
  626. p:=GetEnvironmentStrings;
  627. hp:=p;
  628. while hp^<>#0 do
  629. begin
  630. s:=strpas(hp);
  631. i:=pos('=',s);
  632. if uppercase(copy(s,1,i-1))=upcase(envvar) then
  633. begin
  634. Result:=copy(s,i+1,length(s)-i);
  635. break;
  636. end;
  637. { next string entry}
  638. hp:=hp+strlen(hp)+1;
  639. end;
  640. FreeEnvironmentStrings(p);
  641. end;
  642. Function GetEnvironmentVariableCount : Integer;
  643. var
  644. hp,p : pchar;
  645. begin
  646. Result:=0;
  647. p:=GetEnvironmentStrings;
  648. hp:=p;
  649. If (Hp<>Nil) then
  650. while hp^<>#0 do
  651. begin
  652. Inc(Result);
  653. hp:=hp+strlen(hp)+1;
  654. end;
  655. FreeEnvironmentStrings(p);
  656. end;
  657. Function GetEnvironmentString(Index : Integer) : String;
  658. var
  659. hp,p : pchar;
  660. begin
  661. Result:='';
  662. p:=GetEnvironmentStrings;
  663. hp:=p;
  664. If (Hp<>Nil) then
  665. begin
  666. while (hp^<>#0) and (Index>1) do
  667. begin
  668. Dec(Index);
  669. hp:=hp+strlen(hp)+1;
  670. end;
  671. If (hp^<>#0) then
  672. Result:=StrPas(HP);
  673. end;
  674. FreeEnvironmentStrings(p);
  675. end;
  676. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;ExecInheritsHandles:boolean):integer;
  677. // win specific function
  678. var
  679. SI: TStartupInfo;
  680. PI: TProcessInformation;
  681. Proc : THandle;
  682. l : DWord;
  683. CommandLine : ansistring;
  684. e : EOSError;
  685. begin
  686. FillChar(SI, SizeOf(SI), 0);
  687. SI.cb:=SizeOf(SI);
  688. SI.wShowWindow:=1;
  689. { always surround the name of the application by quotes
  690. so that long filenames will always be accepted. But don't
  691. do it if there are already double quotes, since Win32 does not
  692. like double quotes which are duplicated!
  693. }
  694. if pos('"',path)=0 then
  695. CommandLine:='"'+path+'"'
  696. else
  697. CommandLine:=path;
  698. if ComLine <> '' then
  699. CommandLine:=Commandline+' '+ComLine+#0
  700. else
  701. CommandLine := CommandLine + #0;
  702. if not CreateProcess(nil, pchar(CommandLine),
  703. Nil, Nil, ExecInheritsHandles,$20, Nil, Nil, SI, PI) then
  704. begin
  705. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  706. e.ErrorCode:=GetLastError;
  707. raise e;
  708. end;
  709. Proc:=PI.hProcess;
  710. if WaitForSingleObject(Proc, dword($ffffffff)) <> $ffffffff then
  711. begin
  712. GetExitCodeProcess(Proc,l);
  713. CloseHandle(Proc);
  714. CloseHandle(PI.hThread);
  715. result:=l;
  716. end
  717. else
  718. begin
  719. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  720. e.ErrorCode:=GetLastError;
  721. CloseHandle(Proc);
  722. CloseHandle(PI.hThread);
  723. raise e;
  724. end;
  725. end;
  726. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  727. // os independant function
  728. begin
  729. result:=Executeprocess(path,comline,false);
  730. end;
  731. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;ExecInheritsHandles:boolean):integer;
  732. var
  733. CommandLine: AnsiString;
  734. I: integer;
  735. begin
  736. Commandline := '';
  737. for I := 0 to High (ComLine) do
  738. if Pos (' ', ComLine [I]) <> 0 then
  739. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  740. else
  741. CommandLine := CommandLine + ' ' + Comline [I];
  742. ExecuteProcess := ExecuteProcess (Path, CommandLine,ExecInheritsHandles);
  743. end;
  744. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString):integer;
  745. // os independant function
  746. begin
  747. result:=Executeprocess(path,comline,false);
  748. end;
  749. Procedure Sleep(Milliseconds : Cardinal);
  750. begin
  751. Windows.Sleep(MilliSeconds)
  752. end;
  753. Function GetLastOSError : Integer;
  754. begin
  755. Result:=GetLastError;
  756. end;
  757. {****************************************************************************
  758. Initialization code
  759. ****************************************************************************}
  760. var
  761. kernel32dll : THandle;
  762. Procedure LoadVersionInfo;
  763. // and getfreespaceex
  764. Var
  765. versioninfo : TOSVERSIONINFO;
  766. begin
  767. kernel32dll:=0;
  768. GetDiskFreeSpaceEx:=nil;
  769. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  770. GetVersionEx(versioninfo);
  771. Win32Platform:=versionInfo.dwPlatformId;
  772. Win32MajorVersion:=versionInfo.dwMajorVersion;
  773. Win32MinorVersion:=versionInfo.dwMinorVersion;
  774. Win32BuildNumber:=versionInfo.dwBuildNumber;
  775. Move (versioninfo.szCSDVersion ,Win32CSDVersion[1],128);
  776. win32CSDVersion[0]:=chr(strlen(pchar(@versioninfo.szCSDVersion)));
  777. if ((versioninfo.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) and
  778. (versioninfo.dwBuildNUmber>=1000)) or
  779. (versioninfo.dwPlatformId=VER_PLATFORM_WIN32_NT) then
  780. begin
  781. kernel32dll:=LoadLibrary('kernel32');
  782. if kernel32dll<>0 then
  783. GetDiskFreeSpaceEx:=TGetDiskFreeSpaceEx(GetProcAddress(kernel32dll,'GetDiskFreeSpaceExA'));
  784. end;
  785. end;
  786. function FreeLibrary(hLibModule : THANDLE) : longbool;
  787. stdcall;external 'kernel32' name 'FreeLibrary';
  788. function GetVersionEx(var VersionInformation:TOSVERSIONINFO) : longbool;
  789. stdcall;external 'kernel32' name 'GetVersionExA';
  790. function LoadLibrary(lpLibFileName : pchar):THandle;
  791. stdcall;external 'kernel32' name 'LoadLibraryA';
  792. function GetProcAddress(hModule : THandle;lpProcName : pchar) : pointer;
  793. stdcall;external 'kernel32' name 'GetProcAddress';
  794. Const
  795. CSIDL_PROGRAMS = $0002; { %SYSTEMDRIVE%\Program Files }
  796. CSIDL_PERSONAL = $0005; { %USERPROFILE%\My Documents }
  797. CSIDL_FAVORITES = $0006; { %USERPROFILE%\Favorites }
  798. CSIDL_STARTUP = $0007; { %USERPROFILE%\Start menu\Programs\Startup }
  799. CSIDL_RECENT = $0008; { %USERPROFILE%\Recent }
  800. CSIDL_SENDTO = $0009; { %USERPROFILE%\Sendto }
  801. CSIDL_STARTMENU = $000B; { %USERPROFILE%\Start menu }
  802. CSIDL_MYMUSIC = $000D; { %USERPROFILE%\Documents\My Music }
  803. CSIDL_MYVIDEO = $000E; { %USERPROFILE%\Documents\My Videos }
  804. CSIDL_DESKTOPDIRECTORY = $0010; { %USERPROFILE%\Desktop }
  805. CSIDL_NETHOOD = $0013; { %USERPROFILE%\NetHood }
  806. CSIDL_TEMPLATES = $0015; { %USERPROFILE%\Templates }
  807. CSIDL_COMMON_STARTMENU = $0016; { %PROFILEPATH%\All users\Start menu }
  808. CSIDL_COMMON_PROGRAMS = $0017; { %PROFILEPATH%\All users\Start menu\Programs }
  809. CSIDL_COMMON_STARTUP = $0018; { %PROFILEPATH%\All users\Start menu\Programs\Startup }
  810. CSIDL_COMMON_DESKTOPDIRECTORY = $0019; { %PROFILEPATH%\All users\Desktop }
  811. CSIDL_APPDATA = $001A; { %USERPROFILE%\Application Data (roaming) }
  812. CSIDL_PRINTHOOD = $001B; { %USERPROFILE%\Printhood }
  813. CSIDL_LOCAL_APPDATA = $001C; { %USERPROFILE%\Local Settings\Application Data (non roaming) }
  814. CSIDL_COMMON_FAVORITES = $001F; { %PROFILEPATH%\All users\Favorites }
  815. CSIDL_INTERNET_CACHE = $0020; { %USERPROFILE%\Local Settings\Temporary Internet Files }
  816. CSIDL_COOKIES = $0021; { %USERPROFILE%\Cookies }
  817. CSIDL_HISTORY = $0022; { %USERPROFILE%\Local settings\History }
  818. CSIDL_COMMON_APPDATA = $0023; { %PROFILESPATH%\All Users\Application Data }
  819. CSIDL_WINDOWS = $0024; { %SYSTEMROOT% }
  820. CSIDL_SYSTEM = $0025; { %SYSTEMROOT%\SYSTEM32 (may be system on 95/98/ME) }
  821. CSIDL_PROGRAM_FILES = $0026; { %SYSTEMDRIVE%\Program Files }
  822. CSIDL_MYPICTURES = $0027; { %USERPROFILE%\My Documents\My Pictures }
  823. CSIDL_PROFILE = $0028; { %USERPROFILE% }
  824. CSIDL_PROGRAM_FILES_COMMON = $002B; { %SYSTEMDRIVE%\Program Files\Common }
  825. CSIDL_COMMON_TEMPLATES = $002D; { %PROFILEPATH%\All Users\Templates }
  826. CSIDL_COMMON_DOCUMENTS = $002E; { %PROFILEPATH%\All Users\Documents }
  827. CSIDL_COMMON_ADMINTOOLS = $002F; { %PROFILEPATH%\All Users\Start Menu\Programs\Administrative Tools }
  828. CSIDL_ADMINTOOLS = $0030; { %USERPROFILE%\Start Menu\Programs\Administrative Tools }
  829. CSIDL_COMMON_MUSIC = $0035; { %PROFILEPATH%\All Users\Documents\my music }
  830. CSIDL_COMMON_PICTURES = $0036; { %PROFILEPATH%\All Users\Documents\my pictures }
  831. CSIDL_COMMON_VIDEO = $0037; { %PROFILEPATH%\All Users\Documents\my videos }
  832. CSIDL_CDBURN_AREA = $003B; { %USERPROFILE%\Local Settings\Application Data\Microsoft\CD Burning }
  833. CSIDL_PROFILES = $003E; { %PROFILEPATH% }
  834. CSIDL_FLAG_CREATE = $8000; { (force creation of requested folder if it doesn't exist yet) }
  835. Type
  836. PFNSHGetFolderPath = Function(Ahwnd: HWND; Csidl: Integer; Token: THandle; Flags: DWord; Path: PChar): HRESULT; stdcall;
  837. var
  838. SHGetFolderPath : PFNSHGetFolderPath = Nil;
  839. CFGDLLHandle : THandle = 0;
  840. Procedure InitDLL;
  841. Var
  842. P : Pointer;
  843. begin
  844. CFGDLLHandle:=LoadLibrary('shell32.dll');
  845. if (CFGDLLHandle<>0) then
  846. begin
  847. P:=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
  848. If (P=Nil) then
  849. begin
  850. FreeLibrary(CFGDLLHandle);
  851. CFGDllHandle:=0;
  852. end
  853. else
  854. SHGetFolderPath:=PFNSHGetFolderPath(P);
  855. end;
  856. If (P=Nil) then
  857. begin
  858. CFGDLLHandle:=LoadLibrary('shfolder.dll');
  859. if (CFGDLLHandle<>0) then
  860. begin
  861. P:=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
  862. If (P=Nil) then
  863. begin
  864. FreeLibrary(CFGDLLHandle);
  865. CFGDllHandle:=0;
  866. end
  867. else
  868. ShGetFolderPath:=PFNSHGetFolderPath(P);
  869. end;
  870. end;
  871. If (@ShGetFolderPath=Nil) then
  872. Raise Exception.Create('Could not determine SHGetFolderPath Function');
  873. end;
  874. Function GetSpecialDir(ID : Integer) : String;
  875. Var
  876. APath : Array[0..MAX_PATH] of char;
  877. begin
  878. Result:='';
  879. if (CFGDLLHandle=0) then
  880. InitDLL;
  881. If (SHGetFolderPath<>Nil) then
  882. begin
  883. if SHGetFolderPath(0,ID or CSIDL_FLAG_CREATE,0,0,@APATH[0])=S_OK then
  884. Result:=IncludeTrailingPathDelimiter(StrPas(@APath[0]));
  885. end;
  886. end;
  887. Function GetAppConfigDir(Global : Boolean) : String;
  888. begin
  889. If Global then
  890. Result:=GetSpecialDir(CSIDL_COMMON_APPDATA)
  891. else
  892. Result:=GetSpecialDir(CSIDL_LOCAL_APPDATA);
  893. If (Result<>'') then
  894. begin
  895. if VendorName<>'' then
  896. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  897. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  898. end
  899. else
  900. Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
  901. end;
  902. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  903. begin
  904. result:=DGetAppConfigFile(Global,SubDir);
  905. end;
  906. Function GetUserDir : String;
  907. begin
  908. Result:=GetSpecialDir(CSIDL_PROFILE);
  909. end;
  910. Procedure InitSysConfigDir;
  911. begin
  912. SetLength(SysConfigDir, MAX_PATH);
  913. SetLength(SysConfigDir, GetWindowsDirectory(PChar(SysConfigDir), MAX_PATH));
  914. end;
  915. {****************************************************************************
  916. Target Dependent WideString stuff
  917. ****************************************************************************}
  918. { This is the case of Win9x. Limited to current locale of course, but it's better
  919. than not working at all. }
  920. function DoCompareStringA(const s1, s2: WideString; Flags: DWORD): PtrInt;
  921. var
  922. a1, a2: AnsiString;
  923. begin
  924. a1:=s1;
  925. a2:=s2;
  926. SetLastError(0);
  927. Result:=CompareStringA(LOCALE_USER_DEFAULT,Flags,pchar(a1),
  928. length(a1),pchar(a2),length(a2))-2;
  929. end;
  930. function DoCompareStringW(const s1, s2: WideString; Flags: DWORD): PtrInt;
  931. begin
  932. SetLastError(0);
  933. Result:=CompareStringW(LOCALE_USER_DEFAULT,Flags,pwidechar(s1),
  934. length(s1),pwidechar(s2),length(s2))-2;
  935. if GetLastError=0 then
  936. Exit;
  937. if GetLastError=ERROR_CALL_NOT_IMPLEMENTED then // Win9x case
  938. Result:=DoCompareStringA(s1, s2, Flags);
  939. if GetLastError<>0 then
  940. RaiseLastOSError;
  941. end;
  942. function Win32CompareWideString(const s1, s2 : WideString) : PtrInt;
  943. begin
  944. Result:=DoCompareStringW(s1, s2, 0);
  945. end;
  946. function Win32CompareTextWideString(const s1, s2 : WideString) : PtrInt;
  947. begin
  948. Result:=DoCompareStringW(s1, s2, NORM_IGNORECASE);
  949. end;
  950. function Win32AnsiUpperCase(const s: string): string;
  951. begin
  952. if length(s)>0 then
  953. begin
  954. result:=s;
  955. UniqueString(result);
  956. CharUpperBuff(pchar(result),length(result));
  957. end
  958. else
  959. result:='';
  960. end;
  961. function Win32AnsiLowerCase(const s: string): string;
  962. begin
  963. if length(s)>0 then
  964. begin
  965. result:=s;
  966. UniqueString(result);
  967. CharLowerBuff(pchar(result),length(result));
  968. end
  969. else
  970. result:='';
  971. end;
  972. function Win32AnsiCompareStr(const S1, S2: string): PtrInt;
  973. begin
  974. result:=CompareString(LOCALE_USER_DEFAULT,0,pchar(s1),length(s1),
  975. pchar(s2),length(s2))-2;
  976. end;
  977. function Win32AnsiCompareText(const S1, S2: string): PtrInt;
  978. begin
  979. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pchar(s1),length(s1),
  980. pchar(s2),length(s2))-2;
  981. end;
  982. function Win32AnsiStrComp(S1, S2: PChar): PtrInt;
  983. begin
  984. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,-1,s2,-1)-2;
  985. end;
  986. function Win32AnsiStrIComp(S1, S2: PChar): PtrInt;
  987. begin
  988. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,-1,s2,-1)-2;
  989. end;
  990. function Win32AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  991. begin
  992. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,maxlen,s2,maxlen)-2;
  993. end;
  994. function Win32AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  995. begin
  996. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,maxlen,s2,maxlen)-2;
  997. end;
  998. function Win32AnsiStrLower(Str: PChar): PChar;
  999. begin
  1000. CharLower(str);
  1001. result:=str;
  1002. end;
  1003. function Win32AnsiStrUpper(Str: PChar): PChar;
  1004. begin
  1005. CharUpper(str);
  1006. result:=str;
  1007. end;
  1008. { there is a similiar procedure in the system unit which inits the fields which
  1009. are relevant already for the system unit }
  1010. procedure InitWin32Widestrings;
  1011. begin
  1012. //!!! CharLengthPCharProc : function(const Str: PChar): PtrInt;
  1013. widestringmanager.CompareWideStringProc:=@Win32CompareWideString;
  1014. widestringmanager.CompareTextWideStringProc:=@Win32CompareTextWideString;
  1015. widestringmanager.UpperAnsiStringProc:=@Win32AnsiUpperCase;
  1016. widestringmanager.LowerAnsiStringProc:=@Win32AnsiLowerCase;
  1017. widestringmanager.CompareStrAnsiStringProc:=@Win32AnsiCompareStr;
  1018. widestringmanager.CompareTextAnsiStringProc:=@Win32AnsiCompareText;
  1019. widestringmanager.StrCompAnsiStringProc:=@Win32AnsiStrComp;
  1020. widestringmanager.StrICompAnsiStringProc:=@Win32AnsiStrIComp;
  1021. widestringmanager.StrLCompAnsiStringProc:=@Win32AnsiStrLComp;
  1022. widestringmanager.StrLICompAnsiStringProc:=@Win32AnsiStrLIComp;
  1023. widestringmanager.StrLowerAnsiStringProc:=@Win32AnsiStrLower;
  1024. widestringmanager.StrUpperAnsiStringProc:=@Win32AnsiStrUpper;
  1025. end;
  1026. Initialization
  1027. InitWin32Widestrings;
  1028. InitExceptions; { Initialize exceptions. OS independent }
  1029. InitInternational; { Initialize internationalization settings }
  1030. LoadVersionInfo;
  1031. InitSysConfigDir;
  1032. OnBeep:=@SysBeep;
  1033. Finalization
  1034. DoneExceptions;
  1035. if kernel32dll<>0 then
  1036. FreeLibrary(kernel32dll);
  1037. if CFGDLLHandle<>0 then
  1038. FreeLibrary(CFGDllHandle);
  1039. end.