Page 1 of 1

IsDotNetVersionInstalled() - PB4

Posted: Fri Sep 14, 2007 7:33 pm
by Inf0Byt3
Here's a quick procedure to check if a specific .Net version is installed on the PC:

Code: Select all

Procedure.l IsDotNetVersionInstalled(Version.s)

 ;The version string must be in this format:
 ;"1.0" or "1.1" or "2.0", etc.
 ;The framework is allways installed by Windows
 ;in the following folder: %windir%\Microsoft.NET\Framework\(<version>)
 ;This method was recommemded by a MSDN moderator
 
 ;First get Windows Directory
 #CSIDL_WINDOWS = $24
 WinDir.s = Space(270)
 SHGetSpecialFolderLocation_(0,#CSIDL_WINDOWS,@Listptr)
 SHGetPathFromIDList_(Listptr,@WinDir)
 If Right(WinDir,1) <> "\"
  WinDir + "\"
 EndIf
 DotNetDir.s = WinDir + "Microsoft.NET\Framework\"
 If ExamineDirectory(0,DotNetDir,"*.*")  
  While NextDirectoryEntry(0)
   If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
    DirName.s = DirectoryEntryName(0)
    If DirName <> "" And DirName <> "." And DirName <> ".."
     VersionCheck.s = Trim(Mid(DirName,2,3))
     If Version = VersionCheck
      ProcedureReturn 1
     EndIf
    EndIf
   EndIf
  Wend
  FinishDirectory(0)
 EndIf
 ProcedureReturn 0

EndProcedure

;Example
Debug IsDotNetVersionInstalled("1.1") ;Debugs #True if .Net 1.1 is installed

Re: IsDotNetVersionInstalled() - PB4

Posted: Sat Sep 15, 2007 2:11 am
by PB
Based on your code, I made a smaller optimized version, which you can
also pass a null string to the procedure to see if ANY version is installed.

(Although, I haven't tested whether uninstalling all versions of .NET will
leave the "Microsoft.NET" folder intact, meaning this procedure will give
an incorrect result if null is passed).

Code: Select all

Procedure IsDotNetVersionInstalled(ver$)

  windir$=Space(999) : GetWindowsDirectory_(windir$,999)
  dotnetdir$=windir$+"\Microsoft.NET\Framework\"

  If ver$="" And FileSize(dotnetdir$)=-2
    result=1
  ElseIf ExamineDirectory(0,dotnetdir$,"")
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0)=#PB_DirectoryEntry_Directory
        If Left(DirectoryEntryName(0),4)="v"+ver$ : result=1 : Break : EndIf
      EndIf
    Wend
    FinishDirectory(0)
  EndIf

  ProcedureReturn result

EndProcedure

Debug IsDotNetVersionInstalled("") ; Returns 1 if any DotNet is installed.
Debug IsDotNetVersionInstalled("1.0")
Debug IsDotNetVersionInstalled("1.1")
Debug IsDotNetVersionInstalled("2.0")

Posted: Sat Sep 15, 2007 9:21 pm
by Inf0Byt3
Sweet! Thanks for taking the time to update it :).

Posted: Sat Sep 15, 2007 11:24 pm
by AND51
30% faster and more secure:

Code: Select all

Procedure IsDotNetVersionInstalled(ver.s)
	Protected path.s=Space(#MAX_PATH)
	GetWindowsDirectory_(@path, #MAX_PATH)
	path+"\Microsoft.NET\Framework\"
	If Not ver And FileSize(path) = -2
		ProcedureReturn 1
	Else
		Protected dir=ExamineDirectory(#PB_Any, path, "v"+ver+"*")
		If IsDirectory(dir)
			While NextDirectoryEntry(dir)
				If DirectoryEntryType(dir) = #PB_DirectoryEntry_Directory
					If Left(DirectoryEntryName(dir), 4) = "v"+ver
						FinishDirectory(dir)
						ProcedureReturn 1
					EndIf
				EndIf
			Wend
			FinishDirectory(dir)
		EndIf
		ProcedureReturn 0
	EndIf
EndProcedure

Debug IsDotNetVersionInstalled("") ; Returns 1 if any DotNet is installed. 
Debug IsDotNetVersionInstalled("1.0") 
Debug IsDotNetVersionInstalled("1.1") 
Debug IsDotNetVersionInstalled("2.0") 
  • Edit: isDirectory() added, thanks to Shardik
10.000 runs, my code: 11 sec, PB's code: 14,7 sec.
By the way, PB, why don't you protect the variables? The biggest insecurity of your code, however, is that you simply use ExamineDirecory with ID 0...
What, if the user has already a ExamineDirectory(0), then you would disturb his program execution.
So it is better to work with #PB_Any.

Posted: Sun Sep 16, 2007 1:25 am
by PB
> 10.000 runs, my code: 11 sec, PB's code: 14,7 sec

True, but the speed difference isn't going to matter in a real-world check.
Doing a single check (which is what a real app would do) shows no real
difference in both procedures. Both do it in less than 1 second.

> By the way, PB, why don't you protect the variables?

Because it's just examples that I'm posting; not full applications. Certainly
one can protect the variables and so on once they use my examples, but
that's not my job to do for them.

Posted: Tue Sep 18, 2007 10:06 am
by Shardik
AND51 wrote: 30% faster and more secure:
AND51, your example isn't secure at all because it is the only version that produces an error if .NET isn't installed... :wink:
You don't check the return code of

Code: Select all

Protected dir=ExamineDirectory(#PB_Any, path, "v"+ver+"*")
If .NET isn't installed your code crashes with
PureBASIC error message wrote: Line: 9 - #Directory object not initialized.

Posted: Tue Sep 18, 2007 10:12 am
by AND51
> your example isn't secure at all
Oh, you're right. When I wrote "secure", I meant that I protected the variables, etc.
But you're right, I must consider all points, which concern the security.

> produces an error if .NET isn't installed
Argl... It's a very simple thing and I forgot it? Incredible...

Of course, I correct my version. (Posting above edited)

Thank you for your hint! :D

Posted: Wed Sep 19, 2007 2:22 pm
by pdwyer
seems you are missing a version already

on my PC

Code: Select all


C:\WINDOWS\Microsoft.NET\Framework>dir /ad
 Volume in drive C is System
 Volume Serial Number is 202D-A426

 Directory of C:\WINDOWS\Microsoft.NET\Framework

07/28/2007  07:07 PM    <DIR>          .
07/28/2007  07:07 PM    <DIR>          ..
07/28/2007  07:04 PM    <DIR>          v1.0.3705
07/29/2007  02:21 AM    <DIR>          v1.1.4322
07/28/2007  07:40 PM    <DIR>          v2.0.50727
07/28/2007  07:09 PM    <DIR>          v3.0
               0 File(s)              0 bytes
               6 Dir(s)  28,400,381,952 bytes free

C:\WINDOWS\Microsoft.NET\Framework>



Posted: Wed Sep 19, 2007 3:18 pm
by AND51
> seems you are missing a version already
Which version is missing?
I've tested the source on my PC, and he found v1.0, v1.1, v2.0 and v3.0. Morover, if I give en empty "" string to the procedure it also returns 1.

Posted: Thu Sep 20, 2007 1:18 am
by pdwyer
Sorry, thought you were hard coded to only check to v2.

apologies

Posted: Thu Sep 20, 2007 7:47 am
by AND51
no problem...
Did you think that I tried only to check with "v2" instead of "v2.0" ? Was this your 'missing' version?