sysutils.pp 23 KB

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