LuaEditSysUtils.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. function GetFileLastTimeModified(const sFileName: PChar): TDateTime;
  6. function GetFileReadOnlyAttr(const sFileName: PChar): Boolean;
  7. procedure ToggleFileReadOnlyAttr(const sFileName: PChar);
  8. function GetFileVersion(const FileName: PChar): PChar;
  9. function GetOSInfo: PChar;
  10. function SetPrivilege(sPrivilegeName: PChar; bEnabled: boolean): boolean;
  11. function WinExit(iFlags: integer): Boolean;
  12. implementation
  13. uses SysUtils, Windows;
  14. function GetFileLastTimeModified(const sFileName: PChar): TDateTime;
  15. var
  16. fHandle: THandle;
  17. rCreated, rLastAccessed, rLastWritten: TFileTime;
  18. tLastWritten: TSystemTime;
  19. test: String;
  20. begin
  21. Result := -1;
  22. fHandle := FileOpen(StrPas(sFileName), fmOpenRead);
  23. try
  24. GetFileTime(fHandle, @rCreated, @rLastAccessed, @rLastWritten);
  25. FileTimeToLocalFileTime(rLastWritten, rLastWritten);
  26. FileTimeToSystemTime(rLastWritten, tLastWritten);
  27. Result := SystemTimeToDateTime(tLastWritten);//SystemTimeToDateTime(tAccessed);
  28. finally
  29. CloseHandle(fHandle);
  30. end;
  31. end;
  32. function GetFileReadOnlyAttr(const sFileName: PChar): Boolean;
  33. begin
  34. Result := faReadOnly and FileGetAttr(StrPas(sFileName)) = faReadOnly;
  35. end;
  36. procedure ToggleFileReadOnlyAttr(const sFileName: PChar);
  37. var
  38. iAttr: Integer;
  39. begin
  40. if FileExists(StrPas(sFileName)) then
  41. begin
  42. iAttr := FileGetAttr(StrPas(sFileName));
  43. iAttr := iAttr xor faReadOnly;
  44. FileSetAttr(StrPas(sFileName), iAttr);
  45. end;
  46. end;
  47. function GetFileVersion(const FileName: PChar): PChar;
  48. var
  49. VersionInfoSize,
  50. VersionInfoValueSize,
  51. Zero: DWORD;
  52. VersionInfo,
  53. VersionInfoValue : Pointer;
  54. VersionString: String;
  55. begin
  56. Result := '';
  57. VersionInfoSize := GetFileVersionInfoSize(FileName, Zero);
  58. if VersionInfoSize = 0 then
  59. Exit;
  60. GetMem(VersionInfo, VersionInfoSize);
  61. try
  62. if GetFileVersionInfo(FileName, 0, VersionInfoSize, VersionInfo) and VerQueryValue(VersionInfo, '\' { root block }, VersionInfoValue, VersionInfoValueSize) and (0 <> LongInt(VersionInfoValueSize)) then
  63. begin
  64. with TVSFixedFileInfo(VersionInfoValue^) do
  65. begin
  66. VersionString := IntToStr(HiWord(dwFileVersionMS));
  67. VersionString := VersionString + '.' + IntToStr(LoWord(dwFileVersionMS));
  68. VersionString := VersionString + '.' + IntToStr(HiWord(dwFileVersionLS));
  69. VersionString := VersionString + ' Build(' + IntToStr(LoWord(dwFileVersionLS)) + ')';
  70. Result := PChar(VersionString);
  71. end;
  72. end;
  73. finally
  74. FreeMem(VersionInfo);
  75. end;
  76. end;
  77. function GetOSInfo: PChar;
  78. var
  79. sPlatform: String;
  80. pVersionInfos: _OSVERSIONINFOA;
  81. begin
  82. pVersionInfos.dwOSVersionInfoSize := SizeOf(_OSVERSIONINFOA);
  83. GetVersionEx(pVersionInfos);
  84. case pVersionInfos.dwPlatformId of
  85. VER_PLATFORM_WIN32_NT:
  86. begin
  87. if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 2)) then
  88. begin
  89. sPlatform := 'Microsoft Windows Server 2003';
  90. end
  91. else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 1)) then
  92. begin
  93. sPlatform := 'Microsoft Windows XP';
  94. end
  95. else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 0)) then
  96. begin
  97. sPlatform := 'Microsoft Windows 2000';
  98. end
  99. else if pVersionInfos.dwMajorVersion <= 4 then
  100. begin
  101. sPlatform := 'Microsoft Windows NT';
  102. end;
  103. end;
  104. VER_PLATFORM_WIN32_WINDOWS:
  105. begin
  106. if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 0)) then
  107. begin
  108. sPlatform := 'Microsoft Windows 95';
  109. end
  110. else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 10)) then
  111. begin
  112. if pVersionInfos.szCSDVersion[1] = 'A' then
  113. begin
  114. sPlatform := 'Microsoft Windows 98 SE';
  115. end
  116. else
  117. begin
  118. sPlatform := 'Microsoft Windows 98';
  119. end;
  120. end
  121. else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 90)) then
  122. begin
  123. sPlatform := 'Microsoft Windows ME';
  124. end;
  125. end;
  126. else
  127. sPlatform := 'Unknown OS';
  128. end;
  129. Result := PChar(sPlatform);
  130. end;
  131. function SetPrivilege(sPrivilegeName: PChar; bEnabled: Boolean): Boolean;
  132. var
  133. TPPrev, TP: TTokenPrivileges;
  134. Token: THandle;
  135. dwRetLen: DWord;
  136. begin
  137. Result := False;
  138. OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
  139. TP.PrivilegeCount := 1;
  140. if(LookupPrivilegeValue(nil, sPrivilegeName, TP.Privileges[ 0 ].LUID))then
  141. begin
  142. if(bEnabled)then
  143. begin
  144. TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  145. end
  146. else
  147. begin
  148. TP.Privileges[ 0 ].Attributes := 0;
  149. end;
  150. dwRetLen := 0;
  151. Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
  152. end;
  153. CloseHandle(Token);
  154. end;
  155. function WinExit(iFlags: integer): Boolean;
  156. begin
  157. Result := True;
  158. if(SetPrivilege('SeShutdownPrivilege', True))then
  159. begin
  160. if(not ExitWindowsEx(iFlags, 0))then
  161. begin
  162. // handle errors...
  163. Result := False;
  164. end;
  165. SetPrivilege('SeShutdownPrivilege', False)
  166. end else
  167. begin
  168. // handle errors...
  169. Result := False;
  170. end;
  171. end;
  172. end.