sysutils.pp 23 KB

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