DirectX - How to detect DirectX version

From ISXKB

Jump to: navigation, search

This script at startup of setup check DirectX version, if version is not 4.08 warn user, and if user doesn't want to procede suggest to upgrade DirectX from Microsoft website http://www.microsoft.com/windows/directx/default.mspx.

DirectX version is stored in registry as 4.majorversion.minorversion

DirectX 8.0 is 4.8.0
DirectX 8.1 is 4.8.1
DirectX 9.0 is 4.9.0


Based on code provided by Mike C., NVRefreshTool

[Setup]
AppName=DirectX
AppVerName=DirectX
DefaultDirName={pf}\DirectX
DisableStartupPrompt=true
Uninstallable=false
DisableDirPage=true
OutputBaseFilename=DirectX
CreateAppDir=false
DisableProgramGroupPage=true

[Code]

procedure DecodeVersion( verstr: String; var verint: array of Integer );
var
  i,p: Integer; s: string;
begin
  // initialize array
  verint := [0,0,0,0];
  i := 0;
  while ( (Length(verstr) > 0) and (i < 4) ) do
  begin
  	p := pos('.', verstr);
  	if p > 0 then
  	begin
      if p = 1 then s:= '0' else s:= Copy( verstr, 1, p - 1 );
  	  verint[i] := StrToInt(s);
  	  i := i + 1;
  	  verstr := Copy( verstr, p+1, Length(verstr));
  	end
  	else
  	begin
  	  verint[i] := StrToInt( verstr );
  	  verstr := '';
  	end;
  end;

end;

// This function compares version string
// return -1 if ver1 < ver2
// return  0 if ver1 = ver2
// return  1 if ver1 > ver2
function CompareVersion( ver1, ver2: String ) : Integer;
var
  verint1, verint2: array of Integer;
  i: integer;
begin

  SetArrayLength( verint1, 4 );
  DecodeVersion( ver1, verint1 );

  SetArrayLength( verint2, 4 );
  DecodeVersion( ver2, verint2 );

  Result := 0; i := 0;
  while ( (Result = 0) and ( i < 4 ) ) do
  begin
  	if verint1[i] > verint2[i] then
  	  Result := 1
  	else
      if verint1[i] < verint2[i] then
  	    Result := -1
  	  else
  	    Result := 0;

  	i := i + 1;
  end;

end;

// DirectX version is stored in registry as 4.majorversion.minorversion
// DirectX 8.0 is 4.8.0
// DirectX 8.1 is 4.8.1
// DirectX 9.0 is 4.9.0

function GetDirectXVersion(): String;
var
  sVersion:  String;
begin
  sVersion := '';

  RegQueryStringValue( HKLM, 'SOFTWARE\Microsoft\DirectX', 'Version', sVersion );

  Result := sVersion;
end;


function InitializeSetup(): Boolean;
begin
  // in this case program needs at least directx 8.0
  if CompareVersion( GetDirectXVersion(), '4.8.0') < 0 then
  begin
    Result := MsgBox('DirectX 8 not found. The 3d test will not run and games may not perform optimally. Do you wish to continue?',
        mbConfirmation, MB_YESNO) = idYes;
    if Result = False then
      MsgBox('Please upgrade your DirectX Version at http://www.microsoft.com/windows/directx/default.mspx', mbInformation, MB_OK);
  end
  else
    Result := true;
end;
Personal tools
Ads: