Introduction
In this article, you'll see that before you start using your .NET applications on other computers, you'll need to know whether that computer has the same .NET Framework version as your application requests to have. I searched the internet and found pieces of this problem, then completed it.
Background
The pieces I found on the net show where to look to check for the .NET version. At this website, it shows to specific Registry locations that are created after a successful .NET Framework installation. And the rest was easy. I looked for a certain module file that reaches through registry and controls it. After that, I worked a little to perfect the app and bingo... Here's how to work it.
Using the Code
After creating a VB6 project, you got to add a module file (for reaching and controlling regkeys) and paste the code below. I didn't write this code, but simply Googled it. Here's the module code:
Public Const REG_NONE As Long = 0
Public Const REG_SZ As Long = 1
Public Const REG_EXPAND_SZ As Long = 2
Public Const REG_BINARY As Long = 3
Public Const REG_DWORD As Long = 4
Public Const REG_LINK As Long = 6
Public Const REG_MULTI_SZ As Long = 7
Public Const REG_RESOURCE_LIST As Long = 8
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Private Const ERROR_NONE = 0
Private Const ERROR_BADDB = 1
Private Const ERROR_BADKEY = 2
Private Const ERROR_CANTOPEN = 3
Private Const ERROR_CANTREAD = 4
Private Const ERROR_CANTWRITE = 5
Private Const ERROR_OUTOFMEMORY = 6
Private Const ERROR_INVALID_PARAMETER = 7
Private Const ERROR_ACCESS_DENIED = 8
Private Const ERROR_INVALID_PARAMETERS = 87
Private Const ERROR_NO_MORE_ITEMS = 259
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (
ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (
ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long,
ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (
ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long,
lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (
ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String,
lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long,
ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (
ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (
ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long,
ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (
ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long,
ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (
ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String,
ByVal cbName As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (
ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (
ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String,
ByVal Key As String) As String
Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long,
sValue As String, td As Double
On Error Resume Next
lResult = RegOpenKey(Group, Section, lKeyValue)
sValue = Space$(2048)
lValueLength = Len(sValue)
lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)
If (lResult = 0) And (Err.Number = 0) Then
If lDataTypeValue = REG_DWORD Then
td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) +
&H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
sValue = Format$(td, "000")
End If
If lDataTypeValue = REG_BINARY Then
TStr2 = ""
For i = 1 To lValueLength
TStr1 = Hex(Asc(Mid(sValue, i, 1)))
If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
TStr2 = TStr2 + TStr1
Next
sValue = TStr2
Else
sValue = Left$(sValue, lValueLength - 1)
End If
Else
sValue = "Not Found"
End If
lResult = RegCloseKey(lKeyValue)
ReadRegistry = sValue
End Function
After that, you return to your project's main form and use this module's working function.
If you want to check for .NET v2.0 installation, use this function's return value.
ReadRegistry(HKEY_LOCAL_MACHINE,
"SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727\", "Install")
If you want to check for .NET v3.0 installation, use this function's return value.
ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\",
"Install")
If you want to check for .NET v3.5 installation, use this function's return value.
ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\",
"Install")
If you want to check for .NET v4.0 installation, use this function's return value.
ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client\",
"Install")
Four of the codes for checking the versions of .NET return string
values either "001
" or "Not Installed
". If .NET exists, it returns "001
" value, if not, you know the rest... Have fun...
History
- 24th November, 2010: Initial post