CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Exclamation Windows 7 and Reg Functions..

    i have two very simple functions... to load and save regkeys..
    Code:
           Public Function Load(ByVal Regloc As Reg_type, ByVal Keyloc As String, ByVal Keyname As String, Optional ByVal DefValue As String = "NA") As Regkeys
                Dim TmpReg As New Regkeys
                Dim TmpVal As String
                TmpVal = DefValue
                Try
                    Select Case Regloc
                        Case Reg_type.Current_Config
                            rk = Registry.CurrentConfig.OpenSubKey(Keyloc, False)
                        Case Reg_type.Current_User
                            rk = Registry.CurrentUser.OpenSubKey(Keyloc, False)
    
                        Case Reg_type.Local_Machine
                            rk = Registry.LocalMachine.OpenSubKey(Keyloc, False)
    
                        Case Reg_type.Users
                            rk = Registry.Users.OpenSubKey(Keyloc, False)
                    End Select
                    TmpVal = DirectCast(rk.GetValue(Keyname, DefValue), String)
                Catch ex As Exception
                    RaiseEvent Int_Error(Error_type.Load_Err)
                Finally
                    TmpReg.LoadReg(Regloc, Keyloc, Keyname, TmpVal)
                    TmpReg.Changed = True
                    Try
                        rk.Close()
                    Catch ex As Exception
                    End Try
                    End Try
                    Save(TmpReg)
                    Return TmpReg
            End Function
    
            Public Sub Save(ByRef RegKey As Regkeys)
                If RegKey.Change Then
                    Try
                        Select Case RegKey.RegType
                            Case Reg_type.Current_Config
                                rk = Registry.CurrentConfig.CreateSubKey(RegKey.KeyLoc)
                            Case Reg_type.Current_User
                                rk = Registry.CurrentUser.CreateSubKey(RegKey.KeyLoc)
    
                            Case Reg_type.Local_Machine
                                rk = Registry.LocalMachine.CreateSubKey(RegKey.KeyLoc)
    
                            Case Reg_type.Users
                                rk = Registry.Users.CreateSubKey(RegKey.KeyLoc)
                        End Select
                        rk.SetValue(RegKey.Keyname, RegKey.KeyVal)
                        RegKey.Changed = False
                    Catch ex As Exception
                        RaiseEvent Int_Error(Error_type.Save_Err)
                    Finally
                        rk.Close()
                    End Try
                End If
            End Sub
    these functions work fine in XP and vista, but when tested in Windows 7 .. the save reg function crashes... we assume it may be to do with Windows 7 having changed access to the reg... any ideas ???

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Windows 7 and Reg Functions..

    I haven't tried on windows 7, but my guess is it will be in the "Local Machine" or the "Users". I would seriouly take a look at avoiding the registry all together. Check out the Application Settings. You can set for "Application" or "User" settings. So Application settings would be for things like Database Server name... and User settings would be color/email address/... settings.

    These are stored as files in the user profile.

  3. #3
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Windows 7 and Reg Functions..

    I just fired up what you got, and I'm on Win7(64-bit).

    What type is RegKeys?

    The load function and TmpReg are both of that type.
    I've not seen that type in vb.net.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Windows 7 and Reg Functions..

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Windows 7 and Reg Functions..

    Oh yeah, I use the same registry types, in the same way as were in the link.
    Regkeys was not one of them though.

    I'm thinking either, he has a custom Regkeys, or is missing an import that I've never seen.
    Put the code into a blank project, and you'll see what I mean.


    Particularly these are the lines I've never seen used:
    Code:
                   TmpReg.LoadReg(Regloc, Keyloc, Keyname, TmpVal)
                    TmpReg.Changed = True
    For example this would be the closest type I know of to RegKeys:
    Code:
    Dim TmpReg As New Microsoft.Win32.RegistryKey
    Which does not show .LoadReg() or .Changed, for vb05, as he has above.



    ----------------
    Last edited by TT(n); August 3rd, 2009 at 10:24 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Windows 7 and Reg Functions..

    Might be his own class (looks like the API)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Windows 7 and Reg Functions..

    Sorry ... regkeys is a custom type that i built ..
    Code:
            Public Class Regkeys
                Private _RegType As Reg_type
                Private _KeyLoc As String
                Private _KeyName As String
                Private _KeyVal As String
                Private _Change As Boolean
    
                Public ReadOnly Property RegType() As Reg_type
                    Get
                        Return _RegType
                    End Get
                End Property
    
                Public ReadOnly Property KeyLoc() As String
                    Get
                        Return _KeyLoc
                    End Get
                End Property
    
                Public ReadOnly Property Keyname() As String
                    Get
                        Return _KeyName
                    End Get
                End Property
    
                Public Property KeyVal() As String
                    Get
                        Return _KeyVal
                    End Get
                    Set(ByVal value As String)
                        _KeyVal = value
                        _Change = True
                    End Set
                End Property
    
                Public ReadOnly Property Change() As Boolean
                    Get
                        Return _Change
                    End Get
                End Property
    
                Protected Friend WriteOnly Property Changed() As Boolean
                    Set(ByVal value As Boolean)
                        _Change = value
                    End Set
                End Property
    
                Protected Friend Sub LoadReg(ByVal RegType As Reg_type, ByVal KeyLoc As String, ByVal Keyname As String, ByVal Keyval As String)
                    _RegType = RegType
                    _KeyLoc = KeyLoc
                    _KeyName = Keyname
                    _KeyVal = Keyval
                    _Change = False
                End Sub
            End Class
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Windows 7 and Reg Functions..

    Well if it worked before, then could it be a permission and/or ownership issue?

    I have had a problem with the default security configuration of Win 7.
    When it's first installed, the TrustedInstaller has ownership and full control over certain registry keys, and possibly sub keys.
    Administrators do not have full access yet, until you gain ownership of the key. Then you can give yourself full access.

    Manually if you are logged into an adminstrator/user account.
    Open regedit
    Right click a registry key
    Choose Persmission from the menu
    Click the Advanced button below
    Click the Owner tab
    Choose Administrators as the owner and Apply, OK
    Click checkbox for Full Control, with Administrators selected.
    Apply Ok


    I've got a clean automation routine for that using my Sendkeys.
    It's the best solution I've found yet.
    Stable on Vista and Win7 (32-64 bit)
    No other solutions i found, can do this with the UAC security on.
    Even microsofts SubInACL.exe fails to gain ownership on Win7 64 bit.

    You can slow the process down too, so that the user is asked at each step with a messagebox, or text to speech.
    That way you blend it right into the built in security of the OS without appearing to be violative.
    Would you like the administrator(yourself) to gain ownership of this registry key?
    Would you like the administrator to gain full control over this registry key?
    It could be even more verbose, and explain each window, and how one would repeat the operation manually.





    __
    Last edited by TT(n); August 5th, 2009 at 02:12 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Windows 7 and Reg Functions..

    TrustedInstaller is a service... http://support.microsoft.com/kb/968440/EN-US

    Using Powershell:

    Code:
    [System.Enum]::GetNames([System.Security.AccessControl.RegistryRights])
    [QUOTE]QueryValues
    SetValue
    CreateSubKey
    EnumerateSubKeys
    Notify
    Create
    Link
    Delete
    Read
    Permissions
    WriteKey
    ExecuteKey
    ReadKey
    ChangePermissions
    TakeOwnership
    FullControl
    QUOTE]


    Taking Ownership


    Make sure that you are the "owner" of the key before modifying key permissions as a test. Only if you are the owner you will be able to undo possible mistakes. This is how to take ownership of a registry key (to the extent that your permissions allow it):

    Code:
    $acl = Get-Acl HKCU:\Software\Testkey
    $acl.Owner
    scriptinternals\TobiasWeltner
    Code:
    $me = [System.Security.Principal.NTAccount]"$env:userdomain\$env:username"
    $acl.SetOwner($me)
    Last edited by dglienna; August 5th, 2009 at 08:40 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured