sysutils.pp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl and Yury Sidorov
  4. members of the Free Pascal development team
  5. Sysutils unit for wince
  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. dos,
  20. windows;
  21. {$DEFINE HAS_SLEEP}
  22. {$DEFINE HAS_OSERROR}
  23. {$DEFINE HAS_OSCONFIG}
  24. {$DEFINE HAS_TEMPDIR}
  25. { Include platform independent interface part }
  26. {$i sysutilh.inc}
  27. type
  28. TSystemTime = Windows.TSystemTime;
  29. EWinCEError = class(Exception)
  30. public
  31. ErrorCode : DWORD;
  32. end;
  33. Var
  34. WinCEPlatform : Longint;
  35. WinCEMajorVersion,
  36. WinCEMinorVersion,
  37. WinCEBuildNumber : dword;
  38. WinCECSDVersion : ShortString; // CSD record is 128 bytes only?
  39. implementation
  40. uses
  41. sysconst;
  42. {$DEFINE FPC_NOGENERICANSIROUTINES}
  43. {$define HASEXPANDUNCFILENAME}
  44. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  45. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  46. { Include platform independent implementation part }
  47. {$i sysutils.inc}
  48. procedure PWideCharToString(const str: PWideChar; out Result: string; strlen: longint = -1);
  49. var
  50. len: longint;
  51. begin
  52. if (strlen < 1) and (str^ = #0) then
  53. Result:=''
  54. else
  55. begin
  56. while True do begin
  57. if strlen <> -1 then
  58. len:=strlen + 1
  59. else
  60. len:=WideToAnsiBuf(str, -1, nil, 0);
  61. if len > 0 then
  62. begin
  63. SetLength(Result, len - 1);
  64. if (WideToAnsiBuf(str, strlen, @Result[1], len) = 0) and (strlen <> -1) then
  65. begin
  66. strlen:=-1;
  67. continue;
  68. end;
  69. end
  70. else
  71. Result:='';
  72. break;
  73. end;
  74. end;
  75. end;
  76. function ExpandUNCFileName (const filename:string) : string;
  77. { returns empty string on errors }
  78. var
  79. s : widestring;
  80. size : dword;
  81. rc : dword;
  82. buf : pwidechar;
  83. begin
  84. s := ExpandFileName (filename);
  85. size := max_path*SizeOf(WideChar);
  86. getmem(buf,size);
  87. try
  88. rc := WNetGetUniversalName (pwidechar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  89. if rc=ERROR_MORE_DATA then
  90. begin
  91. buf:=reallocmem(buf,size);
  92. rc := WNetGetUniversalName (pwidechar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
  93. end;
  94. if rc = NO_ERROR then
  95. Result := PRemoteNameInfo(buf)^.lpUniversalName
  96. else if rc = ERROR_NOT_CONNECTED then
  97. Result := filename
  98. else
  99. Result := '';
  100. finally
  101. freemem(buf);
  102. end;
  103. end;
  104. {****************************************************************************
  105. File Functions
  106. ****************************************************************************}
  107. Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
  108. const
  109. AccessMode: array[0..2] of Cardinal = (
  110. GENERIC_READ,
  111. GENERIC_WRITE,
  112. GENERIC_READ or GENERIC_WRITE);
  113. ShareMode: array[0..4] of Integer = (
  114. 0,
  115. 0,
  116. FILE_SHARE_READ,
  117. FILE_SHARE_WRITE,
  118. FILE_SHARE_READ or FILE_SHARE_WRITE);
  119. var
  120. fn: PWideChar;
  121. begin
  122. fn:=StringToPWideChar(FileName);
  123. result := CreateFile(fn, dword(AccessMode[Mode and 3]),
  124. dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
  125. FILE_ATTRIBUTE_NORMAL, 0);
  126. //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
  127. FreeMem(fn);
  128. end;
  129. Function FileCreate (Const FileName : String) : THandle;
  130. var
  131. fn: PWideChar;
  132. begin
  133. fn:=StringToPWideChar(FileName);
  134. Result := CreateFile(fn, GENERIC_READ or GENERIC_WRITE,
  135. 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  136. FreeMem(fn);
  137. end;
  138. Function FileCreate (Const FileName : String; Mode:longint) : THandle;
  139. begin
  140. FileCreate:=FileCreate(FileName);
  141. end;
  142. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
  143. Var
  144. res : dword;
  145. begin
  146. if ReadFile(Handle, Buffer, Count, res, nil) then
  147. FileRead:=Res
  148. else
  149. FileRead:=-1;
  150. end;
  151. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  152. Var
  153. Res : dword;
  154. begin
  155. if WriteFile(Handle, Buffer, Count, Res, nil) then
  156. FileWrite:=Res
  157. else
  158. FileWrite:=-1;
  159. end;
  160. Function FileSeek (Handle : THandle;FOffset,Origin : Longint) : Longint;
  161. begin
  162. Result := longint(SetFilePointer(Handle, FOffset, nil, Origin));
  163. end;
  164. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  165. begin
  166. Result := SetFilePointer(Handle, longint(FOffset), nil, longint(Origin));
  167. end;
  168. Procedure FileClose (Handle : THandle);
  169. begin
  170. if Handle<=4 then
  171. exit;
  172. CloseHandle(Handle);
  173. end;
  174. Function FileTruncate (Handle : THandle;Size: Int64) : boolean;
  175. begin
  176. if FileSeek (Handle, Size, FILE_BEGIN) = Size then
  177. Result:=SetEndOfFile(handle)
  178. else
  179. Result := false;
  180. end;
  181. Function DosToWinTime (DTime:longint; out Wtime : TFileTime):longbool;
  182. begin
  183. DosToWinTime:=dos.DosToWinTime(DTime, Wtime);
  184. end;
  185. Function WinToDosTime (Const Wtime : TFileTime; out DTime:longint):longbool;
  186. begin
  187. WinToDosTime:=dos.WinToDosTime(Wtime, DTime);
  188. end;
  189. Function FileAge (Const FileName : String): Longint;
  190. var
  191. Handle: THandle;
  192. FindData: TWin32FindData;
  193. fn: PWideChar;
  194. begin
  195. fn:=StringToPWideChar(FileName);
  196. Handle := FindFirstFile(fn, FindData);
  197. FreeMem(fn);
  198. if Handle <> INVALID_HANDLE_VALUE then
  199. begin
  200. Windows.FindClose(Handle);
  201. if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  202. If WinToDosTime(FindData.ftLastWriteTime,Result) then
  203. exit;
  204. end;
  205. Result := -1;
  206. end;
  207. Function FileExists (Const FileName : String) : Boolean;
  208. var
  209. Attr:Dword;
  210. begin
  211. Attr:=FileGetAttr(FileName);
  212. if Attr <> $ffffffff then
  213. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0
  214. else
  215. Result:=False;
  216. end;
  217. Function DirectoryExists (Const Directory : String) : Boolean;
  218. var
  219. Attr:Dword;
  220. begin
  221. Attr:=FileGetAttr(Directory);
  222. if Attr <> $ffffffff then
  223. Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) <> 0
  224. else
  225. Result:=False;
  226. end;
  227. Function FindMatch(var f: TSearchRec) : Longint;
  228. begin
  229. { Find file with correct attribute }
  230. While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
  231. begin
  232. if not FindNextFile (F.FindHandle,F.FindData) then
  233. begin
  234. Result:=GetLastError;
  235. exit;
  236. end;
  237. end;
  238. { Convert some attributes back }
  239. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  240. f.size:=F.FindData.NFileSizeLow;
  241. f.attr:=F.FindData.dwFileAttributes;
  242. PWideCharToString(@F.FindData.cFileName[0], f.Name);
  243. Result:=0;
  244. end;
  245. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  246. var
  247. fn: PWideChar;
  248. begin
  249. fn:=StringToPWideChar(Path);
  250. Rslt.Name:=Path;
  251. Rslt.Attr:=attr;
  252. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  253. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  254. { FindFirstFile is a WinCE Call }
  255. Rslt.FindHandle:=FindFirstFile (fn, Rslt.FindData);
  256. FreeMem(fn);
  257. If Rslt.FindHandle=Invalid_Handle_value then
  258. begin
  259. Result:=GetLastError;
  260. exit;
  261. end;
  262. { Find file with correct attribute }
  263. Result:=FindMatch(Rslt);
  264. end;
  265. Function FindNext (Var Rslt : TSearchRec) : Longint;
  266. begin
  267. if FindNextFile(Rslt.FindHandle, Rslt.FindData) then
  268. Result := FindMatch(Rslt)
  269. else
  270. Result := GetLastError;
  271. end;
  272. Procedure FindClose (Var F : TSearchrec);
  273. begin
  274. if F.FindHandle <> INVALID_HANDLE_VALUE then
  275. Windows.FindClose(F.FindHandle);
  276. end;
  277. Function FileGetDate (Handle : THandle) : Longint;
  278. Var
  279. FT : TFileTime;
  280. begin
  281. If GetFileTime(Handle,nil,nil,@ft) and
  282. WinToDosTime(FT, Result) then
  283. exit;
  284. Result:=-1;
  285. end;
  286. Function FileSetDate (Handle : THandle;Age : Longint) : Longint;
  287. Var
  288. FT: TFileTime;
  289. begin
  290. Result := 0;
  291. if DosToWinTime(Age, FT) and SetFileTime(Handle, FT, FT, FT) then
  292. Exit;
  293. Result := GetLastError;
  294. end;
  295. Function FileGetAttr (Const FileName : String) : Longint;
  296. var
  297. fn: PWideChar;
  298. begin
  299. fn:=StringToPWideChar(FileName);
  300. Result:=GetFileAttributes(fn);
  301. FreeMem(fn);
  302. end;
  303. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  304. var
  305. fn: PWideChar;
  306. begin
  307. fn:=StringToPWideChar(FileName);
  308. if not SetFileAttributes(fn, Attr) then
  309. Result := GetLastError
  310. else
  311. Result:=0;
  312. FreeMem(fn);
  313. end;
  314. Function DeleteFile (Const FileName : String) : Boolean;
  315. var
  316. fn: PWideChar;
  317. begin
  318. fn:=StringToPWideChar(FileName);
  319. DeleteFile:=Windows.DeleteFile(fn);
  320. FreeMem(fn);
  321. end;
  322. Function RenameFile (Const OldName, NewName : String) : Boolean;
  323. var
  324. fold, fnew: PWideChar;
  325. begin
  326. fold:=StringToPWideChar(OldName);
  327. fnew:=StringToPWideChar(NewName);
  328. Result := MoveFile(fold, fnew);
  329. FreeMem(fnew);
  330. FreeMem(fold);
  331. end;
  332. {****************************************************************************
  333. Disk Functions
  334. ****************************************************************************}
  335. function diskfree(drive : byte) : int64;
  336. begin
  337. Result := Dos.diskfree(drive);
  338. end;
  339. function disksize(drive : byte) : int64;
  340. begin
  341. Result := Dos.disksize(drive);
  342. end;
  343. Function GetCurrentDir : String;
  344. begin
  345. GetDir(0, result);
  346. end;
  347. Function SetCurrentDir (Const NewDir : String) : Boolean;
  348. begin
  349. {$I-}
  350. ChDir(NewDir);
  351. {$I+}
  352. result := (IOResult = 0);
  353. end;
  354. Function CreateDir (Const NewDir : String) : Boolean;
  355. begin
  356. {$I-}
  357. MkDir(NewDir);
  358. {$I+}
  359. result := (IOResult = 0);
  360. end;
  361. Function RemoveDir (Const Dir : String) : Boolean;
  362. begin
  363. {$I-}
  364. RmDir(Dir);
  365. {$I+}
  366. result := (IOResult = 0);
  367. end;
  368. {****************************************************************************
  369. Time Functions
  370. ****************************************************************************}
  371. Procedure GetLocalTime(var SystemTime: TSystemTime);
  372. begin
  373. windows.Getlocaltime(SystemTime);
  374. end;
  375. {****************************************************************************
  376. Misc Functions
  377. ****************************************************************************}
  378. procedure SysBeep;
  379. begin
  380. MessageBeep(0);
  381. end;
  382. {****************************************************************************
  383. Locale Functions
  384. ****************************************************************************}
  385. function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
  386. var
  387. L: Integer;
  388. Buf: array[0..255] of WideChar;
  389. s: widestring;
  390. begin
  391. L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf) div SizeOf(WideChar));
  392. if L > 0 then
  393. begin
  394. SetString(s, Buf, L - 1);
  395. Result:=s;
  396. end
  397. else
  398. Result := Def;
  399. end;
  400. function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
  401. var
  402. Buf: array[0..1] of WideChar;
  403. Buf2: array[0..1] of Char;
  404. begin
  405. if GetLocaleInfo(LID, LT, Buf, 2) > 0 then
  406. begin
  407. WideToAnsiBuf(Buf, 1, Buf2, SizeOf(Buf2));
  408. Result := Buf2[0];
  409. end
  410. else
  411. Result := Def;
  412. end;
  413. Function GetLocaleInt(LID,TP,Def: LongInt): LongInt;
  414. Var
  415. S: String;
  416. C: Integer;
  417. Begin
  418. S:=GetLocaleStr(LID,TP,'0');
  419. Val(S,Result,C);
  420. If C<>0 Then
  421. Result:=Def;
  422. End;
  423. procedure GetFormatSettings;
  424. var
  425. HF : Shortstring;
  426. LID : LCID;
  427. I,Day,DateOrder : longint;
  428. begin
  429. LID := GetUserDefaultLCID;
  430. { Date stuff }
  431. for I := 1 to 12 do
  432. begin
  433. ShortMonthNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVMONTHNAME1+I-1,ShortMonthNames[i]);
  434. LongMonthNames[I]:=GetLocaleStr(LID,LOCALE_SMONTHNAME1+I-1,LongMonthNames[i]);
  435. end;
  436. for I := 1 to 7 do
  437. begin
  438. Day := (I + 5) mod 7;
  439. ShortDayNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVDAYNAME1+Day,ShortDayNames[i]);
  440. LongDayNames[I]:=GetLocaleStr(LID,LOCALE_SDAYNAME1+Day,LongDayNames[i]);
  441. end;
  442. DateSeparator := GetLocaleChar(LID, LOCALE_SDATE, '/');
  443. DateOrder := GetLocaleInt(LID, LOCALE_IDate, 0);
  444. Case DateOrder Of
  445. 1: Begin
  446. ShortDateFormat := 'dd/mm/yyyy';
  447. LongDateFormat := 'dddd, d. mmmm yyyy';
  448. End;
  449. 2: Begin
  450. ShortDateFormat := 'yyyy/mm/dd';
  451. LongDateFormat := 'dddd, yyyy mmmm d.';
  452. End;
  453. else
  454. // Default american settings...
  455. ShortDateFormat := 'mm/dd/yyyy';
  456. LongDateFormat := 'dddd, mmmm d. yyyy';
  457. End;
  458. { Time stuff }
  459. TimeSeparator := GetLocaleChar(LID, LOCALE_STIME, ':');
  460. TimeAMString := GetLocaleStr(LID, LOCALE_S1159, 'AM');
  461. TimePMString := GetLocaleStr(LID, LOCALE_S2359, 'PM');
  462. if StrToIntDef(GetLocaleStr(LID, LOCALE_ITLZERO, '0'), 0) = 0 then
  463. HF:='h'
  464. else
  465. HF:='hh';
  466. // No support for 12 hour stuff at the moment...
  467. ShortTimeFormat := HF+':nn';
  468. LongTimeFormat := HF + ':nn:ss';
  469. { Currency stuff }
  470. CurrencyString:=GetLocaleStr(LID, LOCALE_SCURRENCY, '');
  471. CurrencyFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRENCY, '0'), 0);
  472. NegCurrFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_INEGCURR, '0'), 0);
  473. { Number stuff }
  474. ThousandSeparator:=GetLocaleChar(LID, LOCALE_STHOUSAND, ',');
  475. DecimalSeparator:=GetLocaleChar(LID, LOCALE_SDECIMAL, '.');
  476. CurrencyDecimals:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRDIGITS, '0'), 0);
  477. end;
  478. Procedure InitInternational;
  479. begin
  480. InitInternationalGeneric;
  481. SysLocale.MBCS:=GetSystemMetrics(SM_DBCSENABLED)<>0;
  482. SysLocale.RightToLeft:=GetSystemMetrics(SM_MIDEASTENABLED)<>0;
  483. GetFormatSettings;
  484. end;
  485. {****************************************************************************
  486. Target Dependent
  487. ****************************************************************************}
  488. function SysErrorMessage(ErrorCode: Integer): String;
  489. var
  490. MsgBuffer: PWideChar;
  491. len: longint;
  492. begin
  493. MsgBuffer:=nil;
  494. len:=FormatMessage(
  495. FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS,
  496. nil,
  497. ErrorCode,
  498. 0,
  499. @MsgBuffer, { This function allocs the memory (in this case you pass a PPwidechar)}
  500. 0,
  501. nil);
  502. if MsgBuffer <> nil then begin
  503. while (len > 0) and (MsgBuffer[len - 1] <= #32) do
  504. Dec(len);
  505. MsgBuffer[len]:=#0;
  506. PWideCharToString(MsgBuffer, Result);
  507. LocalFree(HLOCAL(MsgBuffer));
  508. end
  509. else
  510. Result:='';
  511. end;
  512. {****************************************************************************
  513. Initialization code
  514. ****************************************************************************}
  515. // WinCE does not have environment. It can be emulated via registry or file. (YS)
  516. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  517. begin
  518. Result := '';
  519. end;
  520. Function GetEnvironmentVariableCount : Integer;
  521. begin
  522. Result := 0;
  523. end;
  524. Function GetEnvironmentString(Index : Integer) : String;
  525. begin
  526. Result := '';
  527. end;
  528. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  529. var
  530. PI: TProcessInformation;
  531. Proc : THandle;
  532. l : DWord;
  533. e : EOSError;
  534. begin
  535. DosError := 0;
  536. if not CreateProcess(PWideChar(widestring(Path)), PWideChar(widestring(ComLine)),
  537. nil, nil, FALSE, 0, nil, nil, nil, PI) then
  538. begin
  539. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,GetLastError]);
  540. e.ErrorCode:=GetLastError;
  541. raise e;
  542. end;
  543. Proc:=PI.hProcess;
  544. CloseHandle(PI.hThread);
  545. if WaitForSingleObject(Proc, dword($ffffffff)) <> $ffffffff then
  546. begin
  547. GetExitCodeProcess(Proc,l);
  548. CloseHandle(Proc);
  549. result:=l;
  550. end
  551. else
  552. begin
  553. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,GetLastError]);
  554. e.ErrorCode:=GetLastError;
  555. CloseHandle(Proc);
  556. raise e;
  557. end;
  558. end;
  559. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;Flags:TExecuteFlags=[]):integer;
  560. var
  561. CommandLine: AnsiString;
  562. I: integer;
  563. begin
  564. Commandline := '';
  565. for I := 0 to High (ComLine) do
  566. if Pos (' ', ComLine [I]) <> 0 then
  567. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  568. else
  569. CommandLine := CommandLine + ' ' + Comline [I];
  570. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  571. end;
  572. Procedure Sleep(Milliseconds : Cardinal);
  573. begin
  574. Windows.Sleep(MilliSeconds)
  575. end;
  576. Function GetLastOSError : Integer;
  577. begin
  578. Result:=GetLastError;
  579. end;
  580. {****************************************************************************
  581. Initialization code
  582. ****************************************************************************}
  583. Procedure LoadVersionInfo;
  584. Var
  585. versioninfo : TOSVERSIONINFO;
  586. i : Integer;
  587. begin
  588. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  589. GetVersionEx(versioninfo);
  590. WinCEPlatform:=versionInfo.dwPlatformId;
  591. WinCEMajorVersion:=versionInfo.dwMajorVersion;
  592. WinCEMinorVersion:=versionInfo.dwMinorVersion;
  593. WinCEBuildNumber:=versionInfo.dwBuildNumber;
  594. i:=WideToAnsiBuf(@versioninfo.szCSDVersion[0], -1, @WinCECSDVersion[1], SizeOf(WinCECSDVersion) - 1);
  595. if i <> 0 then
  596. WinCECSDVersion[0]:=chr(i - 1);
  597. end;
  598. Function GetSpecialDir(ID: Integer) : String;
  599. Var
  600. APath : array[0..MAX_PATH] of WideChar;
  601. begin
  602. if SHGetSpecialFolderPath(0, APath, ID, True) then
  603. begin
  604. PWideCharToString(APath, Result);
  605. Result:=IncludeTrailingPathDelimiter(Result);
  606. end
  607. else
  608. Result:='';
  609. end;
  610. Function GetAppConfigDir(Global : Boolean) : String;
  611. begin
  612. If Global then
  613. Result:=GetSpecialDir(CSIDL_WINDOWS)
  614. else
  615. Result:=GetSpecialDir(CSIDL_APPDATA);
  616. If (Result<>'') then
  617. begin
  618. if VendorName<>'' then
  619. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  620. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  621. end
  622. else
  623. Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
  624. end;
  625. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  626. begin
  627. result:=DGetAppConfigFile(Global,SubDir);
  628. end;
  629. Function GetTempDir(Global : Boolean) : String;
  630. var
  631. buf: widestring;
  632. begin
  633. SetLength(buf, MAX_PATH);
  634. SetLength(buf, GetTempPath(Length(buf) + 1, PWideChar(buf)));
  635. Result:=buf;
  636. Result := IncludeTrailingPathDelimiter(Result);
  637. end;
  638. {****************************************************************************
  639. Target Dependent WideString stuff
  640. ****************************************************************************}
  641. function WinCECompareWideString(const s1, s2 : WideString) : PtrInt;
  642. begin
  643. SetLastError(0);
  644. Result:=CompareString(LOCALE_USER_DEFAULT,0,pwidechar(s1),
  645. length(s1),pwidechar(s2),length(s2))-2;
  646. if GetLastError<>0 then
  647. RaiseLastOSError;
  648. end;
  649. function WinCECompareTextWideString(const s1, s2 : WideString) : PtrInt;
  650. begin
  651. SetLastError(0);
  652. Result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pwidechar(s1),
  653. length(s1),pwidechar(s2),length(s2))-2;
  654. if GetLastError<>0 then
  655. RaiseLastOSError;
  656. end;
  657. function WinCEAnsiUpperCase(const s: string): string;
  658. var
  659. buf: PWideChar;
  660. len: longint;
  661. begin
  662. if s <> '' then
  663. begin
  664. buf:=StringToPWideChar(s, @len);
  665. CharUpperBuff(buf, len-1);
  666. PWideCharToString(buf, Result, len-1);
  667. FreeMem(buf);
  668. end
  669. else
  670. Result:='';
  671. end;
  672. function WinCEAnsiLowerCase(const s: string): string;
  673. var
  674. buf: PWideChar;
  675. len: longint;
  676. begin
  677. if s <> '' then
  678. begin
  679. buf:=StringToPWideChar(s, @len);
  680. CharLowerBuff(buf, len-1);
  681. PWideCharToString(buf, Result, len-1);
  682. FreeMem(buf);
  683. end
  684. else
  685. Result:='';
  686. end;
  687. function WinCEAnsiCompareStr(const S1, S2: string): PtrInt;
  688. var
  689. ws1, ws2: PWideChar;
  690. begin
  691. ws1:=StringToPWideChar(S1);
  692. ws2:=StringToPWideChar(S2);
  693. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, Length(S1), ws2, Length(S2)) - 2;
  694. FreeMem(ws2);
  695. FreeMem(ws1);
  696. end;
  697. function WinCEAnsiCompareText(const S1, S2: string): PtrInt;
  698. var
  699. ws1, ws2: PWideChar;
  700. begin
  701. ws1:=StringToPWideChar(S1);
  702. ws2:=StringToPWideChar(S2);
  703. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, Length(S1), ws2, Length(S2)) - 2;
  704. FreeMem(ws2);
  705. FreeMem(ws1);
  706. end;
  707. function WinCEAnsiStrComp(S1, S2: PChar): PtrInt;
  708. var
  709. ws1, ws2: PWideChar;
  710. begin
  711. ws1:=PCharToPWideChar(S1);
  712. ws2:=PCharToPWideChar(S2);
  713. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, -1, ws2, -1) - 2;
  714. FreeMem(ws2);
  715. FreeMem(ws1);
  716. end;
  717. function WinCEAnsiStrIComp(S1, S2: PChar): PtrInt;
  718. var
  719. ws1, ws2: PWideChar;
  720. begin
  721. ws1:=PCharToPWideChar(S1);
  722. ws2:=PCharToPWideChar(S2);
  723. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, -1, ws2, -1) - 2;
  724. FreeMem(ws2);
  725. FreeMem(ws1);
  726. end;
  727. function WinCEAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  728. var
  729. ws1, ws2: PWideChar;
  730. len1, len2: longint;
  731. begin
  732. ws1:=PCharToPWideChar(S1, MaxLen, @len1);
  733. ws2:=PCharToPWideChar(S2, MaxLen, @len2);
  734. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, len1, ws2, len2) - 2;
  735. FreeMem(ws2);
  736. FreeMem(ws1);
  737. end;
  738. function WinCEAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  739. var
  740. ws1, ws2: PWideChar;
  741. len1, len2: longint;
  742. begin
  743. ws1:=PCharToPWideChar(S1, MaxLen, @len1);
  744. ws2:=PCharToPWideChar(S2, MaxLen, @len2);
  745. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, len1, ws2, len2) - 2;
  746. FreeMem(ws2);
  747. FreeMem(ws1);
  748. end;
  749. function WinCEAnsiStrLower(Str: PChar): PChar;
  750. var
  751. buf: PWideChar;
  752. len: longint;
  753. begin
  754. buf:=PCharToPWideChar(Str, -1, @len);
  755. CharLowerBuff(buf, len - 1);
  756. Result:=Str;
  757. WideToAnsiBuf(buf, -1, Result, StrLen(Str));
  758. FreeMem(buf);
  759. end;
  760. function WinCEAnsiStrUpper(Str: PChar): PChar;
  761. var
  762. buf: PWideChar;
  763. len: longint;
  764. begin
  765. buf:=PCharToPWideChar(Str, -1, @len);
  766. CharUpperBuff(buf, len - 1);
  767. Result:=Str;
  768. WideToAnsiBuf(buf, -1, Result, StrLen(Str));
  769. FreeMem(buf);
  770. end;
  771. { there is a similiar procedure in the system unit which inits the fields which
  772. are relevant already for the system unit }
  773. procedure InitWinCEWidestrings;
  774. begin
  775. widestringmanager.CompareWideStringProc:=@WinCECompareWideString;
  776. widestringmanager.CompareTextWideStringProc:=@WinCECompareTextWideString;
  777. widestringmanager.UpperAnsiStringProc:=@WinCEAnsiUpperCase;
  778. widestringmanager.LowerAnsiStringProc:=@WinCEAnsiLowerCase;
  779. widestringmanager.CompareStrAnsiStringProc:=@WinCEAnsiCompareStr;
  780. widestringmanager.CompareTextAnsiStringProc:=@WinCEAnsiCompareText;
  781. widestringmanager.StrCompAnsiStringProc:=@WinCEAnsiStrComp;
  782. widestringmanager.StrICompAnsiStringProc:=@WinCEAnsiStrIComp;
  783. widestringmanager.StrLCompAnsiStringProc:=@WinCEAnsiStrLComp;
  784. widestringmanager.StrLICompAnsiStringProc:=@WinCEAnsiStrLIComp;
  785. widestringmanager.StrLowerAnsiStringProc:=@WinCEAnsiStrLower;
  786. widestringmanager.StrUpperAnsiStringProc:=@WinCEAnsiStrUpper;
  787. end;
  788. Initialization
  789. InitWinCEWidestrings;
  790. InitExceptions; { Initialize exceptions. OS independent }
  791. InitInternational; { Initialize internationalization settings }
  792. LoadVersionInfo;
  793. OnBeep:=@SysBeep;
  794. SysConfigDir:='\Windows';
  795. Finalization
  796. DoneExceptions;
  797. end.