LuaEditSysUtils.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. {The LuaEditSys Dll contains a bunch of system function. Its been created to
  2. "modularize" the LuaEdit code and may be useful for other project.}
  3. unit LuaEditSysUtils;
  4. interface
  5. uses Classes, SysUtils, Windows, Registry, ShellAPI, PSAPI;
  6. function GetFileSizeStr(Size: Cardinal): PChar; cdecl;
  7. function GetFileLastTimeModified(const sFileName: PChar): TDateTime; cdecl;
  8. function GetFileReadOnlyAttr(const sFileName: PChar): Boolean; cdecl;
  9. procedure ToggleFileReadOnlyAttr(const sFileName: PChar); cdecl;
  10. function GetFileVersion(const FileName: PChar): PChar; cdecl;
  11. function GetOSInfo: PChar; cdecl;
  12. function SetPrivilege(sPrivilegeName: PChar; bEnabled: boolean): boolean; cdecl;
  13. function WinExit(iFlags: integer): Boolean; cdecl;
  14. function BrowseURL(URL: PChar): Boolean; cdecl;
  15. function GetRunningProcesses(): PChar; cdecl;
  16. function ParamStrEx(Index: Integer; CommandLine: PChar; ExeName: PChar): PChar; cdecl;
  17. function ParamCountEx(CommandLine: PChar): Integer; cdecl;
  18. implementation
  19. // Return the specified size as the way we see it formated in windows' explorer
  20. function GetFileSizeStr(Size: Cardinal): PChar;
  21. begin
  22. if Size < 1024 then
  23. Result := PChar(IntToStr(Size) + ' Bytes')
  24. else
  25. if Size < (1024 * 1024) then
  26. Result := PChar(IntToStr(Size div (1024)) + 'KB')
  27. else if Size < (1024 * 1024 * 1024) then
  28. Result := PChar(IntToStr(Size div (1024 * 1024)) + 'MB')
  29. else
  30. Result := PChar(IntToStr(Size div (1024 * 1024 * 1024)) + 'GB')
  31. end;
  32. // Retrieve the date/time of the last modification applied to the specified file
  33. function GetFileLastTimeModified(const sFileName: PChar): TDateTime;
  34. var
  35. fHandle: THandle;
  36. rCreated, rLastAccessed, rLastWritten: TFileTime;
  37. tLastWritten: TSystemTime;
  38. test: String;
  39. begin
  40. Result := -1;
  41. fHandle := FileOpen(StrPas(sFileName), fmOpenRead);
  42. try
  43. GetFileTime(fHandle, @rCreated, @rLastAccessed, @rLastWritten);
  44. FileTimeToLocalFileTime(rLastWritten, rLastWritten);
  45. FileTimeToSystemTime(rLastWritten, tLastWritten);
  46. Result := SystemTimeToDateTime(tLastWritten);
  47. finally
  48. CloseHandle(fHandle);
  49. end;
  50. end;
  51. // Return whether or not a file is currently in read only mode
  52. function GetFileReadOnlyAttr(const sFileName: PChar): Boolean;
  53. begin
  54. Result := faReadOnly and FileGetAttr(StrPas(sFileName)) = faReadOnly;
  55. end;
  56. // Toggle a file's read only mode
  57. procedure ToggleFileReadOnlyAttr(const sFileName: PChar);
  58. var
  59. iAttr: Integer;
  60. begin
  61. if FileExists(StrPas(sFileName)) then
  62. begin
  63. iAttr := FileGetAttr(StrPas(sFileName));
  64. iAttr := iAttr xor faReadOnly;
  65. FileSetAttr(StrPas(sFileName), iAttr);
  66. end;
  67. end;
  68. // Return the version string (Major.Minor.Release.Build) of a file
  69. function GetFileVersion(const FileName: PChar): PChar;
  70. var
  71. VersionInfoSize,
  72. VersionInfoValueSize,
  73. Zero: DWORD;
  74. VersionInfo,
  75. VersionInfoValue : Pointer;
  76. VersionString: String;
  77. begin
  78. Result := '';
  79. VersionInfoSize := GetFileVersionInfoSize(FileName, Zero);
  80. if VersionInfoSize = 0 then
  81. Exit;
  82. GetMem(VersionInfo, VersionInfoSize);
  83. try
  84. if GetFileVersionInfo(FileName, 0, VersionInfoSize, VersionInfo) and VerQueryValue(VersionInfo, '\' { root block }, VersionInfoValue, VersionInfoValueSize) and (0 <> LongInt(VersionInfoValueSize)) then
  85. begin
  86. with TVSFixedFileInfo(VersionInfoValue^) do
  87. begin
  88. VersionString := IntToStr(HiWord(dwFileVersionMS));
  89. VersionString := VersionString + '.' + IntToStr(LoWord(dwFileVersionMS));
  90. VersionString := VersionString + '.' + IntToStr(HiWord(dwFileVersionLS));
  91. VersionString := VersionString + ' Build(' + IntToStr(LoWord(dwFileVersionLS)) + ')';
  92. Result := PChar(VersionString);
  93. end;
  94. end;
  95. finally
  96. FreeMem(VersionInfo);
  97. end;
  98. end;
  99. // Return a descriptive string of the current OS
  100. function GetOSInfo: PChar;
  101. var
  102. sPlatform: String;
  103. pVersionInfos: _OSVERSIONINFOA;
  104. begin
  105. pVersionInfos.dwOSVersionInfoSize := SizeOf(_OSVERSIONINFOA);
  106. GetVersionEx(pVersionInfos);
  107. case pVersionInfos.dwPlatformId of
  108. VER_PLATFORM_WIN32_NT:
  109. begin
  110. if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 2)) then
  111. begin
  112. sPlatform := 'Microsoft Windows Server 2003';
  113. end
  114. else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 1)) then
  115. begin
  116. sPlatform := 'Microsoft Windows XP';
  117. end
  118. else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 0)) then
  119. begin
  120. sPlatform := 'Microsoft Windows 2000';
  121. end
  122. else if pVersionInfos.dwMajorVersion <= 4 then
  123. begin
  124. sPlatform := 'Microsoft Windows NT';
  125. end;
  126. end;
  127. VER_PLATFORM_WIN32_WINDOWS:
  128. begin
  129. if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 0)) then
  130. begin
  131. sPlatform := 'Microsoft Windows 95';
  132. end
  133. else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 10)) then
  134. begin
  135. if pVersionInfos.szCSDVersion[1] = 'A' then
  136. begin
  137. sPlatform := 'Microsoft Windows 98 SE';
  138. end
  139. else
  140. begin
  141. sPlatform := 'Microsoft Windows 98';
  142. end;
  143. end
  144. else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 90)) then
  145. begin
  146. sPlatform := 'Microsoft Windows ME';
  147. end;
  148. end;
  149. else
  150. sPlatform := 'Unknown OS';
  151. end;
  152. Result := PChar(sPlatform);
  153. end;
  154. // This function set specified privileges to the current application
  155. // NOTE: The OS must agree the operation
  156. function SetPrivilege(sPrivilegeName: PChar; bEnabled: Boolean): Boolean;
  157. var
  158. TPPrev, TP: TTokenPrivileges;
  159. Token: THandle;
  160. dwRetLen: DWord;
  161. begin
  162. Result := False;
  163. OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
  164. TP.PrivilegeCount := 1;
  165. if(LookupPrivilegeValue(nil, sPrivilegeName, TP.Privileges[ 0 ].LUID))then
  166. begin
  167. if(bEnabled)then
  168. begin
  169. TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  170. end
  171. else
  172. begin
  173. TP.Privileges[ 0 ].Attributes := 0;
  174. end;
  175. dwRetLen := 0;
  176. Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
  177. end;
  178. CloseHandle(Token);
  179. end;
  180. // This function force the PC to reboot
  181. // WARNING: This does not display a reboot message. The caller function must
  182. // handle this in order to advise the user that a reboot is going to happen
  183. function WinExit(iFlags: integer): Boolean;
  184. begin
  185. Result := True;
  186. if(SetPrivilege('SeShutdownPrivilege', True))then
  187. begin
  188. if(not ExitWindowsEx(iFlags, 0))then
  189. begin
  190. // handle errors...
  191. Result := False;
  192. end;
  193. SetPrivilege('SeShutdownPrivilege', False)
  194. end else
  195. begin
  196. // handle errors...
  197. Result := False;
  198. end;
  199. end;
  200. // This function open the specified URL, in a new window, using the default
  201. // internet browser
  202. function BrowseURL(URL: PChar): Boolean;
  203. var
  204. Browser: String;
  205. pReg: TRegistry;
  206. begin
  207. Result := True;
  208. pReg := TRegistry.Create;
  209. Browser := '';
  210. pReg.RootKey := HKEY_CLASSES_ROOT;
  211. pReg.Access := KEY_QUERY_VALUE;
  212. // Open the registry key if available
  213. if pReg.OpenKey('\htmlfile\shell\open\command', False) then
  214. Browser := pReg.ReadString('');
  215. // Close the registry key
  216. pReg.CloseKey;
  217. // If a browser name was found in registry, we force to open it in a new window
  218. // by passing the url as command line parameter during the call
  219. if Browser <> '' then
  220. begin
  221. Browser := Copy(Browser, Pos('"', Browser) + 1, Length(Browser));
  222. Browser := Copy(Browser, 1, Pos('"', Browser) - 1);
  223. ShellExecute(0, 'open', PChar(Browser), URL, nil, SW_SHOW);
  224. end
  225. else
  226. Result := False;
  227. end;
  228. // Get the list of process identifiers.
  229. function GetRunningProcesses(): PChar;
  230. var
  231. aList: TStringList;
  232. lpidProcesses: array[0..1024] of DWord;
  233. cbNeeded, cProcesses, lphModule: DWord;
  234. hProcess: Cardinal;
  235. ProcessName: array[0..1024] of Char;
  236. i: Integer;
  237. begin
  238. Result := '';
  239. if not EnumProcesses(@lpidProcesses, sizeof(lpidProcesses), cbNeeded) then
  240. Exit;
  241. // Calculate how many process identifiers were returned.
  242. cProcesses := cbNeeded div sizeof(Cardinal);
  243. aList := TStringList.Create;
  244. // Retrieve the name and id of each process
  245. for i := 0 to cProcesses - 1 do
  246. begin
  247. hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, lpidProcesses[i]);
  248. if hProcess <> 0 then
  249. begin
  250. cbNeeded := 0;
  251. if EnumProcessModules(hProcess, @lphModule, sizeof(lphModule), cbNeeded) then
  252. begin
  253. GetModuleBaseName(hProcess, lphModule, ProcessName, sizeof(ProcessName));
  254. aList.Add(ProcessName);
  255. end;
  256. end;
  257. end;
  258. Result := aList.GetText;
  259. aList.Free;
  260. end;
  261. // This function returns the parameter part of the command line
  262. // NOTE: DO NOT EXPORT!!!!
  263. function GetParamStrEx(P: PChar; var Param: string): PChar;
  264. var
  265. i, Len: Integer;
  266. Start, S, Q: PChar;
  267. begin
  268. while True do
  269. begin
  270. while (P[0] <> #0) and (P[0] <= ' ') do
  271. P := CharNext(P);
  272. if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
  273. end;
  274. Len := 0;
  275. Start := P;
  276. while P[0] > ' ' do
  277. begin
  278. if P[0] = '"' then
  279. begin
  280. P := CharNext(P);
  281. while (P[0] <> #0) and (P[0] <> '"') do
  282. begin
  283. Q := CharNext(P);
  284. Inc(Len, Q - P);
  285. P := Q;
  286. end;
  287. if P[0] <> #0 then
  288. P := CharNext(P);
  289. end
  290. else
  291. begin
  292. Q := CharNext(P);
  293. Inc(Len, Q - P);
  294. P := Q;
  295. end;
  296. end;
  297. SetLength(Param, Len);
  298. P := Start;
  299. S := Pointer(Param);
  300. i := 0;
  301. while P[0] > ' ' do
  302. begin
  303. if P[0] = '"' then
  304. begin
  305. P := CharNext(P);
  306. while (P[0] <> #0) and (P[0] <> '"') do
  307. begin
  308. Q := CharNext(P);
  309. while P < Q do
  310. begin
  311. S[i] := P^;
  312. Inc(P);
  313. Inc(i);
  314. end;
  315. end;
  316. if P[0] <> #0 then P := CharNext(P);
  317. end
  318. else
  319. begin
  320. Q := CharNext(P);
  321. while P < Q do
  322. begin
  323. S[i] := P^;
  324. Inc(P);
  325. Inc(i);
  326. end;
  327. end;
  328. end;
  329. Result := P;
  330. end;
  331. // Get the x parameter out of the specified command line
  332. function ParamStrEx(Index: Integer; CommandLine: PChar; ExeName: PChar): PChar;
  333. var
  334. P: PChar;
  335. Buffer: array[0..260] of Char;
  336. StrResult: String;
  337. begin
  338. StrResult := '';
  339. if Index = 0 then
  340. StrResult := ExeName
  341. else
  342. begin
  343. P := CommandLine;
  344. while True do
  345. begin
  346. P := GetParamStrEx(P, StrResult);
  347. if (Index = 0) or (StrResult = '') then
  348. Break;
  349. Dec(Index);
  350. end;
  351. end;
  352. Result := PChar(StrResult);
  353. end;
  354. // Returns the number of parameter found in the specified command line
  355. function ParamCountEx(CommandLine: PChar): Integer;
  356. var
  357. P: PChar;
  358. S: String;
  359. begin
  360. Result := 0;
  361. P := GetParamStrEx(CommandLine, S);
  362. while True do
  363. begin
  364. P := GetParamStrEx(P, S);
  365. if S = '' then
  366. Break;
  367. Inc(Result);
  368. end;
  369. end;
  370. end.