DirectX Installation with Web Installer

From NSIS Wiki

The following is for NSIS installations that need DirectX and you assume the user to be connected to the Internet. While this isn't completely full-proof, it's better than adding over 100mb to your install file.

For off-line installers, especially when distributed via DVD, CD-ROM or other fixed media, see DirectX Installation for Fixed Media.

Prerequisites

Obtain the latest DirectX End-User Runtime Web Installer from Microsoft. It's very small as it dynamically pulls the content it needs when run.

This script also assumes the NSIS installer is requesting Admin privileges in the form of RequestExecutionLevel admin.

About the Web Installer

When you run the Web installer, the installer will do the following:

  1. Examine the user's current DirectX install.
  2. Obtain a list of all available DirectX files from the Microsoft servers.
  3. Compare the local install with the latest available.
  4. Download files not existent on the user computer.
  5. Install the new files.
  6. Clean up the existing DirectX install.

The obvious flaw is when a user has unreliable Internet access. The web installer isn't very smart and will take a long time to exit if the Internet doesn't exist for the end user. You will need to introduce messaging to the user that explains the update will work best with Internet access.

Installing DirectX

The process of using the web installer is the following:

  1. Copy the Web Installer onto the user's TEMP folder.
  2. Run the installer from the TEMP folder.
  3. Wait till DirectX is done updating.
  4. Remove the locally stored installer from the TEMP folder.

To start, you will need to declare the following variable early in the NSIS script:

Var DirectXSetupError

To make things easier on yourself, create a new Section for DirectX. NSIS will run the section while installing. The placement of the section could go anywhere, but it's highly recommended that DirectX be the last install section after everything else is complete. This is incase DirectX fails, your software will be installed.

If you use component install UI, DirectX will show up as an entry. You can force on with "SectionIn RO", or leave out if you wish the user to have the option to run the DirectX install.

Section "DirectX Install" SEC_DIRECTX
 
 SectionIn RO
 
 SetOutPath "$TEMP"
 File "dxwebsetup.exe"
 DetailPrint "Running DirectX Setup..."
 ExecWait '"$TEMP\dxwebsetup.exe" /Q' $DirectXSetupError
 DetailPrint "Finished DirectX Setup"
 
 Delete "$TEMP\dxwebsetup.exe"
 
 SetOutPath "$INSTDIR"
 
SectionEnd

Note: I redeclare the SetOutPath to $INSTDIR. This way shortcuts and other functions using SetOutPath won't get messed up. Alter this line accordingly.

The variable $DirectXSetupError is used to force NSIS to pause while the DirectX setup is running. You can use the error code to further script error handling, but a variable is required to be there in order for NSIS to wait.

I've also included DetailPrint lines to display the DirectX status in the install logs. These strings can be localized to your liking.

See Also

Personal tools
donate
ads