How request permission from Vista UAC for writing to registry?

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
The Vista User Account Control seems to deny my VB app from writing to windows registry. How can I request permission from Vista UAC for writing to registry?
 
Last edited:
Just out of interest which areas of the registry are you writing to?

HKEY_LOCAL_MACHINE\SOFTWARE\

It's for the file associations. All this time I had UAC turned off. After recently reinstalling windows I had the UAC turned on again and noticed that my application didn't work.

I posted an example about the subject a year ago:
http://www.xtremedotnettalk.com/showthread.php?t=101236&highlight=file+association

By the way I tried to add this to the post above, but I didn't have permission to edit or answer to the post:
http://www.xtremedotnettalk.com/showthread.php?p=467417
 
This thread on Stack Overflow may be worth reading.

Well seems like one solution is to do the registry writing in another executable which has higher privilege level (right click --> properties --> compatibility --> "run this program as an administrator" --> see attached picture).

I wonder if that setting will hold when the file has been moved from one computer to another.
 

Attachments

A better solution.

I found an example on the subject at CodeProject:
http://www.codeproject.com/KB/vista-security/UAC_Shield_for_Elevation.aspx#xx2860079xx

It's written in C# so I had to translate it to my favorite VB :D

Here's the translation that will hopefully save someone else the trouble:
Make a new form with one button on it and paste in this code:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If VistaSecurity.IsAdmin Then
            Button1.Text = "Already admin"
        Else
            Button1.Text = "Restart as admin"
            VistaSecurity.AddShieldToButton(Button1)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        VistaSecurity.RestartElevated()
    End Sub
Make a new module called VistaSecurity and paste in this code:
Code:
Imports System.Security.Principal

Module VistaSecurity

    'Declare API
    Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    Private Const BCM_FIRST As Int32 = &H1600
    Private Const BCM_SETSHIELD As Int32 = (BCM_FIRST + &HC)

    Public Function IsVistaOrHigher() As Boolean
        Return Environment.OSVersion.Version.Major < 6
    End Function

    ' Checks if the process is elevated
    Public Function IsAdmin() As Boolean
        Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim p As WindowsPrincipal = New WindowsPrincipal(id)
        Return p.IsInRole(WindowsBuiltInRole.Administrator)
    End Function

    ' Add a shield icon to a button
    Public Sub AddShieldToButton(ByRef b As Button)
        b.FlatStyle = FlatStyle.System
        SendMessage(b.Handle, BCM_SETSHIELD, 0, &HFFFFFFFF)
    End Sub

    ' Restart the current process with administrator credentials
    Public Sub RestartElevated()
        Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
        startInfo.UseShellExecute = True
        startInfo.WorkingDirectory = Environment.CurrentDirectory
        startInfo.FileName = Application.ExecutablePath
        startInfo.Verb = "runas"
        Try
            Dim p As Process = Process.Start(startInfo)
        Catch ex As Exception
            Return 'If cancelled, do nothing
        End Try
        Application.Exit()
    End Sub

End Module

The above program checks if it has administration rights - If not then it will add an UAC shield to a button which will restart the app with administration rights.
 
Back
Top