123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- {The LuaEditSys Dll contains a bunch of system function. Its been created to
- "modularize" the LuaEdit code and may be useful for other project.}
- unit LuaEditSysUtils;
- interface
- uses Classes, SysUtils, Windows, Registry, ShellAPI, PSAPI;
- function GetFileSizeStr(Size: Cardinal): PChar; cdecl;
- function GetFileLastTimeModified(const sFileName: PChar): TDateTime; cdecl;
- function GetFileReadOnlyAttr(const sFileName: PChar): Boolean; cdecl;
- procedure ToggleFileReadOnlyAttr(const sFileName: PChar); cdecl;
- function GetFileVersion(const FileName: PChar): PChar; cdecl;
- function GetOSInfo: PChar; cdecl;
- function SetPrivilege(sPrivilegeName: PChar; bEnabled: boolean): boolean; cdecl;
- function WinExit(iFlags: integer): Boolean; cdecl;
- function BrowseURL(URL: PChar): Boolean; cdecl;
- function GetRunningProcesses(): PChar; cdecl;
- function ParamStrEx(Index: Integer; CommandLine: PChar; ExeName: PChar): PChar; cdecl;
- function ParamCountEx(CommandLine: PChar): Integer; cdecl;
- implementation
- // Return the specified size as the way we see it formated in windows' explorer
- function GetFileSizeStr(Size: Cardinal): PChar;
- begin
- if Size < 1024 then
- Result := PChar(IntToStr(Size) + ' Bytes')
- else
- if Size < (1024 * 1024) then
- Result := PChar(IntToStr(Size div (1024)) + 'KB')
- else if Size < (1024 * 1024 * 1024) then
- Result := PChar(IntToStr(Size div (1024 * 1024)) + 'MB')
- else
- Result := PChar(IntToStr(Size div (1024 * 1024 * 1024)) + 'GB')
- end;
- // Retrieve the date/time of the last modification applied to the specified file
- function GetFileLastTimeModified(const sFileName: PChar): TDateTime;
- var
- fHandle: THandle;
- rCreated, rLastAccessed, rLastWritten: TFileTime;
- tLastWritten: TSystemTime;
- test: String;
- begin
- Result := -1;
- fHandle := FileOpen(StrPas(sFileName), fmOpenRead);
- try
- GetFileTime(fHandle, @rCreated, @rLastAccessed, @rLastWritten);
- FileTimeToLocalFileTime(rLastWritten, rLastWritten);
- FileTimeToSystemTime(rLastWritten, tLastWritten);
- Result := SystemTimeToDateTime(tLastWritten);
- finally
- CloseHandle(fHandle);
- end;
- end;
- // Return whether or not a file is currently in read only mode
- function GetFileReadOnlyAttr(const sFileName: PChar): Boolean;
- begin
- Result := faReadOnly and FileGetAttr(StrPas(sFileName)) = faReadOnly;
- end;
- // Toggle a file's read only mode
- procedure ToggleFileReadOnlyAttr(const sFileName: PChar);
- var
- iAttr: Integer;
- begin
- if FileExists(StrPas(sFileName)) then
- begin
- iAttr := FileGetAttr(StrPas(sFileName));
- iAttr := iAttr xor faReadOnly;
- FileSetAttr(StrPas(sFileName), iAttr);
- end;
- end;
- // Return the version string (Major.Minor.Release.Build) of a file
- function GetFileVersion(const FileName: PChar): PChar;
- var
- VersionInfoSize,
- VersionInfoValueSize,
- Zero: DWORD;
- VersionInfo,
- VersionInfoValue : Pointer;
- VersionString: String;
- begin
- Result := '';
- VersionInfoSize := GetFileVersionInfoSize(FileName, Zero);
- if VersionInfoSize = 0 then
- Exit;
- GetMem(VersionInfo, VersionInfoSize);
- try
- if GetFileVersionInfo(FileName, 0, VersionInfoSize, VersionInfo) and VerQueryValue(VersionInfo, '\' { root block }, VersionInfoValue, VersionInfoValueSize) and (0 <> LongInt(VersionInfoValueSize)) then
- begin
- with TVSFixedFileInfo(VersionInfoValue^) do
- begin
- VersionString := IntToStr(HiWord(dwFileVersionMS));
- VersionString := VersionString + '.' + IntToStr(LoWord(dwFileVersionMS));
- VersionString := VersionString + '.' + IntToStr(HiWord(dwFileVersionLS));
- VersionString := VersionString + ' Build(' + IntToStr(LoWord(dwFileVersionLS)) + ')';
- Result := PChar(VersionString);
- end;
- end;
- finally
- FreeMem(VersionInfo);
- end;
- end;
- // Return a descriptive string of the current OS
- function GetOSInfo: PChar;
- var
- sPlatform: String;
- pVersionInfos: _OSVERSIONINFOA;
- begin
- pVersionInfos.dwOSVersionInfoSize := SizeOf(_OSVERSIONINFOA);
- GetVersionEx(pVersionInfos);
- case pVersionInfos.dwPlatformId of
- VER_PLATFORM_WIN32_NT:
- begin
- if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 2)) then
- begin
- sPlatform := 'Microsoft Windows Server 2003';
- end
- else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 1)) then
- begin
- sPlatform := 'Microsoft Windows XP';
- end
- else if ((pVersionInfos.dwMajorVersion = 5) and (pVersionInfos.dwMinorVersion = 0)) then
- begin
- sPlatform := 'Microsoft Windows 2000';
- end
- else if pVersionInfos.dwMajorVersion <= 4 then
- begin
- sPlatform := 'Microsoft Windows NT';
- end;
- end;
- VER_PLATFORM_WIN32_WINDOWS:
- begin
- if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 0)) then
- begin
- sPlatform := 'Microsoft Windows 95';
- end
- else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 10)) then
- begin
- if pVersionInfos.szCSDVersion[1] = 'A' then
- begin
- sPlatform := 'Microsoft Windows 98 SE';
- end
- else
- begin
- sPlatform := 'Microsoft Windows 98';
- end;
- end
- else if ((pVersionInfos.dwMajorVersion = 4) and (pVersionInfos.dwMinorVersion = 90)) then
- begin
- sPlatform := 'Microsoft Windows ME';
- end;
- end;
- else
- sPlatform := 'Unknown OS';
- end;
- Result := PChar(sPlatform);
- end;
- // This function set specified privileges to the current application
- // NOTE: The OS must agree the operation
- function SetPrivilege(sPrivilegeName: PChar; bEnabled: Boolean): Boolean;
- var
- TPPrev, TP: TTokenPrivileges;
- Token: THandle;
- dwRetLen: DWord;
- begin
- Result := False;
- OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
- TP.PrivilegeCount := 1;
-
- if(LookupPrivilegeValue(nil, sPrivilegeName, TP.Privileges[ 0 ].LUID))then
- begin
- if(bEnabled)then
- begin
- TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
- end
- else
- begin
- TP.Privileges[ 0 ].Attributes := 0;
- end;
- dwRetLen := 0;
- Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
- end;
- CloseHandle(Token);
- end;
- // This function force the PC to reboot
- // WARNING: This does not display a reboot message. The caller function must
- // handle this in order to advise the user that a reboot is going to happen
- function WinExit(iFlags: integer): Boolean;
- begin
- Result := True;
- if(SetPrivilege('SeShutdownPrivilege', True))then
- begin
- if(not ExitWindowsEx(iFlags, 0))then
- begin
- // handle errors...
- Result := False;
- end;
-
- SetPrivilege('SeShutdownPrivilege', False)
- end else
- begin
- // handle errors...
- Result := False;
- end;
- end;
- // This function open the specified URL, in a new window, using the default
- // internet browser
- function BrowseURL(URL: PChar): Boolean;
- var
- Browser: String;
- pReg: TRegistry;
- begin
- Result := True;
- pReg := TRegistry.Create;
- Browser := '';
- pReg.RootKey := HKEY_CLASSES_ROOT;
- pReg.Access := KEY_QUERY_VALUE;
- // Open the registry key if available
- if pReg.OpenKey('\htmlfile\shell\open\command', False) then
- Browser := pReg.ReadString('');
- // Close the registry key
- pReg.CloseKey;
- // If a browser name was found in registry, we force to open it in a new window
- // by passing the url as command line parameter during the call
- if Browser <> '' then
- begin
- Browser := Copy(Browser, Pos('"', Browser) + 1, Length(Browser));
- Browser := Copy(Browser, 1, Pos('"', Browser) - 1);
- ShellExecute(0, 'open', PChar(Browser), URL, nil, SW_SHOW);
- end
- else
- Result := False;
- end;
- // Get the list of process identifiers.
- function GetRunningProcesses(): PChar;
- var
- aList: TStringList;
- lpidProcesses: array[0..1024] of DWord;
- cbNeeded, cProcesses, lphModule: DWord;
- hProcess: Cardinal;
- ProcessName: array[0..1024] of Char;
- i: Integer;
- begin
- Result := '';
- if not EnumProcesses(@lpidProcesses, sizeof(lpidProcesses), cbNeeded) then
- Exit;
- // Calculate how many process identifiers were returned.
- cProcesses := cbNeeded div sizeof(Cardinal);
- aList := TStringList.Create;
- // Retrieve the name and id of each process
- for i := 0 to cProcesses - 1 do
- begin
- hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, lpidProcesses[i]);
- if hProcess <> 0 then
- begin
- cbNeeded := 0;
-
- if EnumProcessModules(hProcess, @lphModule, sizeof(lphModule), cbNeeded) then
- begin
- GetModuleBaseName(hProcess, lphModule, ProcessName, sizeof(ProcessName));
- aList.Add(ProcessName);
- end;
- end;
- end;
- Result := aList.GetText;
- aList.Free;
- end;
- // This function returns the parameter part of the command line
- // NOTE: DO NOT EXPORT!!!!
- function GetParamStrEx(P: PChar; var Param: string): PChar;
- var
- i, Len: Integer;
- Start, S, Q: PChar;
- begin
- while True do
- begin
- while (P[0] <> #0) and (P[0] <= ' ') do
- P := CharNext(P);
- if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
- end;
- Len := 0;
- Start := P;
- while P[0] > ' ' do
- begin
- if P[0] = '"' then
- begin
- P := CharNext(P);
- while (P[0] <> #0) and (P[0] <> '"') do
- begin
- Q := CharNext(P);
- Inc(Len, Q - P);
- P := Q;
- end;
- if P[0] <> #0 then
- P := CharNext(P);
- end
- else
- begin
- Q := CharNext(P);
- Inc(Len, Q - P);
- P := Q;
- end;
- end;
- SetLength(Param, Len);
- P := Start;
- S := Pointer(Param);
- i := 0;
- while P[0] > ' ' do
- begin
- if P[0] = '"' then
- begin
- P := CharNext(P);
- while (P[0] <> #0) and (P[0] <> '"') do
- begin
- Q := CharNext(P);
- while P < Q do
- begin
- S[i] := P^;
- Inc(P);
- Inc(i);
- end;
- end;
- if P[0] <> #0 then P := CharNext(P);
- end
- else
- begin
- Q := CharNext(P);
- while P < Q do
- begin
- S[i] := P^;
- Inc(P);
- Inc(i);
- end;
- end;
- end;
- Result := P;
- end;
- // Get the x parameter out of the specified command line
- function ParamStrEx(Index: Integer; CommandLine: PChar; ExeName: PChar): PChar;
- var
- P: PChar;
- Buffer: array[0..260] of Char;
- StrResult: String;
- begin
- StrResult := '';
-
- if Index = 0 then
- StrResult := ExeName
- else
- begin
- P := CommandLine;
- while True do
- begin
- P := GetParamStrEx(P, StrResult);
-
- if (Index = 0) or (StrResult = '') then
- Break;
- Dec(Index);
- end;
- end;
-
- Result := PChar(StrResult);
- end;
- // Returns the number of parameter found in the specified command line
- function ParamCountEx(CommandLine: PChar): Integer;
- var
- P: PChar;
- S: String;
- begin
- Result := 0;
- P := GetParamStrEx(CommandLine, S);
-
- while True do
- begin
- P := GetParamStrEx(P, S);
-
- if S = '' then
- Break;
- Inc(Result);
- end;
- end;
- end.
|