Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing the registry from VBA!?

Posted on 1998-07-24
2
Medium Priority
?
586 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

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

Accepted Solution

by:
sstew earned 1400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses

704 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