sysutils.pp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. {$MODESWITCH OUT}
  16. { force ansistrings }
  17. {$H+}
  18. uses
  19. windows;
  20. {$DEFINE HAS_SLEEP}
  21. {$DEFINE HAS_OSERROR}
  22. {$DEFINE HAS_OSCONFIG}
  23. {$DEFINE HAS_OSUSERDIR}
  24. {$DEFINE HAS_CREATEGUID}
  25. {$DEFINE HAS_LOCALTIMEZONEOFFSET}
  26. { Include platform independent interface part }
  27. {$i sysutilh.inc}
  28. type
  29. TSystemTime = Windows.TSystemTime;
  30. EWin32Error = class(Exception)
  31. public
  32. ErrorCode : DWORD;
  33. end;
  34. Var
  35. Win32Platform : Longint;
  36. Win32MajorVersion,
  37. Win32MinorVersion,
  38. Win32BuildNumber : dword;
  39. Win32CSDVersion : ShortString; // CSD record is 128 bytes only?
  40. const
  41. MaxEraCount = 7;
  42. var
  43. EraNames: array [1..MaxEraCount] of String;
  44. EraYearOffsets: array [1..MaxEraCount] of Integer;
  45. { Compatibility with Delphi }
  46. function Win32Check(res:boolean):boolean;inline;
  47. function WinCheck(res:boolean):boolean;
  48. function CheckWin32Version(Major,Minor : Integer ): Boolean;
  49. function CheckWin32Version(Major : Integer): Boolean;
  50. Procedure RaiseLastWin32Error;
  51. function GetFileVersion(const AFileName: string): Cardinal;
  52. procedure GetFormatSettings;
  53. procedure GetLocaleFormatSettings(LCID: Integer; var FormatSettings: TFormatSettings); platform;
  54. implementation
  55. uses
  56. sysconst,
  57. windirs;
  58. function WinCheck(res:boolean):boolean;
  59. begin
  60. if not res then
  61. RaiseLastOSError;
  62. result:=res;
  63. end;
  64. function Win32Check(res:boolean):boolean;inline;
  65. begin
  66. result:=WinCheck(res);
  67. end;
  68. procedure RaiseLastWin32Error;
  69. begin
  70. RaiseLastOSError;
  71. end;
  72. function CheckWin32Version(Major : Integer): Boolean;
  73. begin
  74. Result:=CheckWin32Version(Major,0)
  75. end;
  76. function CheckWin32Version(Major,Minor: Integer): Boolean;
  77. begin
  78. Result:=(Win32MajorVersion>dword(Major)) or
  79. ((Win32MajorVersion=dword(Major)) and (Win32MinorVersion>=dword(Minor)));
  80. end;
  81. function GetFileVersion(const AFileName:string):Cardinal;
  82. var
  83. { useful only as long as we don't need to touch different stack pages }
  84. buf : array[0..3071] of byte;
  85. bufp : pointer;
  86. fn : string;
  87. valsize,
  88. size : DWORD;
  89. h : DWORD;
  90. valrec : PVSFixedFileInfo;
  91. begin
  92. result:=$fffffff;
  93. fn:=AFileName;
  94. UniqueString(fn);
  95. size:=GetFileVersionInfoSize(pchar(fn),@h);
  96. if size>sizeof(buf) then
  97. begin
  98. getmem(bufp,size);
  99. try
  100. if GetFileVersionInfo(pchar(fn),h,size,bufp) then
  101. if VerQueryValue(bufp,'\',valrec,valsize) then
  102. result:=valrec^.dwFileVersionMS;
  103. finally
  104. freemem(bufp);
  105. end;
  106. end
  107. else
  108. begin
  109. if GetFileVersionInfo(pchar(fn),h,size,@buf) then
  110. if VerQueryValue(@buf,'\',valrec,valsize) then
  111. result:=valrec^.dwFileVersionMS;
  112. end;
  113. end;
  114. {$define HASCREATEGUID}
  115. {$define HASEXPANDUNCFILENAME}
  116. {$DEFINE FPC_NOGENERICANSIROUTINES}
  117. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  118. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  119. { used OS file system APIs use ansistring }
  120. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  121. function ConvertEraYearString(Count ,Year,Month,Day : integer) : string; forward;
  122. function ConvertEraString(Count ,Year,Month,Day : integer) : string; forward;
  123. { Include platform independent implementation part }
  124. {$i sysutils.inc}
  125. function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
  126. begin
  127. Result:= Windows.GetTempFileNameA(Dir,Prefix,uUnique,TempFileName);
  128. end;
  129. { UUID generation. }
  130. function CoCreateGuid(out guid: TGUID): HResult; stdcall; external 'ole32.dll' name 'CoCreateGuid';
  131. function SysCreateGUID(out Guid: TGUID): Integer;
  132. begin
  133. Result := Integer(CoCreateGuid(Guid));
  134. end;
  135. function ExpandUNCFileName (const filename:string) : string;
  136. { returns empty string on errors }
  137. var
  138. s : ansistring;
  139. size : dword;
  140. rc : dword;
  141. buf : pchar;
  142. begin
  143. s := ExpandFileName (filename);
  144. s := s + #0;
  145. size := max_path;
  146. getmem(buf,size);
  147. try
  148. rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  149. if rc=ERROR_MORE_DATA then
  150. begin
  151. buf:=reallocmem(buf,size);
  152. rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  153. end;
  154. if rc = NO_ERROR then
  155. Result := PRemoteNameInfo(buf)^.lpUniversalName
  156. else if rc = ERROR_NOT_CONNECTED then
  157. Result := filename
  158. else
  159. Result := '';
  160. finally
  161. freemem(buf);
  162. end;
  163. end;
  164. {****************************************************************************
  165. File Functions
  166. ****************************************************************************}
  167. const
  168. AccessMode: array[0..2] of Cardinal = (
  169. GENERIC_READ,
  170. GENERIC_WRITE,
  171. GENERIC_READ or GENERIC_WRITE);
  172. ShareModes: array[0..4] of Integer = (
  173. 0,
  174. 0,
  175. FILE_SHARE_READ,
  176. FILE_SHARE_WRITE,
  177. FILE_SHARE_READ or FILE_SHARE_WRITE);
  178. Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
  179. Var
  180. SystemFileName: RawByteString;
  181. begin
  182. SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
  183. result := CreateFile(PChar(SystemFileName), dword(AccessMode[Mode and 3]),
  184. dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
  185. FILE_ATTRIBUTE_NORMAL, 0);
  186. //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
  187. end;
  188. Function FileCreate (Const FileName : RawByteString) : THandle;
  189. begin
  190. FileCreate:=FileCreate(FileName, fmShareExclusive, 0);
  191. end;
  192. Function FileCreate (Const FileName : RawByteString; Rights:longint) : THandle;
  193. begin
  194. FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
  195. end;
  196. Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
  197. Var
  198. SystemFileName: RawByteString;
  199. begin
  200. SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
  201. Result := CreateFile(PChar(SystemFileName), GENERIC_READ or GENERIC_WRITE,
  202. dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  203. end;
  204. Function FileRead (Handle : THandle; out Buffer; Count : longint) : Longint;
  205. Var
  206. res : dword;
  207. begin
  208. if ReadFile(Handle, Buffer, Count, res, nil) then
  209. FileRead:=Res
  210. else
  211. FileRead:=-1;
  212. end;
  213. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  214. Var
  215. Res : dword;
  216. begin
  217. if WriteFile(Handle, Buffer, Count, Res, nil) then
  218. FileWrite:=Res
  219. else
  220. FileWrite:=-1;
  221. end;
  222. Function FileSeek (Handle : THandle;FOffset,Origin : Longint) : Longint;
  223. begin
  224. Result := longint(SetFilePointer(Handle, FOffset, nil, Origin));
  225. end;
  226. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  227. var
  228. rslt: Int64Rec;
  229. begin
  230. rslt := Int64Rec(FOffset);
  231. rslt.lo := SetFilePointer(Handle, rslt.lo, @rslt.hi, Origin);
  232. if (rslt.lo = $FFFFFFFF) and (GetLastError <> 0) then
  233. rslt.hi := $FFFFFFFF;
  234. Result := Int64(rslt);
  235. end;
  236. Procedure FileClose (Handle : THandle);
  237. begin
  238. if Handle<=4 then
  239. exit;
  240. CloseHandle(Handle);
  241. end;
  242. Function FileTruncate (Handle : THandle;Size: Int64) : boolean;
  243. begin
  244. {
  245. Result:=longint(SetFilePointer(handle,Size,nil,FILE_BEGIN))<>-1;
  246. }
  247. if FileSeek (Handle, Size, FILE_BEGIN) = Size then
  248. Result:=SetEndOfFile(handle)
  249. else
  250. Result := false;
  251. end;
  252. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  253. var
  254. lft : TFileTime;
  255. begin
  256. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,@lft) and
  257. LocalFileTimeToFileTime(lft,Wtime);
  258. end;
  259. Function WinToDosTime (Var Wtime : TFileTime;var DTime:longint):longbool;
  260. var
  261. lft : TFileTime;
  262. begin
  263. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  264. FileTimeToDosDateTime(lft,Longrec(Dtime).Hi,LongRec(DTIME).lo);
  265. end;
  266. Function FileAge (Const FileName : String): Longint;
  267. var
  268. Handle: THandle;
  269. FindData: TWin32FindData;
  270. begin
  271. Handle := FindFirstFile(Pchar(FileName), FindData);
  272. if Handle <> INVALID_HANDLE_VALUE then
  273. begin
  274. Windows.FindClose(Handle);
  275. if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  276. If WinToDosTime(FindData.ftLastWriteTime,Result) then
  277. exit;
  278. end;
  279. Result := -1;
  280. end;
  281. Function FileExists (Const FileName : String) : Boolean;
  282. var
  283. Attr:Dword;
  284. begin
  285. Attr:=GetFileAttributes(PChar(FileName));
  286. if Attr <> $ffffffff then
  287. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0
  288. else
  289. Result:=False;
  290. end;
  291. Function DirectoryExists (Const Directory : String) : Boolean;
  292. var
  293. Attr:Dword;
  294. begin
  295. Attr:=GetFileAttributes(PChar(Directory));
  296. if Attr <> $ffffffff then
  297. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) > 0
  298. else
  299. Result:=False;
  300. end;
  301. Function FindMatch(var f: TSearchRec) : Longint;
  302. begin
  303. { Find file with correct attribute }
  304. While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
  305. begin
  306. if not FindNextFile (F.FindHandle,F.FindData) then
  307. begin
  308. Result:=GetLastError;
  309. exit;
  310. end;
  311. end;
  312. { Convert some attributes back }
  313. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  314. f.size:=F.FindData.NFileSizeLow+(qword(maxdword)+1)*F.FindData.NFileSizeHigh;
  315. f.attr:=F.FindData.dwFileAttributes;
  316. f.Name:=StrPas(@F.FindData.cFileName[0]);
  317. Result:=0;
  318. end;
  319. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  320. begin
  321. Rslt.Name:=Path;
  322. Rslt.Attr:=attr;
  323. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  324. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  325. { FindFirstFile is a Win32 Call }
  326. Rslt.FindHandle:=FindFirstFile (PChar(Path),Rslt.FindData);
  327. If Rslt.FindHandle=Invalid_Handle_value then
  328. begin
  329. Result:=GetLastError;
  330. exit;
  331. end;
  332. { Find file with correct attribute }
  333. Result:=FindMatch(Rslt);
  334. end;
  335. Function FindNext (Var Rslt : TSearchRec) : Longint;
  336. begin
  337. if FindNextFile(Rslt.FindHandle, Rslt.FindData) then
  338. Result := FindMatch(Rslt)
  339. else
  340. Result := GetLastError;
  341. end;
  342. Procedure FindClose (Var F : TSearchrec);
  343. begin
  344. if F.FindHandle <> INVALID_HANDLE_VALUE then
  345. Windows.FindClose(F.FindHandle);
  346. end;
  347. Function FileGetDate (Handle : THandle) : Longint;
  348. Var
  349. FT : TFileTime;
  350. begin
  351. If GetFileTime(Handle,nil,nil,@ft) and
  352. WinToDosTime(FT,Result) then
  353. exit;
  354. Result:=-1;
  355. end;
  356. Function FileSetDate (Handle : THandle;Age : Longint) : Longint;
  357. Var
  358. FT: TFileTime;
  359. begin
  360. Result := 0;
  361. if DosToWinTime(Age,FT) and
  362. SetFileTime(Handle, nil, nil, @FT) then
  363. Exit;
  364. Result := GetLastError;
  365. end;
  366. Function FileGetAttr (Const FileName : String) : Longint;
  367. begin
  368. Result:=Longint(GetFileAttributes(PChar(FileName)));
  369. end;
  370. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  371. begin
  372. if SetFileAttributes(PChar(FileName), Attr) then
  373. Result:=0
  374. else
  375. Result := GetLastError;
  376. end;
  377. Function DeleteFile (Const FileName : String) : Boolean;
  378. begin
  379. Result:=Windows.DeleteFile(Pchar(FileName));
  380. end;
  381. Function RenameFile (Const OldName, NewName : String) : Boolean;
  382. begin
  383. Result := MoveFile(PChar(OldName), PChar(NewName));
  384. end;
  385. {****************************************************************************
  386. Disk Functions
  387. ****************************************************************************}
  388. type
  389. TGetDiskFreeSpaceEx = function(drive:pchar;var availableforcaller,total,free):longbool;stdcall;
  390. var
  391. GetDiskFreeSpaceEx : TGetDiskFreeSpaceEx;
  392. function diskfree(drive : byte) : int64;
  393. var
  394. disk : array[1..4] of char;
  395. secs,bytes,
  396. free,total : dword;
  397. qwtotal,qwfree,qwcaller : int64;
  398. begin
  399. if drive=0 then
  400. begin
  401. disk[1]:='\';
  402. disk[2]:=#0;
  403. end
  404. else
  405. begin
  406. disk[1]:=chr(drive+64);
  407. disk[2]:=':';
  408. disk[3]:='\';
  409. disk[4]:=#0;
  410. end;
  411. if assigned(GetDiskFreeSpaceEx) then
  412. begin
  413. if GetDiskFreeSpaceEx(@disk[1],qwcaller,qwtotal,qwfree) then
  414. diskfree:=qwfree
  415. else
  416. diskfree:=-1;
  417. end
  418. else
  419. begin
  420. if GetDiskFreeSpace(@disk[1],secs,bytes,free,total) then
  421. diskfree:=int64(free)*secs*bytes
  422. else
  423. diskfree:=-1;
  424. end;
  425. end;
  426. function disksize(drive : byte) : int64;
  427. var
  428. disk : array[1..4] of char;
  429. secs,bytes,
  430. free,total : dword;
  431. qwtotal,qwfree,qwcaller : int64;
  432. begin
  433. if drive=0 then
  434. begin
  435. disk[1]:='\';
  436. disk[2]:=#0;
  437. end
  438. else
  439. begin
  440. disk[1]:=chr(drive+64);
  441. disk[2]:=':';
  442. disk[3]:='\';
  443. disk[4]:=#0;
  444. end;
  445. if assigned(GetDiskFreeSpaceEx) then
  446. begin
  447. if GetDiskFreeSpaceEx(@disk[1],qwcaller,qwtotal,qwfree) then
  448. disksize:=qwtotal
  449. else
  450. disksize:=-1;
  451. end
  452. else
  453. begin
  454. if GetDiskFreeSpace(@disk[1],secs,bytes,free,total) then
  455. disksize:=int64(total)*secs*bytes
  456. else
  457. disksize:=-1;
  458. end;
  459. end;
  460. Function GetCurrentDir : String;
  461. begin
  462. GetDir(0, result);
  463. end;
  464. Function SetCurrentDir (Const NewDir : String) : Boolean;
  465. begin
  466. Result:=SetCurrentDirectory(PChar(NewDir));
  467. end;
  468. Function CreateDir (Const NewDir : String) : Boolean;
  469. begin
  470. Result:=CreateDirectory(PChar(NewDir),nil);
  471. end;
  472. Function RemoveDir (Const Dir : String) : Boolean;
  473. begin
  474. Result:=RemoveDirectory(PChar(Dir));
  475. end;
  476. {****************************************************************************
  477. Time Functions
  478. ****************************************************************************}
  479. Procedure GetLocalTime(var SystemTime: TSystemTime);
  480. begin
  481. windows.Getlocaltime(SystemTime);
  482. end;
  483. function GetLocalTimeOffset: Integer;
  484. var
  485. TZInfo: TTimeZoneInformation;
  486. begin
  487. case GetTimeZoneInformation(TZInfo) of
  488. TIME_ZONE_ID_UNKNOWN:
  489. Result := TZInfo.Bias;
  490. TIME_ZONE_ID_STANDARD:
  491. Result := TZInfo.Bias + TZInfo.StandardBias;
  492. TIME_ZONE_ID_DAYLIGHT:
  493. Result := TZInfo.Bias + TZInfo.DaylightBias;
  494. else
  495. Result := 0;
  496. end;
  497. end;
  498. {****************************************************************************
  499. Misc Functions
  500. ****************************************************************************}
  501. procedure sysbeep;
  502. begin
  503. MessageBeep(0);
  504. end;
  505. {****************************************************************************
  506. Locale Functions
  507. ****************************************************************************}
  508. function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
  509. var
  510. L: Integer;
  511. Buf: array[0..255] of Char;
  512. begin
  513. L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
  514. if L > 0 then
  515. SetString(Result, @Buf[0], L - 1)
  516. else
  517. Result := Def;
  518. end;
  519. function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
  520. var
  521. Buf: array[0..3] of Char; // sdate allows 4 chars.
  522. begin
  523. if GetLocaleInfo(LID, LT, Buf, sizeof(buf)) > 0 then
  524. Result := Buf[0]
  525. else
  526. Result := Def;
  527. end;
  528. function ConvertEraString(Count ,Year,Month,Day : integer) : string;
  529. var
  530. ASystemTime: TSystemTime;
  531. buf: array[0..100] of char;
  532. ALCID : LCID;
  533. PriLangID : Word;
  534. SubLangID : Word;
  535. begin
  536. Result := ''; if (Count<=0) then exit;
  537. DateTimeToSystemTime(EncodeDate(Year,Month,Day),ASystemTime);
  538. ALCID := GetThreadLocale;
  539. // ALCID := SysLocale.DefaultLCID;
  540. if GetDateFormat(ALCID , DATE_USE_ALT_CALENDAR
  541. , @ASystemTime, PChar('gg')
  542. , @buf, SizeOf(buf)) > 0 then
  543. begin
  544. Result := buf;
  545. if Count = 1 then
  546. begin
  547. PriLangID := ALCID and $3FF;
  548. SubLangID := (ALCID and $FFFF) shr 10;
  549. case PriLangID of
  550. LANG_JAPANESE:
  551. begin
  552. Result := Copy(WideString(Result),1,1);
  553. end;
  554. LANG_CHINESE:
  555. if (SubLangID = SUBLANG_CHINESE_TRADITIONAL) then
  556. begin
  557. Result := Copy(WideString(Result),1,1);
  558. end;
  559. end;
  560. end;
  561. end;
  562. // if Result = '' then Result := StringOfChar('G',Count);
  563. end;
  564. function ConvertEraYearString(Count ,Year,Month,Day : integer) : string;
  565. var
  566. ALCID : LCID;
  567. ASystemTime : TSystemTime;
  568. AFormatText : string;
  569. buf : array[0..100] of Char;
  570. begin
  571. Result := '';
  572. DateTimeToSystemTime(EncodeDate(Year,Month,Day),ASystemTime);
  573. if Count <= 2 then
  574. AFormatText := 'yy'
  575. else
  576. AFormatText := 'yyyy';
  577. ALCID := GetThreadLocale;
  578. // ALCID := SysLocale.DefaultLCID;
  579. if GetDateFormat(ALCID, DATE_USE_ALT_CALENDAR
  580. , @ASystemTime, PChar(AFormatText)
  581. , @buf, SizeOf(buf)) > 0 then
  582. begin
  583. Result := buf;
  584. if (Count = 1) and (Result[1] = '0') then
  585. Result := Copy(Result, 2, Length(Result)-1);
  586. end;
  587. end;
  588. Function GetLocaleInt(LID,TP,Def: LongInt): LongInt;
  589. Var
  590. S: String;
  591. C: Integer;
  592. Begin
  593. S:=GetLocaleStr(LID,TP,'0');
  594. Val(S,Result,C);
  595. If C<>0 Then
  596. Result:=Def;
  597. End;
  598. function EnumEraNames(Names: PChar): WINBOOL; stdcall;
  599. var
  600. i : integer;
  601. begin
  602. Result := False;
  603. for i := Low(EraNames) to High(EraNames) do
  604. if (EraNames[i] = '') then
  605. begin
  606. EraNames[i] := Names;
  607. Result := True;
  608. break;
  609. end;
  610. end;
  611. function EnumEraYearOffsets(YearOffsets: PChar): WINBOOL; stdcall;
  612. var
  613. i : integer;
  614. begin
  615. Result := False;
  616. for i := Low(EraYearOffsets) to High(EraYearOffsets) do
  617. if (EraYearOffsets[i] = -1) then
  618. begin
  619. EraYearOffsets[i] := StrToIntDef(YearOffsets, 0);
  620. Result := True;
  621. break;
  622. end;
  623. end;
  624. procedure GetEraNamesAndYearOffsets;
  625. var
  626. ACALID : CALID;
  627. ALCID : LCID;
  628. buf : array[0..10] of char;
  629. i : integer;
  630. begin
  631. for i:= 1 to MaxEraCount do
  632. begin
  633. EraNames[i] := ''; EraYearOffsets[i] := -1;
  634. end;
  635. ALCID := GetThreadLocale;
  636. if GetLocaleInfo(ALCID , LOCALE_IOPTIONALCALENDAR, buf, sizeof(buf)) <= 0 then exit;
  637. ACALID := StrToIntDef(buf,1);
  638. if ACALID in [3..5] then
  639. begin
  640. EnumCalendarInfoA(@EnumEraNames, ALCID, ACALID , CAL_SERASTRING);
  641. EnumCalendarInfoA(@EnumEraYearOffsets, ALCID, ACALID, CAL_IYEAROFFSETRANGE);
  642. end;
  643. (*
  644. 1 CAL_GREGORIAN Gregorian (localized)
  645. 2 CAL_GREGORIAN_US Gregorian (English strings always)
  646. 3 CAL_JAPAN Japanese Emperor Era
  647. 4 CAL_TAIWAN Taiwan Calendar
  648. 5 CAL_KOREA Korean Tangun Era
  649. 6 CAL_HIJRI Hijri (Arabic Lunar)
  650. 7 CAL_THAI Thai
  651. 8 CAL_HEBREW Hebrew (Lunar)
  652. 9 CAL_GREGORIAN_ME_FRENCH Gregorian Middle East French
  653. 10 CAL_GREGORIAN_ARABIC Gregorian Arabic
  654. 11 CAL_GREGORIAN_XLIT_ENGLISH Gregorian transliterated English
  655. 12 CAL_GREGORIAN_XLIT_FRENCH Gregorian transliterated French
  656. 23 CAL_UMALQURA Windows Vista or later: Um Al Qura (Arabic lunar) calendar
  657. *)
  658. end;
  659. procedure GetLocaleFormatSettings(LCID: Integer; var FormatSettings: TFormatSettings);
  660. var
  661. HF : Shortstring;
  662. LID : Windows.LCID;
  663. I,Day : longint;
  664. begin
  665. LID := LCID;
  666. with FormatSettings do
  667. begin
  668. { Date stuff }
  669. for I := 1 to 12 do
  670. begin
  671. ShortMonthNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVMONTHNAME1+I-1,ShortMonthNames[i]);
  672. LongMonthNames[I]:=GetLocaleStr(LID,LOCALE_SMONTHNAME1+I-1,LongMonthNames[i]);
  673. end;
  674. for I := 1 to 7 do
  675. begin
  676. Day := (I + 5) mod 7;
  677. ShortDayNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVDAYNAME1+Day,ShortDayNames[i]);
  678. LongDayNames[I]:=GetLocaleStr(LID,LOCALE_SDAYNAME1+Day,LongDayNames[i]);
  679. end;
  680. DateSeparator := GetLocaleChar(LID, LOCALE_SDATE, '/');
  681. ShortDateFormat := GetLocaleStr(LID, LOCALE_SSHORTDATE, 'm/d/yy');
  682. LongDateFormat := GetLocaleStr(LID, LOCALE_SLONGDATE, 'mmmm d, yyyy');
  683. { Time stuff }
  684. TimeSeparator := GetLocaleChar(LID, LOCALE_STIME, ':');
  685. TimeAMString := GetLocaleStr(LID, LOCALE_S1159, 'AM');
  686. TimePMString := GetLocaleStr(LID, LOCALE_S2359, 'PM');
  687. if StrToIntDef(GetLocaleStr(LID, LOCALE_ITLZERO, '0'), 0) = 0 then
  688. HF:='h'
  689. else
  690. HF:='hh';
  691. // No support for 12 hour stuff at the moment...
  692. ShortTimeFormat := HF+':nn';
  693. LongTimeFormat := HF + ':nn:ss';
  694. { Currency stuff }
  695. CurrencyString:=GetLocaleStr(LID, LOCALE_SCURRENCY, '');
  696. CurrencyFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRENCY, '0'), 0);
  697. NegCurrFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_INEGCURR, '0'), 0);
  698. { Number stuff }
  699. ThousandSeparator:=GetLocaleChar(LID, LOCALE_STHOUSAND, ',');
  700. DecimalSeparator:=GetLocaleChar(LID, LOCALE_SDECIMAL, '.');
  701. CurrencyDecimals:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRDIGITS, '0'), 0);
  702. ListSeparator := GetLocaleChar(LID, LOCALE_SLIST, ',');
  703. end;
  704. end;
  705. procedure GetFormatSettings;
  706. begin
  707. GetlocaleFormatSettings(GetThreadLocale, DefaultFormatSettings);
  708. end;
  709. Procedure InitInternational;
  710. var
  711. { A call to GetSystemMetrics changes the value of the 8087 Control Word on
  712. Pentium4 with WinXP SP2 }
  713. old8087CW: word;
  714. DefaultCustomLocaleID : LCID; // typedef DWORD LCID;
  715. DefaultCustomLanguageID : Word; // typedef WORD LANGID;
  716. begin
  717. /// workaround for Windows 7 bug, see bug report #18574
  718. SetThreadLocale(GetUserDefaultLCID);
  719. InitInternationalGeneric;
  720. old8087CW:=Get8087CW;
  721. SysLocale.MBCS:=GetSystemMetrics(SM_DBCSENABLED)<>0;
  722. SysLocale.RightToLeft:=GetSystemMetrics(SM_MIDEASTENABLED)<>0;
  723. SysLocale.DefaultLCID := $0409;
  724. SysLocale.PriLangID := LANG_ENGLISH;
  725. SysLocale.SubLangID := SUBLANG_ENGLISH_US;
  726. // probably needs update with getthreadlocale. post 2.0.2
  727. DefaultCustomLocaleID := GetThreadLocale;
  728. if DefaultCustomLocaleID <> 0 then
  729. begin
  730. { Locale Identifiers
  731. +-------------+---------+-------------------------+
  732. | Reserved | Sort ID | Language ID |
  733. +-------------+---------+-------------------------+
  734. 31 20 19 16 15 0 bit }
  735. DefaultCustomLanguageID := DefaultCustomLocaleID and $FFFF; // 2^16
  736. if DefaultCustomLanguageID <> 0 then
  737. begin
  738. SysLocale.DefaultLCID := DefaultCustomLocaleID;
  739. { Language Identifiers
  740. +-------------------------+-------------------------+
  741. | SubLanguage ID | Primary Language ID |
  742. +-------------------------+-------------------------+
  743. 15 10 9 0 bit }
  744. SysLocale.PriLangID := DefaultCustomLanguageID and $3ff; // 2^10
  745. SysLocale.SubLangID := DefaultCustomLanguageID shr 10;
  746. end;
  747. end;
  748. Set8087CW(old8087CW);
  749. GetFormatSettings;
  750. if SysLocale.FarEast then GetEraNamesAndYearOffsets;
  751. end;
  752. {****************************************************************************
  753. Target Dependent
  754. ****************************************************************************}
  755. function SysErrorMessage(ErrorCode: Integer): String;
  756. const
  757. MaxMsgSize = Format_Message_Max_Width_Mask;
  758. var
  759. MsgBuffer: pChar;
  760. begin
  761. GetMem(MsgBuffer, MaxMsgSize);
  762. FillChar(MsgBuffer^, MaxMsgSize, #0);
  763. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
  764. nil,
  765. ErrorCode,
  766. MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
  767. MsgBuffer, { This function allocs the memory }
  768. MaxMsgSize, { Maximum message size }
  769. nil);
  770. SysErrorMessage := StrPas(MsgBuffer);
  771. FreeMem(MsgBuffer, MaxMsgSize);
  772. end;
  773. {****************************************************************************
  774. Initialization code
  775. ****************************************************************************}
  776. {$push}
  777. { GetEnvironmentStrings cannot be checked by CheckPointer function }
  778. {$checkpointer off}
  779. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  780. var
  781. s : string;
  782. i : longint;
  783. hp,p : pchar;
  784. begin
  785. Result:='';
  786. p:=GetEnvironmentStrings;
  787. hp:=p;
  788. while hp^<>#0 do
  789. begin
  790. s:=strpas(hp);
  791. i:=pos('=',s);
  792. if uppercase(copy(s,1,i-1))=upcase(envvar) then
  793. begin
  794. Result:=copy(s,i+1,length(s)-i);
  795. break;
  796. end;
  797. { next string entry}
  798. hp:=hp+strlen(hp)+1;
  799. end;
  800. FreeEnvironmentStrings(p);
  801. end;
  802. Function GetEnvironmentVariableCount : Integer;
  803. var
  804. hp,p : pchar;
  805. begin
  806. Result:=0;
  807. p:=GetEnvironmentStrings;
  808. hp:=p;
  809. If (Hp<>Nil) then
  810. while hp^<>#0 do
  811. begin
  812. Inc(Result);
  813. hp:=hp+strlen(hp)+1;
  814. end;
  815. FreeEnvironmentStrings(p);
  816. end;
  817. Function GetEnvironmentString(Index : Integer) : String;
  818. var
  819. hp,p : pchar;
  820. begin
  821. Result:='';
  822. p:=GetEnvironmentStrings;
  823. hp:=p;
  824. If (Hp<>Nil) then
  825. begin
  826. while (hp^<>#0) and (Index>1) do
  827. begin
  828. Dec(Index);
  829. hp:=hp+strlen(hp)+1;
  830. end;
  831. If (hp^<>#0) then
  832. Result:=StrPas(HP);
  833. end;
  834. FreeEnvironmentStrings(p);
  835. end;
  836. {$pop}
  837. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  838. // win specific function
  839. var
  840. SI: TStartupInfo;
  841. PI: TProcessInformation;
  842. Proc : THandle;
  843. l : DWord;
  844. CommandLine : ansistring;
  845. e : EOSError;
  846. ExecInherits : longbool;
  847. begin
  848. FillChar(SI, SizeOf(SI), 0);
  849. SI.cb:=SizeOf(SI);
  850. SI.wShowWindow:=1;
  851. { always surround the name of the application by quotes
  852. so that long filenames will always be accepted. But don't
  853. do it if there are already double quotes, since Win32 does not
  854. like double quotes which are duplicated!
  855. }
  856. if pos('"',path)=0 then
  857. CommandLine:='"'+path+'"'
  858. else
  859. CommandLine:=path;
  860. if ComLine <> '' then
  861. CommandLine:=Commandline+' '+ComLine+#0
  862. else
  863. CommandLine := CommandLine + #0;
  864. ExecInherits:=ExecInheritsHandles in Flags;
  865. if not CreateProcess(nil, pchar(CommandLine),
  866. Nil, Nil, ExecInherits,$20, Nil, Nil, SI, PI) then
  867. begin
  868. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  869. e.ErrorCode:=GetLastError;
  870. raise e;
  871. end;
  872. Proc:=PI.hProcess;
  873. if WaitForSingleObject(Proc, dword($ffffffff)) <> $ffffffff then
  874. begin
  875. GetExitCodeProcess(Proc,l);
  876. CloseHandle(Proc);
  877. CloseHandle(PI.hThread);
  878. result:=l;
  879. end
  880. else
  881. begin
  882. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  883. e.ErrorCode:=GetLastError;
  884. CloseHandle(Proc);
  885. CloseHandle(PI.hThread);
  886. raise e;
  887. end;
  888. end;
  889. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;Flags:TExecuteFlags=[]):integer;
  890. var
  891. CommandLine: AnsiString;
  892. I: integer;
  893. begin
  894. Commandline := '';
  895. for I := 0 to High (ComLine) do
  896. if Pos (' ', ComLine [I]) <> 0 then
  897. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  898. else
  899. CommandLine := CommandLine + ' ' + Comline [I];
  900. ExecuteProcess := ExecuteProcess (Path, CommandLine,Flags);
  901. end;
  902. Procedure Sleep(Milliseconds : Cardinal);
  903. begin
  904. Windows.Sleep(MilliSeconds)
  905. end;
  906. Function GetLastOSError : Integer;
  907. begin
  908. Result:=GetLastError;
  909. end;
  910. {****************************************************************************
  911. Initialization code
  912. ****************************************************************************}
  913. var
  914. kernel32dll : THandle;
  915. Procedure LoadVersionInfo;
  916. // and getfreespaceex
  917. Var
  918. versioninfo : TOSVERSIONINFO;
  919. begin
  920. GetDiskFreeSpaceEx:=nil;
  921. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  922. GetVersionEx(versioninfo);
  923. Win32Platform:=versionInfo.dwPlatformId;
  924. Win32MajorVersion:=versionInfo.dwMajorVersion;
  925. Win32MinorVersion:=versionInfo.dwMinorVersion;
  926. Win32BuildNumber:=versionInfo.dwBuildNumber;
  927. Move (versioninfo.szCSDVersion ,Win32CSDVersion[1],128);
  928. win32CSDVersion[0]:=chr(strlen(pchar(@versioninfo.szCSDVersion)));
  929. kernel32dll:=GetModuleHandle('kernel32');
  930. if kernel32dll<>0 then
  931. GetDiskFreeSpaceEx:=TGetDiskFreeSpaceEx(GetProcAddress(kernel32dll,'GetDiskFreeSpaceExA'));
  932. end;
  933. Function GetAppConfigDir(Global : Boolean) : String;
  934. begin
  935. If Global then
  936. Result:=GetWindowsSpecialDir(CSIDL_COMMON_APPDATA)
  937. else
  938. Result:=GetWindowsSpecialDir(CSIDL_LOCAL_APPDATA);
  939. If (Result<>'') then
  940. begin
  941. if VendorName<>'' then
  942. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  943. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  944. end
  945. else
  946. Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
  947. end;
  948. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  949. begin
  950. result:=DGetAppConfigFile(Global,SubDir);
  951. end;
  952. Function GetUserDir : String;
  953. begin
  954. Result:=GetWindowsSpecialDir(CSIDL_PROFILE);
  955. end;
  956. Procedure InitSysConfigDir;
  957. begin
  958. SetLength(SysConfigDir, MAX_PATH);
  959. SetLength(SysConfigDir, GetWindowsDirectory(PChar(SysConfigDir), MAX_PATH));
  960. end;
  961. {****************************************************************************
  962. Target Dependent WideString stuff
  963. ****************************************************************************}
  964. { This is the case of Win9x. Limited to current locale of course, but it's better
  965. than not working at all. }
  966. function DoCompareStringA(P1, P2: PWideChar; L1, L2: PtrUInt; Flags: DWORD): PtrInt;
  967. var
  968. a1, a2: AnsiString;
  969. begin
  970. if L1>0 then
  971. widestringmanager.Wide2AnsiMoveProc(P1,a1,DefaultSystemCodePage,L1);
  972. if L2>0 then
  973. widestringmanager.Wide2AnsiMoveProc(P2,a2,DefaultSystemCodePage,L2);
  974. SetLastError(0);
  975. Result:=CompareStringA(LOCALE_USER_DEFAULT,Flags,pchar(a1),
  976. length(a1),pchar(a2),length(a2))-2;
  977. end;
  978. function DoCompareStringW(P1, P2: PWideChar; L1, L2: PtrUInt; Flags: DWORD): PtrInt;
  979. begin
  980. SetLastError(0);
  981. Result:=CompareStringW(LOCALE_USER_DEFAULT,Flags,P1,L1,P2,L2)-2;
  982. if GetLastError=0 then
  983. Exit;
  984. if GetLastError=ERROR_CALL_NOT_IMPLEMENTED then // Win9x case
  985. Result:=DoCompareStringA(P1, P2, L1, L2, Flags);
  986. if GetLastError<>0 then
  987. RaiseLastOSError;
  988. end;
  989. function Win32CompareWideString(const s1, s2 : WideString) : PtrInt;
  990. begin
  991. Result:=DoCompareStringW(PWideChar(s1), PWideChar(s2), Length(s1), Length(s2), 0);
  992. end;
  993. function Win32CompareTextWideString(const s1, s2 : WideString) : PtrInt;
  994. begin
  995. Result:=DoCompareStringW(PWideChar(s1), PWideChar(s2), Length(s1), Length(s2), NORM_IGNORECASE);
  996. end;
  997. function Win32AnsiUpperCase(const s: string): string;
  998. begin
  999. if length(s)>0 then
  1000. begin
  1001. result:=s;
  1002. UniqueString(result);
  1003. CharUpperBuff(pchar(result),length(result));
  1004. end
  1005. else
  1006. result:='';
  1007. end;
  1008. function Win32AnsiLowerCase(const s: string): string;
  1009. begin
  1010. if length(s)>0 then
  1011. begin
  1012. result:=s;
  1013. UniqueString(result);
  1014. CharLowerBuff(pchar(result),length(result));
  1015. end
  1016. else
  1017. result:='';
  1018. end;
  1019. function Win32AnsiCompareStr(const S1, S2: string): PtrInt;
  1020. begin
  1021. result:=CompareString(LOCALE_USER_DEFAULT,0,pchar(s1),length(s1),
  1022. pchar(s2),length(s2))-2;
  1023. end;
  1024. function Win32AnsiCompareText(const S1, S2: string): PtrInt;
  1025. begin
  1026. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pchar(s1),length(s1),
  1027. pchar(s2),length(s2))-2;
  1028. end;
  1029. function Win32AnsiStrComp(S1, S2: PChar): PtrInt;
  1030. begin
  1031. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,-1,s2,-1)-2;
  1032. end;
  1033. function Win32AnsiStrIComp(S1, S2: PChar): PtrInt;
  1034. begin
  1035. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,-1,s2,-1)-2;
  1036. end;
  1037. function Win32AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  1038. begin
  1039. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,maxlen,s2,maxlen)-2;
  1040. end;
  1041. function Win32AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  1042. begin
  1043. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,maxlen,s2,maxlen)-2;
  1044. end;
  1045. function Win32AnsiStrLower(Str: PChar): PChar;
  1046. begin
  1047. CharLower(str);
  1048. result:=str;
  1049. end;
  1050. function Win32AnsiStrUpper(Str: PChar): PChar;
  1051. begin
  1052. CharUpper(str);
  1053. result:=str;
  1054. end;
  1055. function Win32CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  1056. begin
  1057. Result:=DoCompareStringW(PWideChar(s1), PWideChar(s2), Length(s1), Length(s2), 0);
  1058. end;
  1059. function Win32CompareTextUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  1060. begin
  1061. Result:=DoCompareStringW(PWideChar(s1), PWideChar(s2), Length(s1), Length(s2), NORM_IGNORECASE);
  1062. end;
  1063. { there is a similiar procedure in the system unit which inits the fields which
  1064. are relevant already for the system unit }
  1065. procedure InitWin32Widestrings;
  1066. begin
  1067. { return value: number of code points in the string. Whenever an invalid
  1068. code point is encountered, all characters part of this invalid code point
  1069. are considered to form one "character" and the next character is
  1070. considered to be the start of a new (possibly also invalid) code point }
  1071. //!!! CharLengthPCharProc : function(const Str: PChar): PtrInt;
  1072. { return value:
  1073. -1 if incomplete or invalid code point
  1074. 0 if NULL character,
  1075. > 0 if that's the length in bytes of the code point }
  1076. //!!!! CodePointLengthProc : function(const Str: PChar; MaxLookAead: PtrInt): Ptrint;
  1077. widestringmanager.CompareWideStringProc:=@Win32CompareWideString;
  1078. widestringmanager.CompareTextWideStringProc:=@Win32CompareTextWideString;
  1079. widestringmanager.UpperAnsiStringProc:=@Win32AnsiUpperCase;
  1080. widestringmanager.LowerAnsiStringProc:=@Win32AnsiLowerCase;
  1081. widestringmanager.CompareStrAnsiStringProc:=@Win32AnsiCompareStr;
  1082. widestringmanager.CompareTextAnsiStringProc:=@Win32AnsiCompareText;
  1083. widestringmanager.StrCompAnsiStringProc:=@Win32AnsiStrComp;
  1084. widestringmanager.StrICompAnsiStringProc:=@Win32AnsiStrIComp;
  1085. widestringmanager.StrLCompAnsiStringProc:=@Win32AnsiStrLComp;
  1086. widestringmanager.StrLICompAnsiStringProc:=@Win32AnsiStrLIComp;
  1087. widestringmanager.StrLowerAnsiStringProc:=@Win32AnsiStrLower;
  1088. widestringmanager.StrUpperAnsiStringProc:=@Win32AnsiStrUpper;
  1089. widestringmanager.CompareUnicodeStringProc:=@Win32CompareUnicodeString;
  1090. widestringmanager.CompareTextUnicodeStringProc:=@Win32CompareTextUnicodeString;
  1091. end;
  1092. { Platform-specific exception support }
  1093. function WinExceptionObject(code: Longint; const rec: TExceptionRecord): Exception;
  1094. var
  1095. entry: PExceptMapEntry;
  1096. begin
  1097. entry := FindExceptMapEntry(code);
  1098. if assigned(entry) then
  1099. result:=entry^.cls.CreateRes(entry^.msg)
  1100. else
  1101. result:=EExternalException.CreateResFmt(@SExternalException,[rec.ExceptionCode]);
  1102. if result is EExternal then
  1103. EExternal(result).FExceptionRecord:=rec;
  1104. end;
  1105. function WinExceptionClass(code: longint): ExceptClass;
  1106. var
  1107. entry: PExceptMapEntry;
  1108. begin
  1109. entry := FindExceptMapEntry(code);
  1110. if assigned(entry) then
  1111. result:=entry^.cls
  1112. else
  1113. result:=EExternalException;
  1114. end;
  1115. Initialization
  1116. InitWin32Widestrings;
  1117. InitExceptions; { Initialize exceptions. OS independent }
  1118. {$ifdef win64} { Nothing win64-specific here, just keeping exe size down
  1119. as these procedures aren't used in generic exception handling }
  1120. ExceptObjProc:=@WinExceptionObject;
  1121. ExceptClsProc:=@WinExceptionClass;
  1122. {$endif win64}
  1123. InitInternational; { Initialize internationalization settings }
  1124. LoadVersionInfo;
  1125. InitSysConfigDir;
  1126. OnBeep:=@SysBeep;
  1127. Finalization
  1128. DoneExceptions;
  1129. end.