Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:35:02+00:00 2026-05-25T20:35:02+00:00

So I am using a user to run the following code that is a

  • 0

So I am using a user to run the following code that is a member of the “User” group on a Windows 7, x64 machine. I am trying to use impersonation (by logging in as a user that is part of the Administrator group) to allow the current user to read from the registry. For some reason the login happens successfully but even though WindowsIdentity.GetCurrent() is returning the user that is part of the Administrator group I am still getting an error message saying “Requested registry access is not allowed”. What am I doing wrong?

This is the main code:

            Dim ra As RunAs = Nothing
            If UserDomain.Length > 0 AndAlso UserName.Length > 0 AndAlso UserPassword.Length > 0 Then
                ra = New RunAs
                ra.ImpersonateStart(UserDomain, UserName, UserPassword)
            End If
            If Not My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting", "DontShowUI", 0) Is Nothing AndAlso _
            My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting", "DontShowUI", 0) = 0 Then
                    My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting", "DontShowUI", 1)
            End If

And suppose my RunAs class is the following:

Public Class RunAs
 Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

    Public Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
      ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
      ByRef DuplicateTokenHandle As IntPtr) As Boolean

    ' Test harness.
    ' If you incorporate this code into a DLL, be sure to demand FullTrust.
    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Public Sub ImpersonateStart(ByVal Domain As String, ByVal userName As String, ByVal Password As String)
            tokenHandle = IntPtr.Zero
            ' Call LogonUser to obtain a handle to an access token.
            Dim returnValue As Boolean = LogonUser(userName, Domain, Password, 2, 0, tokenHandle)

            'check if logon successful
            If returnValue = False Then
                Dim ret As Integer = Marshal.GetLastWin32Error()
                Console.WriteLine("LogonUser failed with error code : {0}", ret)
                Throw New System.ComponentModel.Win32Exception(ret)
                Exit Sub
            End If

            'Logon succeeded

            ' Use the token handle returned by LogonUser.
            Dim newId As New WindowsIdentity(tokenHandle)
            impersonatedUser = newId.Impersonate()
    End Sub
End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:35:03+00:00Added an answer on May 25, 2026 at 8:35 pm

    I agree with @Hans. With UAC, you need to restart the application with UAC privileges, which will cause the UAC prompt to display. A simple way to accomplish this is as follows:

    1. In normal path of the application, when admin privileges are needed, restart the application with UAC request and a command-line flag, like /admin.
    2. On second run of the application, detect the flag /admin, and do the administrative part of the application.
    3. When the second run finishes (#2), if it was successful, then continue application logic from the first pass. If not successful, display error/perform appropriate exception handling logic.

    In our application, I have a helper method called RunElevated that attempts to restart the application with requested administrator privileges, which will cause the UAC prompt to display (I have also included my IsAdmin() helper function):

    Private Function RunElevated(commandLine As String, Optional ByVal timeout As Integer = 0) As Boolean
        Dim startInfo As New ProcessStartInfo
        startInfo.UseShellExecute = True
        startInfo.WorkingDirectory = Environment.CurrentDirectory
        Dim uri As New Uri(Assembly.GetEntryAssembly.GetName.CodeBase)
        startInfo.FileName = uri.LocalPath
        startInfo.Verb = "runas"
        startInfo.Arguments = commandLine
    
        Dim success As Boolean
        Try
            Dim p As Process = Process.Start(startInfo)
            ' wait thirty seconds for completion
            If timeout > 0 Then
                If Not p.WaitForExit(30000) Then
                    ' did not complete in thirty seconds, so kill
                    p.Kill()
                    success = False
                Else
                    success = True
                End If
            Else
                p.WaitForExit()
                success = True
            End If
        Catch ex As Win32Exception
            success = False
        Catch ex As Exception
            MsgBox("Error occurred while trying to start application as administrator: " & ex.Message)
            success = False
        End Try
        Return success
    End Function
    
    Public Function IsAdmin() As Boolean
        Dim id As WindowsIdentity = WindowsIdentity.GetCurrent
        Dim p As New WindowsPrincipal(id)
        Return p.IsInRole(WindowsBuiltInRole.Administrator)
    End Function
    

    To use, I pass a flag and run elevated. In my case, I have a function that sets registry keys, and uses the flag /setregistry to indicate that the instance is started for UAC purposes to just set the registry keys. That code looks something like this:

        Dim success As Boolean
        If Not IsAdmin() Then
            ' try to create the registry keys as administrator
            success = RunElevated("/setregistry", 30000)
            success = success And ValidateKeysSet() ' check if it was successful
            Return success
        End If
    
        ' If we are Admin (Not IsAdmin() = False), then go ahead and set the keys here
    

    Then in the startup logic (Form_Load, since this is a forms application), I check if that flag is present:

        If Command.ToLower.Contains("/setregistry") Then
            ' if application instance is for sole purpose of setting registry keys as admin
            If IsAdmin() Then
                SetRegistryKeys() ' set the keys, since we are admin
            Else
                MsgBox("ERROR: Application must be run as administrator to set registry keys.")
            End If
        Else
            ' Perform normal startup process
        End If
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the following code, to assign user selected text in
I'm trying to run Python scripts using Xcode's User Scripts menu. The issue I'm
The following code is what I'm trying to use to print data to the
I'm currently using a homegrown method to run a process as a different user
I've been using user controls extensively but never use a HttpHandler and was wondering
I am trying to create a trapezoid using user inputted options. I know my
Are use cases just multiple user stories?? What are the benefits of using user
Is it alright to expect that the user using the back end will have
I get the memberOf property for my user using this code: DirectorySearcher search =
We know that compiler generates some member functions for user-defined class if that member

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.