sysutils.pp 36 KB

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