sysutils.pp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for win32
  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. {$IFNDEF VIRTUALPASCAL}
  15. {$MODE objfpc}
  16. {$ENDIF}
  17. { force ansistrings }
  18. {$H+}
  19. uses
  20. {$IFDEF VIRTUALPASCAL}
  21. vpglue,
  22. strings,
  23. crt,
  24. {$ENDIF}
  25. dos,
  26. windows;
  27. {$DEFINE HAS_SLEEP}
  28. {$DEFINE HAS_OSERROR}
  29. {$DEFINE HAS_OSCONFIG}
  30. { Include platform independent interface part }
  31. {$i sysutilh.inc}
  32. type
  33. TSystemTime = Windows.TSystemTime;
  34. EWin32Error = class(Exception)
  35. public
  36. ErrorCode : DWORD;
  37. end;
  38. Var
  39. Win32Platform : Longint;
  40. Win32MajorVersion,
  41. Win32MinorVersion,
  42. Win32BuildNumber : dword;
  43. Win32CSDVersion : ShortString; // CSD record is 128 bytes only?
  44. implementation
  45. uses
  46. sysconst;
  47. { Include platform independent implementation part }
  48. {$i sysutils.inc}
  49. {****************************************************************************
  50. File Functions
  51. ****************************************************************************}
  52. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  53. const
  54. AccessMode: array[0..2] of Cardinal = (
  55. GENERIC_READ,
  56. GENERIC_WRITE,
  57. GENERIC_READ or GENERIC_WRITE);
  58. ShareMode: array[0..4] of Integer = (
  59. 0,
  60. 0,
  61. FILE_SHARE_READ,
  62. FILE_SHARE_WRITE,
  63. FILE_SHARE_READ or FILE_SHARE_WRITE);
  64. Var
  65. FN : string;
  66. begin
  67. FN:=FileName+#0;
  68. result := CreateFile(@FN[1], dword(AccessMode[Mode and 3]),
  69. dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
  70. FILE_ATTRIBUTE_NORMAL, 0);
  71. end;
  72. Function FileCreate (Const FileName : String) : Longint;
  73. Var
  74. FN : string;
  75. begin
  76. FN:=FileName+#0;
  77. Result := CreateFile(@FN[1], GENERIC_READ or GENERIC_WRITE,
  78. 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  79. end;
  80. Function FileCreate (Const FileName : String; Mode:longint) : SizeInt;
  81. begin
  82. FileCreate:=FileCreate(FileName);
  83. end;
  84. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  85. Var
  86. res : dword;
  87. begin
  88. if ReadFile(Handle, Buffer, Count, res, nil) then
  89. FileRead:=Res
  90. else
  91. FileRead:=-1;
  92. end;
  93. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  94. Var
  95. Res : dword;
  96. begin
  97. if WriteFile(Handle, Buffer, Count, Res, nil) then
  98. FileWrite:=Res
  99. else
  100. FileWrite:=-1;
  101. end;
  102. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  103. begin
  104. Result := longint(SetFilePointer(Handle, FOffset, nil, Origin));
  105. end;
  106. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  107. begin
  108. {$warning need to add 64bit call }
  109. Result := longint(SetFilePointer(Handle, FOffset, nil, Origin));
  110. end;
  111. Procedure FileClose (Handle : Longint);
  112. begin
  113. if Handle<=4 then
  114. exit;
  115. CloseHandle(Handle);
  116. end;
  117. Function FileTruncate (Handle,Size: Longint) : boolean;
  118. begin
  119. Result:=longint(SetFilePointer(handle,Size,nil,FILE_BEGIN))<>-1;
  120. If Result then
  121. Result:=SetEndOfFile(handle);
  122. end;
  123. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  124. var
  125. lft : TFileTime;
  126. begin
  127. {$IFDEF VIRTUALPASCAL}
  128. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,lft) and
  129. LocalFileTimeToFileTime(lft,Wtime);
  130. {$ELSE}
  131. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,@lft) and
  132. LocalFileTimeToFileTime(lft,Wtime);
  133. {$ENDIF}
  134. end;
  135. Function WinToDosTime (Var Wtime : TFileTime;var DTime:longint):longbool;
  136. var
  137. lft : TFileTime;
  138. begin
  139. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  140. FileTimeToDosDateTime(lft,Longrec(Dtime).Hi,LongRec(DTIME).lo);
  141. end;
  142. Function FileAge (Const FileName : String): Longint;
  143. var
  144. Handle: THandle;
  145. FindData: TWin32FindData;
  146. begin
  147. Handle := FindFirstFile(Pchar(FileName), FindData);
  148. if Handle <> INVALID_HANDLE_VALUE then
  149. begin
  150. Windows.FindClose(Handle);
  151. if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  152. If WinToDosTime(FindData.ftLastWriteTime,Result) then
  153. exit;
  154. end;
  155. Result := -1;
  156. end;
  157. Function FileExists (Const FileName : String) : Boolean;
  158. var
  159. Handle: THandle;
  160. FindData: TWin32FindData;
  161. begin
  162. Handle := FindFirstFile(Pchar(FileName), FindData);
  163. Result:=Handle <> INVALID_HANDLE_VALUE;
  164. If Result then
  165. Windows.FindClose(Handle);
  166. end;
  167. Function DirectoryExists (Const Directory : String) : Boolean;
  168. var
  169. Handle: THandle;
  170. FindData: TWin32FindData;
  171. begin
  172. Result:=False;
  173. Handle := FindFirstFile(Pchar(Directory), FindData);
  174. If (Handle <> INVALID_HANDLE_VALUE) then
  175. begin
  176. Result:=((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY);
  177. Windows.FindClose(Handle);
  178. end;
  179. end;
  180. Function FindMatch(var f: TSearchRec) : Longint;
  181. begin
  182. { Find file with correct attribute }
  183. While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
  184. begin
  185. if not FindNextFile (F.FindHandle,F.FindData) then
  186. begin
  187. Result:=GetLastError;
  188. exit;
  189. end;
  190. end;
  191. { Convert some attributes back }
  192. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  193. f.size:=F.FindData.NFileSizeLow;
  194. f.attr:=F.FindData.dwFileAttributes;
  195. f.Name:=StrPas(@F.FindData.cFileName);
  196. Result:=0;
  197. end;
  198. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  199. begin
  200. Rslt.Name:=Path;
  201. Rslt.Attr:=attr;
  202. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  203. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  204. { FindFirstFile is a Win32 Call }
  205. Rslt.FindHandle:=FindFirstFile (PChar(Path),Rslt.FindData);
  206. If Rslt.FindHandle=Invalid_Handle_value then
  207. begin
  208. Result:=GetLastError;
  209. exit;
  210. end;
  211. { Find file with correct attribute }
  212. Result:=FindMatch(Rslt);
  213. end;
  214. Function FindNext (Var Rslt : TSearchRec) : Longint;
  215. begin
  216. if FindNextFile(Rslt.FindHandle, Rslt.FindData) then
  217. Result := FindMatch(Rslt)
  218. else
  219. Result := GetLastError;
  220. end;
  221. Procedure FindClose (Var F : TSearchrec);
  222. begin
  223. if F.FindHandle <> INVALID_HANDLE_VALUE then
  224. Windows.FindClose(F.FindHandle);
  225. end;
  226. Function FileGetDate (Handle : Longint) : Longint;
  227. Var
  228. FT : TFileTime;
  229. begin
  230. If GetFileTime(Handle,nil,nil,@ft) and
  231. WinToDosTime(FT,Result) then
  232. exit;
  233. Result:=-1;
  234. end;
  235. Function FileSetDate (Handle,Age : Longint) : Longint;
  236. Var
  237. FT: TFileTime;
  238. begin
  239. {$IFDEF VIRTUALPASCAL}
  240. Result := 0;
  241. {$ELSE}
  242. Result := 0;
  243. if DosToWinTime(Age,FT) and
  244. SetFileTime(Handle, ft, ft, FT) then
  245. Exit;
  246. Result := GetLastError;
  247. {$ENDIF}
  248. end;
  249. Function FileGetAttr (Const FileName : String) : Longint;
  250. begin
  251. Result:=GetFileAttributes(PChar(FileName));
  252. end;
  253. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  254. begin
  255. if not SetFileAttributes(PChar(FileName), Attr) then
  256. Result := GetLastError
  257. else
  258. Result:=0;
  259. end;
  260. Function DeleteFile (Const FileName : String) : Boolean;
  261. begin
  262. DeleteFile:=Windows.DeleteFile(Pchar(FileName));
  263. end;
  264. Function RenameFile (Const OldName, NewName : String) : Boolean;
  265. begin
  266. Result := MoveFile(PChar(OldName), PChar(NewName));
  267. end;
  268. {****************************************************************************
  269. Disk Functions
  270. ****************************************************************************}
  271. function GetDiskFreeSpace(drive:pchar;var sector_cluster,bytes_sector,
  272. freeclusters,totalclusters:longint):longbool;
  273. stdcall;external 'kernel32' name 'GetDiskFreeSpaceA';
  274. type
  275. {$IFDEF VIRTUALPASCAL}
  276. {&StdCall+}
  277. TGetDiskFreeSpaceEx = function(drive:pchar;var availableforcaller,total,free):longbool;
  278. {&StdCall-}
  279. {$ELSE}
  280. TGetDiskFreeSpaceEx = function(drive:pchar;var availableforcaller,total,free):longbool;stdcall;
  281. {$ENDIF}
  282. var
  283. GetDiskFreeSpaceEx : TGetDiskFreeSpaceEx;
  284. function diskfree(drive : byte) : int64;
  285. var
  286. disk : array[1..4] of char;
  287. secs,bytes,
  288. free,total : longint;
  289. qwtotal,qwfree,qwcaller : int64;
  290. begin
  291. if drive=0 then
  292. begin
  293. disk[1]:='\';
  294. disk[2]:=#0;
  295. end
  296. else
  297. begin
  298. disk[1]:=chr(drive+64);
  299. disk[2]:=':';
  300. disk[3]:='\';
  301. disk[4]:=#0;
  302. end;
  303. if assigned(GetDiskFreeSpaceEx) then
  304. begin
  305. if GetDiskFreeSpaceEx(@disk,qwcaller,qwtotal,qwfree) then
  306. diskfree:=qwfree
  307. else
  308. diskfree:=-1;
  309. end
  310. else
  311. begin
  312. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  313. diskfree:=int64(free)*secs*bytes
  314. else
  315. diskfree:=-1;
  316. end;
  317. end;
  318. function disksize(drive : byte) : int64;
  319. var
  320. disk : array[1..4] of char;
  321. secs,bytes,
  322. free,total : longint;
  323. qwtotal,qwfree,qwcaller : int64;
  324. begin
  325. if drive=0 then
  326. begin
  327. disk[1]:='\';
  328. disk[2]:=#0;
  329. end
  330. else
  331. begin
  332. disk[1]:=chr(drive+64);
  333. disk[2]:=':';
  334. disk[3]:='\';
  335. disk[4]:=#0;
  336. end;
  337. if assigned(GetDiskFreeSpaceEx) then
  338. begin
  339. if GetDiskFreeSpaceEx(@disk,qwcaller,qwtotal,qwfree) then
  340. disksize:=qwtotal
  341. else
  342. disksize:=-1;
  343. end
  344. else
  345. begin
  346. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  347. disksize:=int64(total)*secs*bytes
  348. else
  349. disksize:=-1;
  350. end;
  351. end;
  352. Function GetCurrentDir : String;
  353. begin
  354. GetDir(0, result);
  355. end;
  356. Function SetCurrentDir (Const NewDir : String) : Boolean;
  357. begin
  358. {$I-}
  359. ChDir(NewDir);
  360. {$I+}
  361. result := (IOResult = 0);
  362. end;
  363. Function CreateDir (Const NewDir : String) : Boolean;
  364. begin
  365. {$I-}
  366. MkDir(NewDir);
  367. {$I+}
  368. result := (IOResult = 0);
  369. end;
  370. Function RemoveDir (Const Dir : String) : Boolean;
  371. begin
  372. {$I-}
  373. RmDir(Dir);
  374. {$I+}
  375. result := (IOResult = 0);
  376. end;
  377. {****************************************************************************
  378. Time Functions
  379. ****************************************************************************}
  380. Procedure GetLocalTime(var SystemTime: TSystemTime);
  381. Var
  382. Syst : Windows.TSystemtime;
  383. begin
  384. windows.Getlocaltime(@syst);
  385. SystemTime.year:=syst.wYear;
  386. SystemTime.month:=syst.wMonth;
  387. SystemTime.day:=syst.wDay;
  388. SystemTime.hour:=syst.wHour;
  389. SystemTime.minute:=syst.wMinute;
  390. SystemTime.second:=syst.wSecond;
  391. SystemTime.millisecond:=syst.wMilliSeconds;
  392. end;
  393. {****************************************************************************
  394. Misc Functions
  395. ****************************************************************************}
  396. procedure Beep;
  397. begin
  398. MessageBeep(0);
  399. end;
  400. {****************************************************************************
  401. Locale Functions
  402. ****************************************************************************}
  403. Procedure InitAnsi;
  404. Var
  405. i : longint;
  406. begin
  407. { Fill table entries 0 to 127 }
  408. for i := 0 to 96 do
  409. UpperCaseTable[i] := chr(i);
  410. for i := 97 to 122 do
  411. UpperCaseTable[i] := chr(i - 32);
  412. for i := 123 to 191 do
  413. UpperCaseTable[i] := chr(i);
  414. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  415. for i := 0 to 64 do
  416. LowerCaseTable[i] := chr(i);
  417. for i := 65 to 90 do
  418. LowerCaseTable[i] := chr(i + 32);
  419. for i := 91 to 191 do
  420. LowerCaseTable[i] := chr(i);
  421. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  422. end;
  423. function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
  424. var
  425. L: Integer;
  426. Buf: array[0..255] of Char;
  427. begin
  428. L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
  429. if L > 0 then
  430. SetString(Result, @Buf[0], L - 1)
  431. else
  432. Result := Def;
  433. end;
  434. function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
  435. var
  436. Buf: array[0..1] of Char;
  437. begin
  438. if GetLocaleInfo(LID, LT, Buf, 2) > 0 then
  439. Result := Buf[0]
  440. else
  441. Result := Def;
  442. end;
  443. Function GetLocaleInt(LID,TP,Def: LongInt): LongInt;
  444. Var
  445. S: String;
  446. C: Integer;
  447. Begin
  448. S:=GetLocaleStr(LID,TP,'0');
  449. Val(S,Result,C);
  450. If C<>0 Then
  451. Result:=Def;
  452. End;
  453. procedure GetFormatSettings;
  454. var
  455. HF : Shortstring;
  456. LID : LCID;
  457. I,Day,DateOrder : longint;
  458. begin
  459. LID := GetThreadLocale;
  460. { Date stuff }
  461. for I := 1 to 12 do
  462. begin
  463. ShortMonthNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVMONTHNAME1+I-1,ShortMonthNames[i]);
  464. LongMonthNames[I]:=GetLocaleStr(LID,LOCALE_SMONTHNAME1+I-1,LongMonthNames[i]);
  465. end;
  466. for I := 1 to 7 do
  467. begin
  468. Day := (I + 5) mod 7;
  469. ShortDayNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVDAYNAME1+Day,ShortDayNames[i]);
  470. LongDayNames[I]:=GetLocaleStr(LID,LOCALE_SDAYNAME1+Day,LongDayNames[i]);
  471. end;
  472. DateSeparator := GetLocaleChar(LID, LOCALE_SDATE, '/');
  473. DateOrder := GetLocaleInt(LID, LOCALE_IDate, 0);
  474. Case DateOrder Of
  475. 1: Begin
  476. ShortDateFormat := 'dd/mm/yyyy';
  477. LongDateFormat := 'dddd, d. mmmm yyyy';
  478. End;
  479. 2: Begin
  480. ShortDateFormat := 'yyyy/mm/dd';
  481. LongDateFormat := 'dddd, yyyy mmmm d.';
  482. End;
  483. else
  484. // Default american settings...
  485. ShortDateFormat := 'mm/dd/yyyy';
  486. LongDateFormat := 'dddd, mmmm d. yyyy';
  487. End;
  488. { Time stuff }
  489. TimeSeparator := GetLocaleChar(LID, LOCALE_STIME, ':');
  490. TimeAMString := GetLocaleStr(LID, LOCALE_S1159, 'AM');
  491. TimePMString := GetLocaleStr(LID, LOCALE_S2359, 'PM');
  492. if StrToIntDef(GetLocaleStr(LID, LOCALE_ITLZERO, '0'), 0) = 0 then
  493. HF:='h'
  494. else
  495. HF:='hh';
  496. // No support for 12 hour stuff at the moment...
  497. ShortTimeFormat := HF+':nn';
  498. LongTimeFormat := HF + ':nn:ss';
  499. { Currency stuff }
  500. CurrencyString:=GetLocaleStr(LID, LOCALE_SCURRENCY, '');
  501. CurrencyFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRENCY, '0'), 0);
  502. NegCurrFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_INEGCURR, '0'), 0);
  503. { Number stuff }
  504. ThousandSeparator:=GetLocaleChar(LID, LOCALE_STHOUSAND, ',');
  505. DecimalSeparator:=GetLocaleChar(LID, LOCALE_SDECIMAL, '.');
  506. CurrencyDecimals:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRDIGITS, '0'), 0);
  507. end;
  508. Procedure InitInternational;
  509. var
  510. { A call to GetSystemMetrics changes the value of the 8087 Control Word on
  511. Pentium4 with WinXP SP2 }
  512. old8087CW: word;
  513. begin
  514. InitInternationalGeneric;
  515. old8087CW:=Get8087CW;
  516. SysLocale.MBCS:=GetSystemMetrics(SM_DBCSENABLED)<>0;
  517. SysLocale.RightToLeft:=GetSystemMetrics(SM_MIDEASTENABLED)<>0;
  518. Set8087CW(old8087CW);
  519. InitAnsi;
  520. GetFormatSettings;
  521. end;
  522. {****************************************************************************
  523. Target Dependent
  524. ****************************************************************************}
  525. function FormatMessageA(dwFlags : DWORD;
  526. lpSource : Pointer;
  527. dwMessageId : DWORD;
  528. dwLanguageId: DWORD;
  529. lpBuffer : PCHAR;
  530. nSize : DWORD;
  531. Arguments : Pointer): DWORD; stdcall;external 'kernel32' name 'FormatMessageA';
  532. function SysErrorMessage(ErrorCode: Integer): String;
  533. const
  534. MaxMsgSize = Format_Message_Max_Width_Mask;
  535. var
  536. MsgBuffer: pChar;
  537. begin
  538. GetMem(MsgBuffer, MaxMsgSize);
  539. FillChar(MsgBuffer^, MaxMsgSize, #0);
  540. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
  541. nil,
  542. ErrorCode,
  543. MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
  544. MsgBuffer, { This function allocs the memory }
  545. MaxMsgSize, { Maximum message size }
  546. nil);
  547. SysErrorMessage := StrPas(MsgBuffer);
  548. FreeMem(MsgBuffer, MaxMsgSize);
  549. end;
  550. {****************************************************************************
  551. Initialization code
  552. ****************************************************************************}
  553. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  554. var
  555. s : string;
  556. i : longint;
  557. hp,p : pchar;
  558. begin
  559. Result:='';
  560. p:=GetEnvironmentStrings;
  561. hp:=p;
  562. while hp^<>#0 do
  563. begin
  564. s:=strpas(hp);
  565. i:=pos('=',s);
  566. if uppercase(copy(s,1,i-1))=upcase(envvar) then
  567. begin
  568. Result:=copy(s,i+1,length(s)-i);
  569. break;
  570. end;
  571. { next string entry}
  572. hp:=hp+strlen(hp)+1;
  573. end;
  574. FreeEnvironmentStrings(p);
  575. end;
  576. Function GetEnvironmentVariableCount : Integer;
  577. var
  578. hp,p : pchar;
  579. begin
  580. Result:=0;
  581. p:=GetEnvironmentStrings;
  582. hp:=p;
  583. If (Hp<>Nil) then
  584. while hp^<>#0 do
  585. begin
  586. Inc(Result);
  587. hp:=hp+strlen(hp)+1;
  588. end;
  589. FreeEnvironmentStrings(p);
  590. end;
  591. Function GetEnvironmentString(Index : Integer) : String;
  592. var
  593. hp,p : pchar;
  594. begin
  595. Result:='';
  596. p:=GetEnvironmentStrings;
  597. hp:=p;
  598. If (Hp<>Nil) then
  599. begin
  600. while (hp^<>#0) and (Index>1) do
  601. begin
  602. Dec(Index);
  603. hp:=hp+strlen(hp)+1;
  604. end;
  605. If (hp^<>#0) then
  606. Result:=StrPas(HP);
  607. end;
  608. FreeEnvironmentStrings(p);
  609. end;
  610. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  611. var
  612. SI: TStartupInfo;
  613. PI: TProcessInformation;
  614. Proc : TWin32Handle;
  615. l : DWord;
  616. CommandLine : ansistring;
  617. e : EOSError;
  618. begin
  619. DosError := 0;
  620. FillChar(SI, SizeOf(SI), 0);
  621. SI.cb:=SizeOf(SI);
  622. SI.wShowWindow:=1;
  623. { always surround the name of the application by quotes
  624. so that long filenames will always be accepted. But don't
  625. do it if there are already double quotes, since Win32 does not
  626. like double quotes which are duplicated!
  627. }
  628. if pos('"',path)=0 then
  629. CommandLine:='"'+path+'"'
  630. else
  631. CommandLine:=path;
  632. if ComLine <> '' then
  633. CommandLine:=Commandline+' '+ComLine+#0
  634. else
  635. CommandLine := CommandLine + #0;
  636. if not CreateProcess(nil, pchar(CommandLine),
  637. Nil, Nil, ExecInheritsHandles,$20, Nil, Nil, SI, PI) then
  638. begin
  639. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  640. e.ErrorCode:=GetLastError;
  641. raise e;
  642. end;
  643. Proc:=PI.hProcess;
  644. CloseHandle(PI.hThread);
  645. if WaitForSingleObject(Proc, dword($ffffffff)) <> $ffffffff then
  646. begin
  647. GetExitCodeProcess(Proc,l);
  648. CloseHandle(Proc);
  649. result:=l;
  650. end
  651. else
  652. begin
  653. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
  654. e.ErrorCode:=GetLastError;
  655. CloseHandle(Proc);
  656. raise e;
  657. end;
  658. end;
  659. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString):integer;
  660. Var
  661. CommandLine : AnsiString;
  662. i : Integer;
  663. Begin
  664. Commandline:='';
  665. For i:=0 to high(ComLine) Do
  666. Commandline:=CommandLine+' '+Comline[i];
  667. ExecuteProcess:=ExecuteProcess(Path,CommandLine);
  668. End;
  669. Procedure Sleep(Milliseconds : Cardinal);
  670. begin
  671. Windows.Sleep(MilliSeconds)
  672. end;
  673. Function GetLastOSError : Integer;
  674. begin
  675. Result:=GetLastError;
  676. end;
  677. {****************************************************************************
  678. Initialization code
  679. ****************************************************************************}
  680. var
  681. kernel32dll : THandle;
  682. Procedure LoadVersionInfo;
  683. // and getfreespaceex
  684. Var
  685. versioninfo : TOSVERSIONINFO;
  686. i : Integer;
  687. begin
  688. kernel32dll:=0;
  689. GetDiskFreeSpaceEx:=nil;
  690. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  691. GetVersionEx(versioninfo);
  692. Win32Platform:=versionInfo.dwPlatformId;
  693. Win32MajorVersion:=versionInfo.dwMajorVersion;
  694. Win32MinorVersion:=versionInfo.dwMinorVersion;
  695. Win32BuildNumber:=versionInfo.dwBuildNumber;
  696. Move (versioninfo.szCSDVersion ,Win32CSDVersion[1],128);
  697. win32CSDVersion[0]:=chr(strlen(pchar(@versioninfo.szCSDVersion)));
  698. if ((versioninfo.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) and
  699. (versioninfo.dwBuildNUmber>=1000)) or
  700. (versioninfo.dwPlatformId=VER_PLATFORM_WIN32_NT) then
  701. begin
  702. kernel32dll:=LoadLibrary('kernel32');
  703. if kernel32dll<>0 then
  704. {$IFDEF VIRTUALPASCAL}
  705. @GetDiskFreeSpaceEx:=GetProcAddress(0,'GetDiskFreeSpaceExA');
  706. {$ELSE}
  707. GetDiskFreeSpaceEx:=TGetDiskFreeSpaceEx(GetProcAddress(kernel32dll,'GetDiskFreeSpaceExA'));
  708. {$ENDIF}
  709. end;
  710. end;
  711. function FreeLibrary(hLibModule : THANDLE) : longbool;
  712. stdcall;external 'kernel32' name 'FreeLibrary';
  713. function GetVersionEx(var VersionInformation:TOSVERSIONINFO) : longbool;
  714. stdcall;external 'kernel32' name 'GetVersionExA';
  715. function LoadLibrary(lpLibFileName : pchar):THandle;
  716. stdcall;external 'kernel32' name 'LoadLibraryA';
  717. function GetProcAddress(hModule : THandle;lpProcName : pchar) : pointer;
  718. stdcall;external 'kernel32' name 'GetProcAddress';
  719. Const
  720. CSIDL_PROGRAMS = $0002; { %SYSTEMDRIVE%\Program Files }
  721. CSIDL_PERSONAL = $0005; { %USERPROFILE%\My Documents }
  722. CSIDL_FAVORITES = $0006; { %USERPROFILE%\Favorites }
  723. CSIDL_STARTUP = $0007; { %USERPROFILE%\Start menu\Programs\Startup }
  724. CSIDL_RECENT = $0008; { %USERPROFILE%\Recent }
  725. CSIDL_SENDTO = $0009; { %USERPROFILE%\Sendto }
  726. CSIDL_STARTMENU = $000B; { %USERPROFILE%\Start menu }
  727. CSIDL_MYMUSIC = $000D; { %USERPROFILE%\Documents\My Music }
  728. CSIDL_MYVIDEO = $000E; { %USERPROFILE%\Documents\My Videos }
  729. CSIDL_DESKTOPDIRECTORY = $0010; { %USERPROFILE%\Desktop }
  730. CSIDL_NETHOOD = $0013; { %USERPROFILE%\NetHood }
  731. CSIDL_TEMPLATES = $0015; { %USERPROFILE%\Templates }
  732. CSIDL_COMMON_STARTMENU = $0016; { %PROFILEPATH%\All users\Start menu }
  733. CSIDL_COMMON_PROGRAMS = $0017; { %PROFILEPATH%\All users\Start menu\Programs }
  734. CSIDL_COMMON_STARTUP = $0018; { %PROFILEPATH%\All users\Start menu\Programs\Startup }
  735. CSIDL_COMMON_DESKTOPDIRECTORY = $0019; { %PROFILEPATH%\All users\Desktop }
  736. CSIDL_APPDATA = $001A; { %USERPROFILE%\Application Data (roaming) }
  737. CSIDL_PRINTHOOD = $001B; { %USERPROFILE%\Printhood }
  738. CSIDL_LOCAL_APPDATA = $001C; { %USERPROFILE%\Local Settings\Application Data (non roaming) }
  739. CSIDL_COMMON_FAVORITES = $001F; { %PROFILEPATH%\All users\Favorites }
  740. CSIDL_INTERNET_CACHE = $0020; { %USERPROFILE%\Local Settings\Temporary Internet Files }
  741. CSIDL_COOKIES = $0021; { %USERPROFILE%\Cookies }
  742. CSIDL_HISTORY = $0022; { %USERPROFILE%\Local settings\History }
  743. CSIDL_COMMON_APPDATA = $0023; { %PROFILESPATH%\All Users\Application Data }
  744. CSIDL_WINDOWS = $0024; { %SYSTEMROOT% }
  745. CSIDL_SYSTEM = $0025; { %SYSTEMROOT%\SYSTEM32 (may be system on 95/98/ME) }
  746. CSIDL_PROGRAM_FILES = $0026; { %SYSTEMDRIVE%\Program Files }
  747. CSIDL_MYPICTURES = $0027; { %USERPROFILE%\My Documents\My Pictures }
  748. CSIDL_PROFILE = $0028; { %USERPROFILE% }
  749. CSIDL_PROGRAM_FILES_COMMON = $002B; { %SYSTEMDRIVE%\Program Files\Common }
  750. CSIDL_COMMON_TEMPLATES = $002D; { %PROFILEPATH%\All Users\Templates }
  751. CSIDL_COMMON_DOCUMENTS = $002E; { %PROFILEPATH%\All Users\Documents }
  752. CSIDL_COMMON_ADMINTOOLS = $002F; { %PROFILEPATH%\All Users\Start Menu\Programs\Administrative Tools }
  753. CSIDL_ADMINTOOLS = $0030; { %USERPROFILE%\Start Menu\Programs\Administrative Tools }
  754. CSIDL_COMMON_MUSIC = $0035; { %PROFILEPATH%\All Users\Documents\my music }
  755. CSIDL_COMMON_PICTURES = $0036; { %PROFILEPATH%\All Users\Documents\my pictures }
  756. CSIDL_COMMON_VIDEO = $0037; { %PROFILEPATH%\All Users\Documents\my videos }
  757. CSIDL_CDBURN_AREA = $003B; { %USERPROFILE%\Local Settings\Application Data\Microsoft\CD Burning }
  758. CSIDL_PROFILES = $003E; { %PROFILEPATH% }
  759. CSIDL_FLAG_CREATE = $8000; { (force creation of requested folder if it doesn't exist yet) }
  760. Type
  761. PFNSHGetFolderPath = Function(Ahwnd: HWND; Csidl: Integer; Token: THandle; Flags: DWord; Path: PChar): HRESULT; stdcall;
  762. var
  763. SHGetFolderPath : PFNSHGetFolderPath = Nil;
  764. CFGDLLHandle : THandle = 0;
  765. Procedure InitDLL;
  766. Var
  767. P : Pointer;
  768. begin
  769. CFGDLLHandle:=LoadLibrary('shell32.dll');
  770. if (CFGDLLHandle<>0) then
  771. begin
  772. P:=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
  773. If (P=Nil) then
  774. begin
  775. FreeLibrary(CFGDLLHandle);
  776. CFGDllHandle:=0;
  777. end
  778. else
  779. SHGetFolderPath:=PFNSHGetFolderPath(P);
  780. end;
  781. If (P=Nil) then
  782. begin
  783. CFGDLLHandle:=LoadLibrary('shfolder.dll');
  784. if (CFGDLLHandle<>0) then
  785. begin
  786. P:=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
  787. If (P=Nil) then
  788. begin
  789. FreeLibrary(CFGDLLHandle);
  790. CFGDllHandle:=0;
  791. end
  792. else
  793. ShGetFolderPath:=PFNSHGetFolderPath(P);
  794. end;
  795. end;
  796. If (@ShGetFolderPath=Nil) then
  797. Raise Exception.Create('Could not determine SHGetFolderPath Function');
  798. end;
  799. Function GetSpecialDir(ID : Integer) : String;
  800. Var
  801. APath : Array[0..MAX_PATH] of char;
  802. begin
  803. Result:='';
  804. if (CFGDLLHandle=0) then
  805. InitDLL;
  806. If (SHGetFolderPath<>Nil) then
  807. begin
  808. if SHGetFolderPath(0,ID or CSIDL_FLAG_CREATE,0,0,@APATH[0])=S_OK then
  809. Result:=IncludeTrailingPathDelimiter(StrPas(@APath[0]));
  810. end;
  811. end;
  812. Function GetAppConfigDir(Global : Boolean) : String;
  813. begin
  814. If Global then
  815. Result:=DGetAppConfigDir(Global) // or use windows dir ??
  816. else
  817. begin
  818. Result:=GetSpecialDir(CSIDL_LOCAL_APPDATA)+ApplicationName;
  819. If (Result='') then
  820. Result:=DGetAppConfigDir(Global);
  821. end;
  822. end;
  823. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  824. begin
  825. if Global then
  826. begin
  827. Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
  828. if SubDir then
  829. Result:=IncludeTrailingPathDelimiter(Result+'Config');
  830. Result:=Result+ApplicationName+ConfigExtension;
  831. end
  832. else
  833. begin
  834. Result:=IncludeTrailingPathDelimiter(GetAppConfigDir(False));
  835. if SubDir then
  836. Result:=Result+'Config\';
  837. Result:=Result+ApplicationName+ConfigExtension;
  838. end;
  839. end;
  840. Procedure InitSysConfigDir;
  841. begin
  842. SetLength(SysConfigDir, MAX_PATH);
  843. SetLength(SysConfigDir, GetWindowsDirectory(PChar(SysConfigDir), MAX_PATH));
  844. end;
  845. {****************************************************************************
  846. Target Dependent WideString stuff
  847. ****************************************************************************}
  848. function Win32CompareWideString(const s1, s2 : WideString) : PtrInt;
  849. begin
  850. SetLastError(0);
  851. Result:=CompareStringW(LOCALE_USER_DEFAULT,0,pwidechar(s1),
  852. length(s1),pwidechar(s2),length(s2))-2;
  853. if GetLastError<>0 then
  854. RaiseLastOSError;
  855. end;
  856. function Win32CompareTextWideString(const s1, s2 : WideString) : PtrInt;
  857. begin
  858. SetLastError(0);
  859. Result:=CompareStringW(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pwidechar(s1),
  860. length(s1),pwidechar(s2),length(s2))-2;
  861. if GetLastError<>0 then
  862. RaiseLastOSError;
  863. end;
  864. function Win32AnsiUpperCase(const s: string): string;
  865. begin
  866. if length(s)>0 then
  867. begin
  868. result:=s;
  869. UniqueString(result);
  870. CharUpperBuff(pchar(result),length(result));
  871. end
  872. else
  873. result:='';
  874. end;
  875. function Win32AnsiLowerCase(const s: string): string;
  876. begin
  877. if length(s)>0 then
  878. begin
  879. result:=s;
  880. UniqueString(result);
  881. CharLowerBuff(pchar(result),length(result));
  882. end
  883. else
  884. result:='';
  885. end;
  886. function Win32AnsiCompareStr(const S1, S2: string): PtrInt;
  887. begin
  888. result:=CompareString(LOCALE_USER_DEFAULT,0,pchar(s1),length(s1),
  889. pchar(s2),length(s2))-2;
  890. end;
  891. function Win32AnsiCompareText(const S1, S2: string): PtrInt;
  892. begin
  893. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pchar(s1),length(s1),
  894. pchar(s2),length(s2))-2;
  895. end;
  896. function Win32AnsiStrComp(S1, S2: PChar): PtrInt;
  897. begin
  898. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,-1,s2,-1)-2;
  899. end;
  900. function Win32AnsiStrIComp(S1, S2: PChar): PtrInt;
  901. begin
  902. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,-1,s2,-1)-2;
  903. end;
  904. function Win32AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  905. begin
  906. result:=CompareString(LOCALE_USER_DEFAULT,0,s1,maxlen,s2,maxlen)-2;
  907. end;
  908. function Win32AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  909. begin
  910. result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,maxlen,s2,maxlen)-2;
  911. end;
  912. function Win32AnsiStrLower(Str: PChar): PChar;
  913. begin
  914. CharLower(str);
  915. result:=str;
  916. end;
  917. function Win32AnsiStrUpper(Str: PChar): PChar;
  918. begin
  919. CharUpper(str);
  920. result:=str;
  921. end;
  922. { there is a similiar procedure in the system unit which inits the fields which
  923. are relevant already for the system unit }
  924. procedure InitWin32Widestrings;
  925. begin
  926. widestringmanager.CompareWideStringProc:=@Win32CompareWideString;
  927. widestringmanager.CompareTextWideStringProc:=@Win32CompareTextWideString;
  928. end;
  929. Initialization
  930. InitWin32Widestrings;
  931. InitExceptions; { Initialize exceptions. OS independent }
  932. InitInternational; { Initialize internationalization settings }
  933. LoadVersionInfo;
  934. InitSysConfigDir;
  935. Finalization
  936. DoneExceptions;
  937. if kernel32dll<>0 then
  938. FreeLibrary(kernel32dll);
  939. if CFGDLLHandle<>0 then
  940. FreeLibrary(CFGDllHandle);
  941. end.