Solved

Visual Basic 4 Registry Editor

Posted on 1998-08-14
1
194 Views
Last Modified: 2013-11-25
How can i have visual basic 4 read the registry and grab the information im looking for and input it into a text box? I have tried everything and I can seem to figure it out. Any help would be appreciated. Thanks!

-diesl0w
0
Comment
Question by:diesl0w
1 Comment
 
LVL 2

Accepted Solution

by:
cantrell earned 100 total points
ID: 1468910
Add a module and put this in it (Note: You should be able to just cut and paste this):

'Registry Functions.
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32" 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 ReturnedRegValue As String, lpcbData As Long) As Long

'Security Mask constants
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const STANDARD_RIGHTS_READ = READ_CONTROL
Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

'Predefined Registry Keys used in hKey Argument
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

' Return codes from Registration functions.
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_BADDB = 1009&
Public Const ERROR_BADKEY = 1010&
Public Const ERROR_CANTOPEN = 1011&
Public Const ERROR_CANTREAD = 1012&
Public Const ERROR_CANTWRITE = 1013&
Public Const ERROR_OUTOFMEMORY = 14&
Public Const ERROR_INVALID_PARAMETER = 87&
Public Const ERROR_ACCESS_DENIED = 5&

'Data type Public Constants
Public Const REG_NONE = 0
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_LINK = 6
Public Const REG_MULTI_SZ = 7
Public Const REG_RESOURCE_LIST = 8
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10



Public Function RegistryQueryValue(RegistryKey&, ValueName$, ValueDataType&, Optional KeyName As String) As String

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''                                                                                 '''
''' The function RegistryQueryValue queries the value from an already opened        '''
'''     registry key, returning it as a string, given the below parameters.         '''
''' The passed parameters are:                                                      '''
'''     RegistryKey& - the handle to the already opened key (see the                '''
'''         Registry32.RegistryOpenKey or Registry32.RegistryCreateKey functions).  '''
'''     ValueName$ - the name of the value to be queried. If an empty string is     '''
'''         passed to it, it will grab the "default" value.                         '''
'''     ValueDataType& - the type of value to grab. See the constants in the        '''
'''         Registry32.declarations section for acceptable values to pass.          '''
'''                                                                                 '''
''' The function declaration for RegQueryValueEx was taken from the (Jan96) MSDN    '''
'''     and from WIN32API.TXT. It is declared in the declarations section.          '''
''' The parameters used with RegQueeryValueEx are defined in the (Jan96) MSDN in    '''
'''     \Product Documentation\SDKs\Win32 SDK\Win32 Programmer's Reference\         '''
'''     \Reference\Functions\ReadEventLog to RemoveMenu\RegQueeryValueEx.           '''
'''     RegistryKey& - the handle to the already opened key (see the                '''
'''         Registry32.RegistryOpenKey or Registry32.RegistryCreateKey functions).  '''
'''     ValueName$ - the name of the value to be queried. If an empty string is     '''
'''         passed to it, it will grab the "default" value. The pointer to the      '''
'''         string is passed (VB ByVal for strings).                                '''
'''     0& - Reserved by the API, not used.                                         '''
'''     ValueDataType& - the type of value to grab. See the constants in the        '''
'''         Registry32.declarations section for acceptable values to pass.  The     '''
'''         pointer to the buffer is passed (VB ByRef).                             '''
'''     ReturnedRegValue - the pointer to the buffer that holds the queried data.             '''
'''     lpcbData - the pointer to the size of the buffer pointed to by ReturnedRegValue       '''
'''     lReturn - a constant returned describing the success/failure of the         '''
'''         function. The constants are defined in the declarations section, and    '''
'''         processed by Registry32.gAPIDisplayError.                               '''
'''                                                                                 '''
''' The function has a Select statement according to the type of data held in the   '''
'''     Registry key's value:                                                       '''
'''     REG_SZ and REG_EXPAND_SZ uses the left$ VB function to remove the NULL      '''
'''         character at the end the string.                                        '''
'''     REG_MULTI_SZ returns the first string, fills in the variant variable-size   '''
'''         array if passed. Note that the passed array will have all previous      '''
'''         values lost.                                                            '''
'''     REG_BINARY requires the translation of the unreadable binary data into      '''
'''         readable hex format. The translation is performed by                    '''
'''         Registry32.BinaryInStringToHexInString. Note that VB can't directly     '''
'''         handle binary data.                                                     '''
'''                                                                                 '''
''' The function performs the following tasks in corresponding order:               '''
'''     1) Identify the data type of the value being queried. The acceptable        '''
'''         options are REG_SZ and REG_EXPAND_SZ (NULL terminated string), and      '''
'''         REG_BINARY (binary data without a termination character).               '''
'''     2) Dimension the size of the data buffer. This includes having it take up   '''
'''         the desired memory space by filling it with space characters, and       '''
'''         declaring the size of the buffer (lpcbData). Note that passing a VB     '''
'''         string By Val (see the function declaration for RegQueryValueEx in      '''
'''         Registry32.declarations) passes the pointer to the string, not the      '''
'''         actual string itself.                                                   '''
'''     3) Query the data from the Registry value.                                  '''
'''     4) Process lReturn. For details, see the documentation for                  '''
'''         Registry32.gAPIDisplayError.                                            '''
'''     5) Return the queried value as a string to the calling procedure.           '''
'''                                                                                 '''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Dim ReturnedRegValue As String       'the pointer to the buffer that holds the queried data
Dim lpcbData As Long                 'the pointer to the size of the buffer pointed to by ReturnedRegValue
Dim lReturn As Long                  'the error value returned by the registry function


'''''''''''''''''''''''''''''''''''''''''''
'' Dimension the size of the data buffer ''
'''''''''''''''''''''''''''''''''''''''''''
        lpcbData = 255
        ReturnedRegValue = Space(lpcbData)
       
''''''''''''''''''''''''''''''''''''''''''''
'' Query the data from the Registry value ''
''''''''''''''''''''''''''''''''''''''''''''
        lReturn = RegQueryValueExString(RegistryKey&, ValueName$, 0&, ValueDataType&, ReturnedRegValue, lpcbData)
       
'''''''''''''''''''''
'' Process lReturn ''
'''''''''''''''''''''
        Call modRegistry32.gAPIDisplayError(lReturn, RegistryKey, KeyName)
       
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Return the queried value as a string to the calling procedure ''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
         RegistryQueryValue = Left$(ReturnedRegValue, lpcbData)
 

End Function

Public Sub gAPIDisplayError(Code&, keyhandle As Long, KeyName As String)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''                                                                                 '''
''' The subroutine gAPIDisplayError processes Code&. Code& is returned by a         '''
'''     function calling one of the Win32 API Registry functions, and describes     '''
'''     the end result of the function call. The value returned by Win32 API        '''
'''     Registry functions are constants defined in Registry32.declarations. If the '''
'''     function call was executed sucessfully, it will return ERROR_SUCCESS, which '''
'''     requires no follow up action. If the function call was executed             '''
'''     unsuccessfully, it will return one of the below values. The only follow up  '''
'''     action performed by this subroutine is to issue an error message to the     '''
'''     screen, halting execution until the OK button is pressed.                   '''
'''                                                                                 '''
''' The passed parameters are:                                                      '''
'''     Code& - returned by a function calling one of the Win32 API Registry        '''
'''         functions, describing the end result of the function call.              '''
'''                                                                                 '''
''' The function performs the following tasks in corresponding order:               '''
'''     1) Perform a Select routine on Code&, and issue the respective error        '''
'''         message to the screen.                                                  '''
'''                                                                                 '''
''' Since this module uses the Win32 API, it is only functional on Win95 and WinNT  '''
'''     systems.                                                                    '''
'''                                                                                 '''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Perform a Select routine on Code&, and issue the respective error message ''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Select Case Code&
    Case ERROR_SUCCESS
      Exit Sub
        'Don't return an error message
    Case ERROR_BADDB
        MsgBox "Corrupt Registry Database!", vbCritical, "Registry Error!"
    Case ERROR_BADKEY
        MsgBox "Key name is bad!", vbCritical, "Registry Error!"
    Case ERROR_CANTOPEN
        MsgBox "Cannot Open Key!", vbCritical, "Registry Error!"
    Case ERROR_CANTREAD
        MsgBox "Cannot Read Key!", vbCritical, "Registry Error!"
    Case ERROR_CANTWRITE
        MsgBox "Cannot Write to Registry!" + Chr$(13) + "If you are running Windows NT YOU MUST BE LOGGED IN AS ADMINISTRATOR", vbCritical, "Registry Error!"
    Case ERROR_ACCESS_DENIED
        MsgBox "Access to Registry Denied!" + Chr$(13) + "If you are running Windows NT YOU MUST BE LOGGED IN AS ADMINISTRATOR", vbCritical, "Registry Error!"
    Case ERROR_OUTOFMEMORY
        MsgBox "Out of memory", vbCritical, "Registry Error!"
    Case ERROR_INVALID_PARAMETER
        MsgBox "Invalid Parameter", vbCritical, "Registry Error!"
    Case Else
       
        MsgBox "REGISTRY ERROR: Undefined key error code: " + KeyName
       
   
End Select

frmMain.SetupError = True

End Sub

**************** END OF MODULE *****************

NOW, CREATE A FORM WITH THIS IN IT:
Function Getreginfo()

Dim hKey As Long
Dim KeyToQuery As String
Dim NewValue As String
Dim NameValue As String

 Dim keyhandle As Long          'To hold the KeyHandle number returned by opening the Registry key.
 Dim dataSize As Long           'Size of the Data (string) we are changing (including the terminating /0).
 Dim ReturnCode As Long         'Holds the return code of the process (i.e. if there was an error).
 
 'Put the registry key you want to query in here
  KeyToQuery = "SOFTWARE\MedE America\MedE Remote\Facility Information"
  'Sub key you want to query.
  NameValue = "Serial"
 
 
'Open the Registry Key.
ReturnCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyToQuery, 0, KEY_ALL_ACCESS, keyhandle)
  'If we can't open the key then it's not there
  If ReturnCode <> 0 Then GoTo error
     
     
  'Check for error opening the key.
  If ReturnCode <> 0 Then GoTo error
     
  'Get the value of the Registry key if it's there.
  ReturnedRegValue = RegistryQueryValue(keyhandle, NameValue, REG_SZ, KeyToQuery)
  'Strip the NUL character out of the end of the string.
  ReturnedRegValue = Left$(ReturnedRegValue, Len(ReturnedRegValue) - 1)
 

'Close the Registry Key.
ReturnCode = RegCloseKey(keyhandle)
  'Check for error
  If ReturnCode <> 0 Then GoTo error
 
 
  If Len(ReturnedRegValue) Then      'We found something in the registry
 
  'This is where you put the registry value.
  Text1 = ReturnedRegValue
 
  Exit Function
 
End If


'If there is an error, display what the error was.
error: Call gAPIDisplayError(ReturnCode, hKey, KeyToQuery)


End Function

Let me know if you have any questions.
Cantrell





0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now