sysutils.pp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. MsgBuffer:=nil;
  502. len:=FormatMessage(
  503. FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS,
  504. nil,
  505. ErrorCode,
  506. 0,
  507. @MsgBuffer, { This function allocs the memory (in this case you pass a PPwidechar)}
  508. 0,
  509. nil);
  510. if MsgBuffer <> nil then begin
  511. while (len > 0) and (MsgBuffer[len - 1] <= #32) do
  512. Dec(len);
  513. MsgBuffer[len]:=#0;
  514. PWideCharToString(MsgBuffer, Result);
  515. LocalFree(HLOCAL(MsgBuffer));
  516. end
  517. else
  518. Result:='';
  519. end;
  520. {****************************************************************************
  521. Initialization code
  522. ****************************************************************************}
  523. // WinCE does not have environment. It can be emulated via registry or file. (YS)
  524. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  525. begin
  526. Result := '';
  527. end;
  528. Function GetEnvironmentVariableCount : Integer;
  529. begin
  530. Result := 0;
  531. end;
  532. Function GetEnvironmentString(Index : Integer) : String;
  533. begin
  534. Result := '';
  535. end;
  536. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  537. var
  538. PI: TProcessInformation;
  539. Proc : THandle;
  540. l : DWord;
  541. e : EOSError;
  542. begin
  543. DosError := 0;
  544. if not CreateProcess(PWideChar(widestring(Path)), PWideChar(widestring(ComLine)),
  545. nil, nil, FALSE, 0, nil, nil, nil, PI) then
  546. begin
  547. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,GetLastError]);
  548. e.ErrorCode:=GetLastError;
  549. raise e;
  550. end;
  551. Proc:=PI.hProcess;
  552. CloseHandle(PI.hThread);
  553. if WaitForSingleObject(Proc, dword($ffffffff)) <> $ffffffff then
  554. begin
  555. GetExitCodeProcess(Proc,l);
  556. CloseHandle(Proc);
  557. result:=l;
  558. end
  559. else
  560. begin
  561. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,GetLastError]);
  562. e.ErrorCode:=GetLastError;
  563. CloseHandle(Proc);
  564. raise e;
  565. end;
  566. end;
  567. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString):integer;
  568. var
  569. CommandLine: AnsiString;
  570. I: integer;
  571. begin
  572. Commandline := '';
  573. for I := 0 to High (ComLine) do
  574. if Pos (' ', ComLine [I]) <> 0 then
  575. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  576. else
  577. CommandLine := CommandLine + ' ' + Comline [I];
  578. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  579. end;
  580. Procedure Sleep(Milliseconds : Cardinal);
  581. begin
  582. Windows.Sleep(MilliSeconds)
  583. end;
  584. Function GetLastOSError : Integer;
  585. begin
  586. Result:=GetLastError;
  587. end;
  588. {****************************************************************************
  589. Initialization code
  590. ****************************************************************************}
  591. Procedure LoadVersionInfo;
  592. Var
  593. versioninfo : TOSVERSIONINFO;
  594. i : Integer;
  595. begin
  596. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  597. GetVersionEx(versioninfo);
  598. WinCEPlatform:=versionInfo.dwPlatformId;
  599. WinCEMajorVersion:=versionInfo.dwMajorVersion;
  600. WinCEMinorVersion:=versionInfo.dwMinorVersion;
  601. WinCEBuildNumber:=versionInfo.dwBuildNumber;
  602. i:=WideToAnsiBuf(@versioninfo.szCSDVersion[0], -1, @WinCECSDVersion[1], SizeOf(WinCECSDVersion) - 1);
  603. if i <> 0 then
  604. WinCECSDVersion[0]:=chr(i - 1);
  605. end;
  606. Function GetSpecialDir(ID: Integer) : String;
  607. Var
  608. APath : array[0..MAX_PATH] of WideChar;
  609. begin
  610. if SHGetSpecialFolderPath(0, APath, ID, True) then
  611. begin
  612. PWideCharToString(APath, Result);
  613. Result:=IncludeTrailingPathDelimiter(Result);
  614. end
  615. else
  616. Result:='';
  617. end;
  618. Function GetAppConfigDir(Global : Boolean) : String;
  619. begin
  620. If Global then
  621. Result:=GetSpecialDir(CSIDL_WINDOWS)
  622. else
  623. Result:=GetSpecialDir(CSIDL_APPDATA);
  624. If (Result<>'') then
  625. begin
  626. if VendorName<>'' then
  627. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  628. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  629. end
  630. else
  631. Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
  632. end;
  633. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  634. begin
  635. result:=DGetAppConfigFile(Global,SubDir);
  636. end;
  637. Function GetTempDir(Global : Boolean) : String;
  638. var
  639. buf: widestring;
  640. begin
  641. SetLength(buf, MAX_PATH);
  642. SetLength(buf, GetTempPath(Length(buf) + 1, PWideChar(buf)));
  643. Result:=buf;
  644. Result := IncludeTrailingPathDelimiter(Result);
  645. end;
  646. {****************************************************************************
  647. Target Dependent WideString stuff
  648. ****************************************************************************}
  649. function WinCECompareWideString(const s1, s2 : WideString) : PtrInt;
  650. begin
  651. SetLastError(0);
  652. Result:=CompareString(LOCALE_USER_DEFAULT,0,pwidechar(s1),
  653. length(s1),pwidechar(s2),length(s2))-2;
  654. if GetLastError<>0 then
  655. RaiseLastOSError;
  656. end;
  657. function WinCECompareTextWideString(const s1, s2 : WideString) : PtrInt;
  658. begin
  659. SetLastError(0);
  660. Result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pwidechar(s1),
  661. length(s1),pwidechar(s2),length(s2))-2;
  662. if GetLastError<>0 then
  663. RaiseLastOSError;
  664. end;
  665. function WinCEAnsiUpperCase(const s: string): string;
  666. var
  667. buf: PWideChar;
  668. len: longint;
  669. begin
  670. if s <> '' then
  671. begin
  672. buf:=StringToPWideChar(s, @len);
  673. CharUpperBuff(buf, len-1);
  674. PWideCharToString(buf, Result, len-1);
  675. FreeMem(buf);
  676. end
  677. else
  678. Result:='';
  679. end;
  680. function WinCEAnsiLowerCase(const s: string): string;
  681. var
  682. buf: PWideChar;
  683. len: longint;
  684. begin
  685. if s <> '' then
  686. begin
  687. buf:=StringToPWideChar(s, @len);
  688. CharLowerBuff(buf, len-1);
  689. PWideCharToString(buf, Result, len-1);
  690. FreeMem(buf);
  691. end
  692. else
  693. Result:='';
  694. end;
  695. function WinCEAnsiCompareStr(const S1, S2: string): PtrInt;
  696. var
  697. ws1, ws2: PWideChar;
  698. begin
  699. ws1:=StringToPWideChar(S1);
  700. ws2:=StringToPWideChar(S2);
  701. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, Length(S1), ws2, Length(S2)) - 2;
  702. FreeMem(ws2);
  703. FreeMem(ws1);
  704. end;
  705. function WinCEAnsiCompareText(const S1, S2: string): PtrInt;
  706. var
  707. ws1, ws2: PWideChar;
  708. begin
  709. ws1:=StringToPWideChar(S1);
  710. ws2:=StringToPWideChar(S2);
  711. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, Length(S1), ws2, Length(S2)) - 2;
  712. FreeMem(ws2);
  713. FreeMem(ws1);
  714. end;
  715. function WinCEAnsiStrComp(S1, S2: PChar): PtrInt;
  716. var
  717. ws1, ws2: PWideChar;
  718. begin
  719. ws1:=PCharToPWideChar(S1);
  720. ws2:=PCharToPWideChar(S2);
  721. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, -1, ws2, -1) - 2;
  722. FreeMem(ws2);
  723. FreeMem(ws1);
  724. end;
  725. function WinCEAnsiStrIComp(S1, S2: PChar): PtrInt;
  726. var
  727. ws1, ws2: PWideChar;
  728. begin
  729. ws1:=PCharToPWideChar(S1);
  730. ws2:=PCharToPWideChar(S2);
  731. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, -1, ws2, -1) - 2;
  732. FreeMem(ws2);
  733. FreeMem(ws1);
  734. end;
  735. function WinCEAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  736. var
  737. ws1, ws2: PWideChar;
  738. len1, len2: longint;
  739. begin
  740. ws1:=PCharToPWideChar(S1, MaxLen, @len1);
  741. ws2:=PCharToPWideChar(S2, MaxLen, @len2);
  742. Result:=CompareString(LOCALE_USER_DEFAULT, 0, ws1, len1, ws2, len2) - 2;
  743. FreeMem(ws2);
  744. FreeMem(ws1);
  745. end;
  746. function WinCEAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  747. var
  748. ws1, ws2: PWideChar;
  749. len1, len2: longint;
  750. begin
  751. ws1:=PCharToPWideChar(S1, MaxLen, @len1);
  752. ws2:=PCharToPWideChar(S2, MaxLen, @len2);
  753. Result:=CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ws1, len1, ws2, len2) - 2;
  754. FreeMem(ws2);
  755. FreeMem(ws1);
  756. end;
  757. function WinCEAnsiStrLower(Str: PChar): PChar;
  758. var
  759. buf: PWideChar;
  760. len: longint;
  761. begin
  762. buf:=PCharToPWideChar(Str, -1, @len);
  763. CharLowerBuff(buf, len - 1);
  764. Result:=Str;
  765. WideToAnsiBuf(buf, -1, Result, StrLen(Str));
  766. FreeMem(buf);
  767. end;
  768. function WinCEAnsiStrUpper(Str: PChar): PChar;
  769. var
  770. buf: PWideChar;
  771. len: longint;
  772. begin
  773. buf:=PCharToPWideChar(Str, -1, @len);
  774. CharUpperBuff(buf, len - 1);
  775. Result:=Str;
  776. WideToAnsiBuf(buf, -1, Result, StrLen(Str));
  777. FreeMem(buf);
  778. end;
  779. { there is a similiar procedure in the system unit which inits the fields which
  780. are relevant already for the system unit }
  781. procedure InitWinCEWidestrings;
  782. begin
  783. widestringmanager.CompareWideStringProc:=@WinCECompareWideString;
  784. widestringmanager.CompareTextWideStringProc:=@WinCECompareTextWideString;
  785. widestringmanager.UpperAnsiStringProc:=@WinCEAnsiUpperCase;
  786. widestringmanager.LowerAnsiStringProc:=@WinCEAnsiLowerCase;
  787. widestringmanager.CompareStrAnsiStringProc:=@WinCEAnsiCompareStr;
  788. widestringmanager.CompareTextAnsiStringProc:=@WinCEAnsiCompareText;
  789. widestringmanager.StrCompAnsiStringProc:=@WinCEAnsiStrComp;
  790. widestringmanager.StrICompAnsiStringProc:=@WinCEAnsiStrIComp;
  791. widestringmanager.StrLCompAnsiStringProc:=@WinCEAnsiStrLComp;
  792. widestringmanager.StrLICompAnsiStringProc:=@WinCEAnsiStrLIComp;
  793. widestringmanager.StrLowerAnsiStringProc:=@WinCEAnsiStrLower;
  794. widestringmanager.StrUpperAnsiStringProc:=@WinCEAnsiStrUpper;
  795. end;
  796. Initialization
  797. InitWinCEWidestrings;
  798. InitExceptions; { Initialize exceptions. OS independent }
  799. InitInternational; { Initialize internationalization settings }
  800. LoadVersionInfo;
  801. SysConfigDir:='\Windows';
  802. Finalization
  803. DoneExceptions;
  804. end.