Get .NET Version

From NSIS Wiki
Jump to navigationJump to search
Author: kichik (talk, contrib)


This function retrieves the latest version of the .NET runtime library installed on the user's computer. It can also be used to detect .NET, as it will return no version where the .NET runtime library is not installed.

Unlike other functions available, this one doesn't rely on the registry. It uses the API provided by Microsoft. APIs are much more reliable and backward compatible than the registry. In this case, it's also much easier to use. There's no need to enumerate any registry key, looking for the latest version. It uses GetCORVersion to retrieve the latest installed version of .NET runtime library.

WARNING: does not work for .NET 3.0 and up

The Function

Function GetDotNETVersion
  Push $0
  Push $1
 
  System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1 ?u"
  StrCmp $1 0 +2
    StrCpy $0 "not found"
 
  Pop $1
  Exch $0
FunctionEnd

Usage Example

This example uses VersionCompare to make sure the user has at least version 1.1 installed.

# comment the next two lines for NSIS version < 2.08
!include WordFunc.nsh
!insertmacro VersionCompare
 
!include LogicLib.nsh
 
Name ".NET Product"
OutFile "ProductInstaller.exe"
 
Function .onInit
  Call GetDotNETVersion
  Pop $0
  ${If} $0 == "not found"
    MessageBox MB_OK|MB_ICONSTOP ".NET runtime library is not installed."
    Abort
  ${EndIf}
 
  StrCpy $0 $0 "" 1 # skip "v"
 
  ${VersionCompare} $0 "1.1" $1
  ${If} $1 == 2
    MessageBox MB_OK|MB_ICONSTOP ".NET runtime library v1.1 or newer is required. You have $0."
    Abort
  ${EndIf}
FunctionEnd
 
Function GetDotNETVersion
  Push $0
  Push $1
 
  System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1 ?u"
  StrCmp $1 "error" 0 +2
    StrCpy $0 "not found"
 
  Pop $1
  Exch $0
FunctionEnd
 
Section
SectionEnd