Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 21,258 times

Downloads

Related Categories

Retrieve Registry Info

Kimmy

This sample code demonstrates how to read various Values from the Registry. It will retrieve the ComputerName, Internet Explorer Version, Windows Version, ProductID and Serial Number.,Registered Name and Company Name.

You Need: Command Buttons 1 to 5

' In a Module1.bas
' Registry
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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
Declare Function RegQueryValueExString 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
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long

Global Const KEY_QUERY_VALUE = &H1
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003

Public Function GetRegStringValue$(Where, sKeyName$, sValueName$)
   Dim lRetVal As Long         'result of the API functions
   Dim hKey As Long         'handle of opened key

   lRetVal = RegOpenKeyEx(Where, sKeyName$, 0, KEY_QUERY_VALUE, hKey)

   Dim cch As Long
   Dim lrc As Long
   Dim lType As Long
   Dim sValue As String

   lrc = RegQueryValueExNULL(hKey, sValueName, 0&, lType, 0&, cch)
   GetRegStringValue$ = Space$(cch)
   lrc = RegQueryValueExString(hKey, sValueName, 0&, lType, GetRegStringValue$, cch)
   If lrc = 0 Then GetRegStringValue$ = Left$(GetRegStringValue$, cch - 1)

   RegCloseKey hKey
End Function


' In a Form
Private Sub Command1_Click()
   Where = HKEY_LOCAL_MACHINE
   sKeyName$ = "System\CurrentControlSet\Control\ComputerName\ComputerName"
   sValueName$ = "ComputerName"
   
   MsgBox GetRegStringValue(Where, sKeyName$, sValueName$)
End Sub

Private Sub Command2_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOwner") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOrganization")
End Sub

Private Sub Command3_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductName") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "Version") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "VersionNumber") + Chr$(10) + Chr$(10) + _
   "   Product ID: " + GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductID") + Chr$(10) + _
   "Product S/N: " + GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductKey") + Chr$(10)
End Sub


Private Sub Command4_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Hardware\Description\System\CentralProcessor\0", "Identifier") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Hardware\Description\System\CentralProcessor\0", "VendorIdentifier")

End Sub

Private Sub Command5_Click()
   MsgBox GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Internet Explorer", "Version")
End Sub

Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Comments

  • Comments

    Posted by Mike733 on 20 May 2002

    Nice. Elegantly simple and concise. I learned a lot. I’m still learning from it.

    I would have liked a bunch more comments, but maybe that is what the rest of us are for, so here are mine for the...