////////////////////////////////////////////////////////////////////////////////
// Windows10.pas
// -------------
// This unit contains functions needed to operate on anything above WinXP.
// I have named the unit Windows10 because that is currently the latest OS.                                                                 
//
// Last Edit: September 12th 2019
// Certified: NA
//
// This unit is coded in : Delphi 7
//
// Unit is copyright (c) 2019, The CoolMon Project
////////////////////////////////////////////////////////////////////////////////
//
// This unit can aquire current elevation level and run an application "as
// admin" which is required for the plugin manager and the autoupdater/installer
// on Windows versions newer than WinXP
//
////////////////////////////////////////////////////////////////////////////////
unit Windows10;

interface

uses Windows, ShellApi, SysUtils, Forms, Registry; // Forms is used for the Application.Handle call

Type
 TWinVer = Class(TObject)
 private
  fBrandName: String;
  fMajorVersion: Integer;
  fMinorVersion: Integer;
  fBuildVersion: Integer;
  fPlatForm: Integer;
  fWinVersionString: String;
  Procedure BuildVersionString;
  Procedure BuildBrandName;
 public
  Constructor Create;
  Property MajorVersion: Integer read fMajorVersion;
  Property MinorVersion: Integer read fMinorVersion;
  Property BuildVersion: Integer read fBuildVersion;
  Property PlatForm: Integer read fPlatForm;
  Property WinVersionString: String read fWinVersionString;
  Property BrandName: String read fBrandName;
 end;
// Windows Version


// Elevated Priviliged and Execution
Procedure RunAsAdmin(hWnd: HWND; aFile: string; aParameters: string);
Procedure ShellExecutionAsAdmin(aFile: String);
Function HasAdminAccess: Boolean;
Procedure RestartApplicationWithElevatedPriviliges;



implementation

Procedure RunAsAdmin(hWnd: HWND; aFile: string; aParameters: string);
var
 sei: TShellExecuteInfoA;
begin
 FillChar(sei, SizeOf(sei), 0);
 sei.cbSize := sizeOf(sei);
 sei.Wnd := hWnd; // No documentation of this, but perhaps application.handle
 sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
 sei.lpVerb := 'runas';
 sei.lpFile := PChar(aFile);
 sei.lpParameters := PChar(aParameters);
 sei.nShow := SW_SHOWNORMAL;
 if not ShellExecuteEx(@sei) then
  RaiseLastOSError;
end;

Procedure ShellExecutionAsAdmin(aFile: String);
Begin
 ShellExecute(Application.Handle, 'runas', PAnsiChar(aFile), nil, nil, SW_SHOWNORMAL);
End;

Function HasAdminAccess: Boolean;
Var
 Reg: TRegistry;
begin
 Result := True;
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_LOCAL_MACHINE;
 Try
  Reg.OpenKey('\SOFTWARE\The CoolMon Project', true);
  Reg.WriteString('ElevationTest', 'If You Can Read This, There Was Admin Access');
 Except
  on ERegistryException do begin
   Result := False;
  end;
 end;
 Reg.Free;
end;

Procedure RestartApplicationWithElevatedPriviliges;
Begin
 ShellExecutionAsAdmin(ParamStr(0));
 Application.Terminate;
 Application.ProcessMessages;


End;

{ TWinVer }

procedure TWinVer.BuildBrandName;
Var
 ver: string;
begin
{
Windows 10	10.0*
Windows Server 2019	10.0*
Windows Server 2016	10.0*
Windows 8.1	6.3*
Windows Server 2012 R2	6.3*
Windows 8	6.2
Windows Server 2012	6.2
Windows 7	6.1
Windows Server 2008 R2	6.1
Windows Server 2008	6.0
Windows Vista	6.0
Windows Server 2003 R2	5.2
Windows Server 2003	5.2
Windows XP 64-Bit Edition	5.2
Windows XP	5.1
Windows 2000	5.0}

 Case fMajorVersion of
  5: Case fMinorVersion of
      0: ver := '2000';
      1: ver := 'XP';
      2: ver := 'XP x64';
      else ver := 'XP Era';
     end;
  6: Case fMinorVersion of
      0: ver := 'Vista';
      1: ver := '7';
      2: ver := '8';
      3: ver := '8.1';
      else ver := 'Vista/Win8 Era';
     end;
  10: ver := '10';
  else ver := 'Unknown';
 End;
 fBrandName := 'Windows ' + ver;
end;

procedure TWinVer.BuildVersionString;
begin
 fWinVersionString := IntToStr(fMajorVersion) + '.' +
                      IntToStr(fMinorVersion) + '.' +
                      IntToStr(fBuildVersion) + ' [Platform: ' +
                      IntToStr(fPlatForm) + ']';
end;

constructor TWinVer.Create;
begin
 fPlatForm := Win32Platform;
 fMajorVersion := Win32MajorVersion;
 fMinorVersion := Win32MinorVersion;
 fBuildVersion := Win32BuildNumber;
 BuildVersionString;
 BuildBrandName;
end;

end.
