sysutils.pp 37 KB

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