Get .NET Version: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
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.
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 [http://msdn.microsoft.com/library/en-us/cpgenref/html/grfungetcorversion.asp GetCORVersion] to retrieve the latest installed version of .NET runtime library.
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 [http://msdn.microsoft.com/en-us/library/87d7kehw.aspx GetCORVersion] to retrieve the latest installed version of .NET runtime library.
 
'''WARNING:''' does not work for .NET 3.0 and up
 
==The Function==
==The Function==
<highlight-nsis>Function GetDotNETVersion
<highlight-nsis>Function GetDotNETVersion
Line 8: Line 11:
   Push $1
   Push $1


   System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1"
   System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1 ?u"
   StrCmp $1 0 +2
   StrCmp $1 0 +2
     StrCpy $0 "not found"
     StrCpy $0 "not found"
Line 47: Line 50:
   Push $1
   Push $1


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

Latest revision as of 10:43, 5 January 2010

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