Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a custom Windows setup program, and all has gone well to this point.  I have working code that allows the program being installed to show in the Windows "Add/Remove Programs window.  When I attempt to uninstall the program in the normal way from the Add/Remove Programs window I get an error, "You do not have sufficient access to uninstall [my program name]…. After some digging I concluded I need to grant privileges to the key …/uninstall/[my program name].  I put together some code in an attempt to grant privileges but it does not work.  What am I doing wrong? I have included my code in "obsolete" VB below.  
Thanks for helping me.  

Code:
Private Sub TestKeys()

        Dim registrySearchKey As RegistryKey

        registrySearchKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE") _
                            .OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall")

        For Each ValueName In registrySearchKey.GetSubKeyNames

            If ValueName = "TimeTracker" Then

                Dim registryKeyToAlter As RegistryKey
                registryKeyToAlter = registrySearchKey.OpenSubKey("TimeTracker")

                Try

                    Dim rs As RegistrySecurity = New RegistrySecurity()

                    Dim user As String = Environment.UserDomainName & "\" & Environment.UserName

                    rs.AddAccessRule(New RegistryAccessRule(user, RegistryRights.WriteKey, InheritanceFlags.ContainerInherit,
                                                                  PropagationFlags.InheritOnly, AccessControlType.Allow))

                    registryKeyToAlter.SetAccessControl(rs)

                    MsgBox("Key Permission was set.")

                Catch ex As UnauthorizedAccessException
                    MsgBox("Error: " & ex.Message)
                End Try

            End If

        Next

    End Sub


What I have tried:

I have searched the web for an answer to my problem and so far I have not found an answer. There is little information on the web pertaining to my specific problem. From various sources I cobbled together an attempt to set registry key permissions. I'm about at the "end of the rope" so to speak. So, I'm kind of begging for someone with experience in this area to show me the way.
Posted
Updated 8-Apr-22 7:44am
Comments
[no name] 8-Apr-22 12:30pm    
https://docs.microsoft.com/en-us/dotnet/api/system.configuration.install.installer?view=netframework-4.8

1 solution

Anything under HKEY_LOCAL_MACHINE is not writable by normal users. DO NOT give any users write/modify permissions to anything under HKEY_LOCAL_MACHINE.

What you do and how you do it depends on what your installer is doing and is it running as the user or as LocalSystem.

For normal users running an installation, your installer can NOT write to HKEY_LOCAL_MACHINE and anywhere under "Program Files" or "Program Files (x86)". If your installer is attempting this, it can only be run by LocalSystem or by an Administrator.

For uninstalls, the same rules apply. If it took an Administrator account to install, it'll take an Administrator account to uninstall it too.
 
Share this answer
 
Comments
Duke Mitchell 8-Apr-22 18:59pm    
Thanks for your answer.

Both the installer and uninstaller run with admin rights. I am just trying to do what other uninstallers do. That is to run the uninstaller in the ... uninstall\my application subkey where there is an entry for the uninstaller path data. As best I can recon it is running as a user. I'm not sure what to do to try LocalSystem.
Dave Kreskowiak 8-Apr-22 20:16pm    
Well, that command line has to be launched under an Administrator account, not as a user. When you launch the uninstall from Apps & Features, it's launched as the account that clicked Uninstall. If that account is not an Administrator, your code won't work.

Unless you write up a rather complex tool to do it, you cannot launch anything as LocalSystem.
Duke Mitchell 11-Apr-22 10:49am    
Thanks again. Actually, my program requires admin. rights.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900