Results 1 to 5 of 5

Thread: Run As Administrator

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Run As Administrator

    This is not popping up to "Run As Administrator". Here is my code. I do not think this requires a manifest file? Do you see any errors in the code? I also included the manifest file in case you think that is required, that did not pop up the UAC Yes/No message either.
    vb Code:
    1. Dim installString As String = "/i " & Chr(34) & Application.StartupPath & "\setup\MySQLSetup.msi" & Chr(34) & " /qn INSTALLDIR=" &
    2.                      Chr(34) & Application.StartupPath & "\MySQL" & Chr(34) & " PORT=55655 " & "PASSWORD=test" & " SERVICENAME=PlazDB"
    3.                 MessageBox.Show("msiexec.exe" & installString)
    4.  
    5. Dim oProcess As New Process()
    6.                 Dim oStartInfo As New ProcessStartInfo("msiexec.exe", installString)
    7.                 If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista or higher check
    8.                     oProcess.StartInfo.Verb = "runas"
    9.                 End If
    10.                 oStartInfo.UseShellExecute = True
    11.                 oStartInfo.RedirectStandardOutput = False
    12.                 oProcess.StartInfo = oStartInfo
    13.                 oProcess.Start()
    14.                 oProcess.WaitForExit()

    xml Code:
    1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    2. <asmv1:assembly manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2">
    3.     <asmv1:assemblyIdentity name="PlazSalesConfigWizard" version="1.2.0.0" type="win32" processorArchitecture="x86"/>
    4.     <asmv2:trustInfo>
    5.         <asmv2:security>
    6.             <asmv2:requestedPrivileges>
    7.                 <asmv2:requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
    8.             </asmv2:requestedPrivileges>
    9.         </asmv2:security>
    10.     </asmv2:trustInfo>
    11. </asmv1:assembly>
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Run As Administrator

    You don't need a manifest file at all...

    I have posted something like this here....

    Quote Originally Posted by i00 View Post
    I find it generally better to execute only parts of my program with administrator access...

    You will need the NativeMethods file from this archive: https://code.msdn.microsoft.com/wind...ation-39b7606f

    You can then have a button that shows an elevate shield (such as the ones in Windows) if you want by going:

    VB.Net Code:
    1. If NativeMethod.IsProcessElevated = False Then
    2.     btnNext.FlatStyle = FlatStyle.System
    3.     NativeMethod.SendMessage(btnNext.Handle, NativeMethod.BCM_SETSHIELD,
    4.                              0, New IntPtr(1))
    5. End If
    ...You can also check using the NativeMethod.IsProcessElevated to see if it needs elevation ... for eg if we are in XP with an administrator account we wouldn't need to elevate this @ all - just run normally!

    As for you process I start mine like this:

    VB.Net Code:
    1. Dim proc As New ProcessStartInfo
    2. proc.UseShellExecute = True
    3. proc.WorkingDirectory = Environment.CurrentDirectory
    4. proc.FileName = Application.ExecutablePath
    5. proc.Verb = "runas"
    6. proc.Arguments = "/ServiceAction Uninstall" & " /ServiceName " & ServiceItem.Name & " /ServiceDesc " & ServiceItem.Description & " /ServicePath " & ServiceItem.path
    7. Dim AppProcess As New Process
    8. AppProcess.StartInfo = proc
    9. AppProcess.Start()

    Also ... I should probably point out that the .Start() line will throw an exception if the user cancels the UAC prompt etc.

    Kris

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: Run As Administrator

    This is on an install Wizzard control (Wizard.FinishClick) so I am not sure if there is a button or a handle that I can access easily. Do you know why my code was not elevating in the first post (I think it should elevate even without the manifest).


    Also, I like your idea, but how can I elevate the entire app if I need to?
    I am thinking this is what I may need to do:
    vb Code:
    1. ' Elevate the process if it is not run as administrator.
    2. If (Not Me.IsRunAsAdmin) Then
    3. ' Launch itself as administrator
    4. Dim proc As New ProcessStartInfo
    5. proc.UseShellExecute = True
    6. proc.WorkingDirectory = Environment.CurrentDirectory
    7. proc.FileName = Application.ExecutablePath
    8. proc.Verb = "runas"
    9.  
    10.  
    11. Try
    12. Process.Start(proc)
    13. Catch
    14. ' The user refused the elevation.
    15. ' Do nothing and return directly ...
    16. Return
    17. End Try
    18.  
    19.  
    20. Application.Exit() ' Quit itself
    21. Else
    22. MessageBox.Show("The process is running as administrator", "UAC")
    23. End If
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Run As Administrator

    Quote Originally Posted by rex64 View Post
    [COLOR=#333333]Also, I like your idea, but how can I elevate the entire app if I need to?[/HIGHLIGHT]
    There is no way to elevate an existing app as such... open task manager on some versions of windows goto details and you will see that there is a button (on pre windows 8 versions from memory) and there is a button that says "show all tasks from all users" or something ... clicking this closes the task manager and opens an elevated one ... in later versions some MS apps (signed by MS), if opened with administrator access, will not need further elevation to bypass UAC.

    You have to open a new instance of your app (see my other post) and pass a command line to it to tell it what to do or use IPC...

    if it does this at startup just check NativeMethod.IsProcessElevated ... if not elevate the app and close the non-elevated one ... if it is elevated just do your stuff... no need for command line or IPC

    Kris

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: Run As Administrator

    Thanks. I ended up elevating my app on startup by re-launching.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

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