Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Allowing VFP to determine installed .NET Frameworks
Message
From
26/12/2013 07:34:09
Emerson Reed
Folhamatic Tecnologia Em Sistemas
Americana - São Paulo, Brazil
 
General information
Forum:
Visual FoxPro
Category:
Installation, Setup and Configuration
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP3
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01590916
Message ID:
01590991
Views:
101
Likes (2)
Hi Tommy.
Here's the code that I wrote some time ago to check the .Net Framework versions installed and to register an assembly. Hope it helps.
* RegAsm
* http://www.geeksengine.com/article/register-dll.html
* http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.setup/2008-04/msg00002.html
* http://msdn.microsoft.com/en-us/library/k0588yw5.aspx
*
* .Net Framework
* http://support.microsoft.com/kb/318785
* http://msdn.microsoft.com/en-us/library/aa480173.aspx
* http://stackoverflow.com/questions/198931/how-do-i-tell-if-net-3-5-sp1-is-installed
* http://www.west-wind.com/weblog/posts/2005/Mar/07/Finding-the-NET-Framework-Path
*
* Registry
* http://support.microsoft.com/kb/258161/en-us
*

Clear
Local lcAssembly, lcParameters
m.lcAssembly = "C:\MyAssembly.dll"
m.lcParameters = "/tlb"

Local loDotNetFramework
m.loDotNetFramework = Createobject("DotNetFramework")
m.loDotNetFramework.Versions.ListInScreen()
*!*	With m.loDotNetFramework.Versions("v20")
*!*	   If .Installed
*!*	      If .RegisterAssembly(m.lcAssembly,m.lcParameters)
*!*	         ? "Assembly successfully registered."
*!*	         If .UnregisterAssembly(m.lcAssembly)
*!*	            ? "Assembly successfully unregistered."
*!*	         Else
*!*	            * Error: Error registering Assembly.
*!*	            ? "Error unregistering Assembly."
*!*	         Endif
*!*	      Else
*!*	         * Error: Error registering Assembly.
*!*	         ? "Error registering Assembly."
*!*	      Endif
*!*	   Else
*!*	      * Error: Framework not installed.
*!*	   Endif
*!*	Endwith
m.loDotNetFramework = Null

Return

Define Class DotNetFramework As Custom
   Versions = Null

   Procedure Init
      *
      Local lcBaseClass
      m.lcBaseClass = "DotNetFrameworkVersion"
      *
      Local laDotNetFramework[7]
      m.laDotNetFramework[1] = "v4_Full"
      m.laDotNetFramework[2] = "v4_Client"
      m.laDotNetFramework[3] = "v35"
      m.laDotNetFramework[4] = "v30"
      m.laDotNetFramework[5] = "v20"
      m.laDotNetFramework[6] = "v11"
      m.laDotNetFramework[7] = "v10"
      *
      This.Versions = Createobject("DotNetFrameworkVersions")
      *
      Local lnItem, lcClassName
      For m.lnItem=1 To Alen(m.laDotNetFramework)
         m.lcClassName = m.lcBaseClass+"_"+m.laDotNetFramework[m.lnItem]
         This.Versions.Add(Createobject(m.lcClassName),m.laDotNetFramework[m.lnItem])
      Endfor
      *
      Local lcRegAsmFolder, lcRegAsm
      For m.lnItem=1 To Alen(m.laDotNetFramework)
         m.lcRegAsmFolder = ""
         With This.Versions(m.laDotNetFramework[m.lnItem])
            If .Installed
               If Empty(.RegAsmVersion)
                  m.lcRegAsmFolder = .InstallPath
               Else
                  m.lcRegAsmFolder = This.Versions(.RegAsmVersion).InstallPath
               Endif
               If Not Empty(m.lcRegAsmFolder)
                  m.lcRegAsm = Addbs(m.lcRegAsmFolder) + "RegAsm.exe"
                  If File(m.lcRegAsm)
                     .RegAsm = m.lcRegAsm
                  Endif
               Endif
            Endif
         Endwith
      Endfor
      *
   Endproc

   Procedure Destroy
      This.Versions = Null
   Endproc
Enddefine

Define Class DotNetFrameworkVersions As Collection
   Procedure ListInScreen
      Local lnDNF
      For m.lnDNF=1 To This.Count
         With This.Item(m.lnDNF)
            ? .Description, .Installed, .InstallPath, .Version, .ServicePack, .RegAsm
         Endwith
      Endfor
   Endproc

   Procedure Destroy
      Do While This.Count>0
         This.Remove(1)
      Enddo
   Endproc
Enddefine

Define Class DotNetFrameworkVersion As Custom
   Description = ""
   SubKey = ""
   AlternativeSubKey = ""
   InstallPathSearchMask = ""
   RegAsmVersion = ""

   Installed = .F.
   InstallPath = ""
   RegAsm = ""
   ServicePack = 0
   Version = ""

   Procedure Init
      #Define HKEY_LOCAL_MACHINE -2147483646 && 0x80000002

      Local loReg, lcValue
      m.loReg = Newobject("Registry",Home()+"FFC\REGISTRY")

      Local lcSubKey, lcValueName, lnValue
      m.lcSubKey = This.SubKey
      If Not m.loReg.IsKey(m.lcSubKey, HKEY_LOCAL_MACHINE)
         If Not Empty(This.AlternativeSubKey)
            m.lcSubKey = This.AlternativeSubKey
         Endif
      Endif

      m.lcValueName = "Install"
      m.lnValue = 0
      If ReadRegDWORD(HKEY_LOCAL_MACHINE, m.lcSubKey, m.lcValueName, @m.lnValue)
         This.Installed = (m.lnValue==1)
      Else
         * Error: Error reading registry key.
      Endif

      m.lcValueName = "SP"
      m.lnValue = 0
      If ReadRegDWORD(HKEY_LOCAL_MACHINE, m.lcSubKey, m.lcValueName, @m.lnValue)
         This.ServicePack = Int(m.lnValue)
      Else
         * Error: Error reading registry key.
      Endif

      m.lcValueName = "Version"
      m.lcValue = ""
      If m.loReg.GetRegKey(m.lcValueName,@m.lcValue,m.lcSubKey,HKEY_LOCAL_MACHINE)==0
         This.Version = m.lcValue
      Else
         * Error: Error reading registry key.
      Endif

      Local lcFrameworkFolder
      m.lcFrameworkFolder = Addbs(Getenv("windir"))+"Microsoft.NET\Framework"

      m.lcValueName = "InstallPath"
      m.lcValue = ""
      If m.loReg.GetRegKey(m.lcValueName,@m.lcValue,m.lcSubKey,HKEY_LOCAL_MACHINE)==0
         This.InstallPath = m.lcValue
      Else
         If This.Installed
            Local laFolders[1]
            If Adir(m.laFolders,Addbs(m.lcFrameworkFolder)+This.InstallPathSearchMask,"D") > 0
               This.InstallPath = Addbs(m.lcFrameworkFolder)+Lower(m.laFolders[Alen(m.laFolders,1)])
            Endif
         Endif
         If Empty(This.InstallPath)
            * Error: Error reading registry key.
         Endif
      Endif

      m.loReg = Null
   Endproc

   Procedure Version_Assign
      Lparameters vNewVal
      If Not This.Installed
         If Not Empty(m.vNewVal)
            This.Installed = .T.
            This.ServicePack = Int(Val(Right(m.vNewVal,1)))
         Endif
      Endif
      This.Version = m.vNewVal
   Endproc

   Function RegisterAssembly
      Lparameters lcAssemblyName, lcParameters
      Local llReturn, loWSH As "WScript.Shell"
      m.loWSH = Createobject("WScript.Shell")
      If Not Empty(Alltrim(m.lcParameters))
         m.lcParameters = " " + Alltrim(m.lcParameters)
      Endif
      m.llReturn = (m.loWSH.Run(This.RegAsm + " " + m.lcAssemblyName + m.lcParameters,1,.T.)==0)
      Store Null To m.loWSH
      Return m.llReturn
   Endfunc

   Function UnregisterAssembly
      Lparameters lcAssemblyName
      Return This.RegisterAssembly(m.lcAssemblyName,"/u")
   Endfunc
Enddefine

Define Class DotNetFrameworkVersion_v4_Full As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 4 Client"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client"
   InstallPathSearchMask = "v4*"
Enddefine

Define Class DotNetFrameworkVersion_v4_Client As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 4 Full"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"
   InstallPathSearchMask = "v4*"
Enddefine

Define Class DotNetFrameworkVersion_v35 As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 3.5"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"
   InstallPathSearchMask = "v3.5*"
   RegAsmVersion = "v20"
Enddefine

Define Class DotNetFrameworkVersion_v30 As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 3.0"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0"
   InstallPathSearchMask = "v3.0*"
   RegAsmVersion = "v20"
Enddefine

Define Class DotNetFrameworkVersion_v20 As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 2.0"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727"
   InstallPathSearchMask = "v2.0*"
Enddefine

Define Class DotNetFrameworkVersion_v11 As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 1.1"
   SubKey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322"
   InstallPathSearchMask = "v1.1*"
   RegAsmVersion = "v20"
Enddefine

Define Class DotNetFrameworkVersion_v10 As DotNetFrameworkVersion
   Description = "Microsoft .NET Framework 1.0"
   SubKey = "Software\Microsoft\Active Setup\Installed Components\{78705f0d-e8db-4b2d-8193-982bdda15ecd}"
   AlternativeSubKey = "Software\Microsoft\Active Setup\Installed Components\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}" && XP Media Center and Tablet PC
   InstallPathSearchMask = "v1.0*"
   RegAsmVersion = "v20"
Enddefine

Function ReadRegDWORD
   * This function reads a REG_DWORD from the registry. It will return .T.
   * if successful and store the value in nValueRead. If not successful, it
   * will return .F. and nValueRead will contain -1.
   Lparameters  nKey, cSubKey, cValue,  nValueRead
   * nKey The root key to open. It can be any of the constants defined below.
   *#DEFINE HKEY_CLASSES_ROOT           -2147483648
   *#DEFINE HKEY_CURRENT_USER           -2147483647
   *#DEFINE HKEY_LOCAL_MACHINE          -2147483646
   *#DEFINE HKEY_USERS                  -2147483645
   *cSubKey The SubKey to open.
   *cValue The value that is going to be read.
   *nValueRead What was read from the registry

   * Constants that are needed for Registry functions
   #Define REG_DWORD  4  && A 32-bit number.

   * Local variables used
   Local nErrCode          && Error Code returned from Registry functions
   Local nKeyHandle        && Handle to Key that is opened in the Registry
   Local lpdwValueType     && Type of Value that we are looking for.
   Local lpbValue          && The data stored in the value
   Local lpcbValueSize     && Size of the variable
   Local lpdwReserved      && Reserved Must be 0

   * Initialize the variables
   nKeyHandle = 0
   lpdwReserved = 0
   lpdwValueType = REG_DWORD
   lpcbValueSize = 4          && DWORD is 4 bytes
   nValueRead = -1
   lpbValue = Space(4)

   nErrCode = apiRegOpenKey(nKey, cSubKey, @nKeyHandle)
   * If the error code isn't 0, then the key doesn't exist or can't be opened.
   If (nErrCode # 0) Then
      Return .F.
   Endif

   nErrCode=apiRegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
   =apiRegCloseKey(nKeyHandle)
   If (nErrCode # 0) Then
      Return .F.
   Endif

   nValueRead = StrToLong(lpbValue)
   Return .T.
Endfunc

Function StrToLong
   * This function converts a String to a Long
   Lparameters cLongStr
   Local nLoopVar, nRetval

   nRetval = 0
   For nLoopVar = 0 To 24 Step 8
      nRetval = nRetval + (Asc(cLongStr) * (2^nLoopVar))
      cLongStr = Right(cLongStr, Len(cLongStr) - 1)
   Next
   Return nRetval
Endfunc

Function apiRegOpenKey
   Lparameters nHKey, cSubKey, nResult
   Declare Integer RegOpenKey In Win32API As apiRegOpenKey ;
      Integer nHKey, ;
      String @ cSubKey, ;
      Integer @ nResult
   Return apiRegOpenKey(m.nHKey, @m.cSubKey, @m.nResult)
Endfunc

Function apiRegQueryValueEx
   Lparameters nHKey, lpszValueName, dwReserved, lpdwType, lpbData, lpcbData
   Declare Integer RegQueryValueEx In Win32API As apiRegQueryValueEx ;
      Integer nHKey, ;
      String lpszValueName, ;
      Integer dwReserved, ;
      Integer @ lpdwType, ;
      String @ lpbData, ;
      Integer @ lpcbData
   Return apiRegQueryValueEx(m.nHKey, m.lpszValueName, m.dwReserved, @m.lpdwType, @m.lpbData, @m.lpcbData)
Endfunc

Function apiRegCloseKey
   Lparameters nHKey
   Declare Integer RegCloseKey In Win32API As apiRegCloseKey ;
      Integer nHKey
   Return apiRegCloseKey(m.nHKey)
Endfunc
Emerson Santon Reed
"One Developer CAN Make a Difference. A community CAN make a future." - Craig Boyd
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform