IsDotNetVersionInstalled() - PB4

Share your advanced PureBasic knowledge/code with the community.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

IsDotNetVersionInstalled() - PB4

Post 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
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: IsDotNetVersionInstalled() - PB4

Post 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")
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Sweet! Thanks for taking the time to update it :).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
Last edited by AND51 on Tue Sep 18, 2007 10:17 am, edited 1 time in total.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post 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.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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>


Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Sorry, thought you were hard coded to only check to v2.

apologies
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

no problem...
Did you think that I tried only to check with "v2" instead of "v2.0" ? Was this your 'missing' version?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply