sysutils.pp 24 KB

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