Hi Dw.

I'm tryign to change a registrykey on my system but I get this error: ArgumentException was unhandled, The specified RegistryKeyPermissionCheck Value is invalid. Parameter name: mode

here is the line that throws this error:

regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Root & Last, True, (Security.AccessControl.RegistryRights.FullControl))

and the rest of the function code is:

Private Function GetRoot(ByVal Adapter As String) As String
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim i As Integer = 0

        Do
            Dim Root As String = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
            Dim Last As String = DoPadding(i)
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Root & Last, True, (Security.AccessControl.RegistryRights.FullControl))

            Try
                Dim cAdapter As String = regKey.GetValue("DriverDesc").ToString()
                If cAdapter = Adapter Then
                    Return Root & Last
                End If
            Catch
                Exit Do
            End Try
            i += 1
        Loop
End Function

This funtion is called after clicking the update button to update the mac address.

Private Sub bt_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_update.Click
        Dim mac_text As String = m1.Text.ToUpper + ":" & m2.Text.ToUpper + ":" & m3.Text.ToUpper + ".:" & m4.Text.ToUpper + ":" & m5.Text.ToUpper + ":" & m6.Text.ToUpper
        If IsOkay() = False Then
            Exit Sub
        End If
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim Addr As String = GetRoot(combo_network.SelectedItem.ToString())
        regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Addr, True)

        regKey.SetValue("NetworkAddress", mac_text.ToString.Replace(":", ""))
        ShowRestart()
    End Sub

I've tried changing all the available access/modificication permissions that are available within that line but still it just throw this error message.

Thank you

Dani AI

Generated

Short answer: the ArgumentException comes from calling the wrong overload of OpenSubKey. The three-argument overload expects a RegistryKeyPermissionCheck enum as the second parameter (not a Boolean), so passing True there produces the "mode" error. As hinted, use the simple writable overload when only reading/writing values, or pass the proper enum when you need explicit rights.

Example fixes (VB):

' Use the boolean overload (simple and usually sufficient)
Dim rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, writable:=True)
If rk IsNot Nothing Then
    rk.SetValue("NetworkAddress", macNoSeparators, Microsoft.Win32.RegistryValueKind.String)
    rk.Close()
End If
' Or use the three-argument overload correctly
Dim rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path,
        Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree,
        System.Security.AccessControl.RegistryRights.SetValue)

Why changes might still not stick (addresses 's later problem): writing to HKLM requires elevation (process must run as Administrator), and on 64-bit Windows a 32-bit process is redirected to the WOW6432Node view. If the working program is 64-bit (or writes to the 64-bit view) but this app is 32-bit, the driver will never see the changes. To target the correct registry view use OpenBaseKey with RegistryView.Registry64 (or compile the app for x64). Also verify the key object is not Nothing before SetValue, use Using/Close to flush, and ensure the NetworkAddress string is exactly 12 hex characters (no separators). After writing most NICs only pick up the value when the driver reloads — disable/enable the adapter or restart the machine.

Troubleshooting checklist

  • Fix the overload (boolean OR RegistryKeyPermissionCheck.ReadWriteSubTree).
  • Run elevated or add requireAdministrator manifest.
  • Target the correct registry view (RegistryView.Registry64 if needed).
  • Confirm the actual registry hive with Regedit (64-bit Regedit on 64-bit OS).
  • Log/check for rk IsNot Nothing and exceptions; use Process Monitor if unsure which process/key is being written.

Recommended Answers

All 2 Replies

Hi

I tried out your code and was able to read a value from that section of the registry, but not DriverDesc as I only have ClassDesc there. The only argument I needed to change was the writeable property which I set to False as it seems that you are only attempting to read the value:

regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Root & Last, False, (Security.AccessControl.RegistryRights.FullControl))

HTH

Thanks. Sorry to take so long but I was busy with other projects. Well now my problem is that even if I've set the values but they are not being saved to the registry.

I have another working program I've downloaded which does change my MAC address but when I'm using mine, the above code (btn_update) the keys/values are not being saved to the registry even after I've disabled and enabled the adapter but still my this code doesn't update the registry.

Is there any thing I'm missing out here or perhaps is there any better way to write to the registry?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.