sysutils.pp 23 KB

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