Solved

Accessing the registry from VBA!?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 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…
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

21 Experts available now in Live!

Get 1:1 Help Now