AutoCAD Civil 3D Customization

AutoCAD Civil 3D Customization

Reply
Active Contributor
laurie.comerford
Posts: 48
Registered: ‎09-08-2006
Message 1 of 4 (580 Views)
Accepted Solution

How to read Local Machine data from registry

580 Views, 3 Replies
04-03-2011 04:02 PM

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
Distinguished Contributor
joshuamodglin
Posts: 147
Registered: ‎08-15-2007
Message 2 of 4 (560 Views)

Re: How to read Local Machine data from registry

04-04-2011 08:43 AM in reply to: laurie.comerford

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
*Expert Elite*
Jeff_M
Posts: 4,298
Registered: ‎07-22-2003
Message 3 of 4 (556 Views)

Re: How to read Local Machine data from registry

04-04-2011 12:40 PM 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
Active Contributor
laurie.comerford
Posts: 48
Registered: ‎09-08-2006
Message 4 of 4 (549 Views)

Re: How to read Local Machine data from registry

04-04-2011 05:36 PM 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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.