sysutils.pp 39 KB

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