sysutils.pp 24 KB

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