Solved

Accessing the registry from VBA!?

Posted on 1998-07-24
2
578 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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