Solved

Accessing the registry from VBA!?

Posted on 1998-07-24
2
579 Views
Last Modified: 2012-05-04
hi I tried to access the registry from VBA. I used the reg funktions from the "advapi32.dll".

the code:
Declare Function RegOpenKeyExA Biblio "ADVAPI32" _
        (ByVal hkey As Long; ByVal lpSubKey As String; _
        ByVal ulOptions As Long; ByVal samDesired As Long; phkResult As Long) _
        As Long
Declare Function RegQueryValueExA Biblio "ADVAPI32" _
        ByVal hkey As Long; ByVal lpValueName As String; _
        ByVal  lpReserved As Long; lpType As Long; ByVal lpData As String; _
        phkResult As Long) As Long
Declare Function RegCloseKey Biblio "ADVAPI32" _
        ByVal hkey As Long) As Long

Dim HKEY_LOCAL_MACHINE As Long
Dim KEY_ALL_ACCESS Als Lang
Dim phkErg As Long
Dim TypVar As Long
Dim langVar As Long
Dim Ergebnis2 As String
    'Evaluated from VC++:
    HKEY_LOCAL_MACHINE = -2147483646
    KEY_ALL_ACCESS = 983103

    Erfolg = RegOpenKeyExA(HKEY_LOCAL_MACHINE; "SOFTWARE\HelloWorld\"; _
       0; KEY_ALL_ACCESS; phkErg)              
 
    Erfolg = RegQueryValueExA(phkErg; "Kanal" & I; 0; TypVar; Ergebnis2; langVar)
         
    Erfolg = RegCloseKey(phkErg)                

The key was opened correctly (returns 0). Also the RegQueryValueEx returns fine (returns 0). The langVar (parameter six) is correct returned. The TypVar parameter (parameter four) is correct returned. Only the Value is empty!!!!!!!!!

comments?
0
Comment
Question by:nil092297
2 Comments
 

Author Comment

by:nil092297
ID: 1466588
Edited text of question
0
 
LVL 1

Accepted Solution

by:
sstew earned 350 total points
ID: 1466589
In your code, TypeVar is not defined.

Try the following:

Global Const REG_MULTI_SZ As Long = 7
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const REG_BINARY As Long = 3
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003
Global Const KEY_ALL_ACCESS = &H3F

Function RegQueryStringValue(ByVal hkey As Long, ByVal strValueName As String, strData As String) As Boolean
  Dim lResult As Long
  Dim lValueType As Variant
  Dim strBuf As String
  Dim lDataBufSize As Long
 
  RegQueryStringValue = False
  On Error GoTo 0
  ' Get length/data type
  lValueType = REG_SZ
  lResult = OSRegQueryValueEx(hkey, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
  If lResult = ERROR_SUCCESS Then
    strBuf = String(lDataBufSize, " ")
    lResult = OSRegQueryValueEx(hkey, strValueName, 0&, lValueType, ByVal strBuf, lDataBufSize)
      If lResult = ERROR_SUCCESS Then
        RegQueryStringValue = True
        strData = StripTerminator(strBuf)
      End If
  End If
 
End Function

This code is a refined version of the code contained in Microsoft's Setupkit program on the VB5 disk.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

860 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