Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to read Local Machine data from registry

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
laurie.comerford
1603 Views, 3 Replies

How to read Local Machine data from registry

Hi,

I'm needing to read a file location value from the Local_Machine section of the registry.

 

On my Win 7 64 bit O/S my code will not return a value for my key, but does return values for an equivalent microsoft key.

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TestRegValue()
        
    End Sub  ' Form1_Load
    Sub TestRegValue()
        Dim sAns As String = ""
        Dim sErr As String = ""

        sAns = RegValue(RegistryHive.LocalMachine, "SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir", sErr)
        If sAns <> "" Then
            MsgBox("Value = " & sAns)
        Else
            MsgBox("This error occurred: " & sErr)

        End If
        sAns = RegValue(RegistryHive.LocalMachine, "SOFTWARE\CADApps\Landscape", "Data File", sErr)
        If sAns <> "" Then
            MsgBox("Value = " & sAns)
        Else
            MsgBox("This error occurred: " & sErr)

        End If
        End
    End Sub
    Public Function RegValue(ByVal Hive As RegistryHive, _
     ByVal Key As String, ByVal ValueName As String, Optional ByRef ErrInfo As String = "") As String

        Dim objParent As RegistryKey = Registry.LocalMachine
        Dim objSubkey As RegistryKey = Nothing
        Dim sAns As String = ""
    
        '  objParent = Registry.LocalMachine
   
        Try
            objSubkey = objParent.OpenSubKey(Key)
            'if can't be found, object is not initialized
            If Not objSubkey Is Nothing Then
                sAns = (objSubkey.GetValue(ValueName))
            End If

        Catch ex As Exception

            ErrInfo = ex.Message
        Finally

            'if no error but value is empty, populate errinfo
            If ErrInfo = "" And sAns = "" Then
                ErrInfo = _
                   "No value found for requested registry key"
            End If
        End Try
        Return sAns
    End Function

'' Snipped code
End Class

 The registry entries seem equivalent as can be seen on the screen grab showing that the 'CADApps\Landscape' key exists.

 

In addition to the above code, slightly modified from code at:

http://www.freevbcode.com/ShowCode.Asp?ID=4487

 

I have tried numerous other code snippets to read this value and all of them will not return a value for the CADApps entry.

 

It this an aspect of the 64 bit O/S ? 

 

The default Security settings for the key seen in the registry "Advanced Security Settings" allow the key to be read.  Changing them to "Full control" has no effect.

 

 

Regards,

 

Laurie Comerford

 

Regards

Laurie Comerford
3 REPLIES 3
Message 2 of 4

Laurie,

 

I would guess that the issue is connected to the code looking in the Wow6432Node when you have your settings stored outside of that node.

 

You are getting to the registry using an Imports of Microsoft.Win32 and thus it will default to the above registry key.

 

HTH

Josh Modglin
Advanced Technologies Solutions Logo
Message 3 of 4
Jeff_M
in reply to: laurie.comerford

In addition to Josh's findings, here's a link to some code (albeit c# not VB) that should help you get the data needed:

 

http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-vers...

Jeff_M, also a frequent Swamper
EESignature
Message 4 of 4
laurie.comerford
in reply to: Jeff_M

Hi Jeff,

 

Thanks.

 

I was easily able to convert the C# code at the Fusion site.

 

A few modifications produced the code below.  Running this code, confirmed Josh's reply (of which I had some vague feeling, but did not realise that when I looked at the registry I was looking at the 64 bit section, while my code was trying to read the 32 bit section.)

 

 

    Sub GetRegistryValue()
        Dim sValue As String = ReadFromLocalMachine("SOFTWARE\CADApps\Landscape", "Data File")
    End Sub

    Private Shared Function ReadFromLocalMachine(ByVal spRegKeyName As String, ByVal spValueName As String) As String
        Dim value64 As String = String.Empty
        Dim value32 As String = String.Empty

        Dim localKey As RegistryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)
        localKey = localKey.OpenSubKey(spRegKeyName)
        If localKey IsNot Nothing Then
            value64 = localKey.GetValue(spValueName).ToString()
        End If
        Dim localKey32 As RegistryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32)
        localKey32 = localKey32.OpenSubKey(spRegKeyName)
        If localKey32 IsNot Nothing Then
            value32 = localKey32.GetValue(spValueName).ToString()
        End If

        Return value64 & " AAA " & value32
    End Function  ' ReadFromLocalMachine 

 

 

 

Regards,

 

Laurie Comerford

Regards

Laurie Comerford

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report