Get directory of installed .NET runtime: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
Line 72: Line 72:
== Issues ==
== Issues ==


None at this time.
It is difficult to know for sure whether calls to external programs, such as RegAsm.exe, in the example above, really succeeded. I tried redirecting the output of RegAsm.exe to a file, and then opening the file to look for the magic words "Types succesfully registered.", with some success. However, if the target machine is an older version of Windows (NT or 95/98/ME) then redirection behaves differently (i.e. not at all, in my recent experience) meaning you can't reliably test that the RegAsm call succeeded.
 


[[Category:DLL, OCX, TLB Functions]]
[[Category:DLL, OCX, TLB Functions]]

Revision as of 12:43, 20 April 2006

Description

Given a .Net version number, this function returns that .Net framework's install directory. This is useful if you intend to call 'RegAsm.exe', or 'GACUtil.exe' on one of your .Net assembly DLLs, since these executables are stored in the directory this function returns.

Parameters

in: version of .Net framework required (eg. "v2.0")
out: directory in which it is installed (eg. "C:\WINNT\Microsoft.NET\Framework\v2.0.50727")

Returns "" if the specified version of the .Net framework does not appear to be installed.

Usage

Section "Install"
 
	; get directory of .NET framework installation
	Push "v2.0"
	Call GetDotNetDir
	Pop $R0 ; .net framework v2.0 installation directory
	StrCmpS "" $R0 err_dot_net_not_found
 
	; Perform our install
	; e.g. use the .Net path in $R0 to call RegAsm.exe
	SetOutPath $INSTDIR
	File "MyDll.dll"
	ExecWait '"$R0\RegAsm.exe" MyDll.dll'
 
	Return
 
err_dot_net_not_found:
	Abort "Aborted: .Net framework not found."
 
SectionEnd

Function

; Given a .NET version number, this function returns that .NET framework's
; install directory. Returns "" if the given .NET version is not installed.
; Params: [version] (eg. "v2.0")
; Return: [dir] (eg. "C:\WINNT\Microsoft.NET\Framework\v2.0.50727")
Function GetDotNetDir
	Exch $R0 ; Set R0 to .net version major
	Push $R1
	Push $R2
 
	; set R1 to minor version number of the installed .NET runtime
	EnumRegValue $R1 HKLM \
		"Software\Microsoft\.NetFramework\policy\$R0" 0
	IfErrors getdotnetdir_err
 
	; set R2 to .NET install dir root
	ReadRegStr $R2 HKLM \
		"Software\Microsoft\.NetFramework" "InstallRoot"
	IfErrors getdotnetdir_err
 
	; set R0 to the .NET install dir full
	StrCpy $R0 "$R2$R0.$R1"
 
getdotnetdir_end:
	Pop $R2
	Pop $R1
	Exch $R0 ; return .net install dir full
	Return
 
getdotnetdir_err:
	StrCpy $R0 ""
	Goto getdotnetdir_end
 
FunctionEnd

Issues

It is difficult to know for sure whether calls to external programs, such as RegAsm.exe, in the example above, really succeeded. I tried redirecting the output of RegAsm.exe to a file, and then opening the file to look for the magic words "Types succesfully registered.", with some success. However, if the target machine is an older version of Windows (NT or 95/98/ME) then redirection behaves differently (i.e. not at all, in my recent experience) meaning you can't reliably test that the RegAsm call succeeded.