sysutils.pp 39 KB

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