|
-
Apr 12th, 2017, 04:50 PM
#1
Thread Starter
Fanatic Member
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:
Dim installString As String = "/i " & Chr(34) & Application.StartupPath & "\setup\MySQLSetup.msi" & Chr(34) & " /qn INSTALLDIR=" & Chr(34) & Application.StartupPath & "\MySQL" & Chr(34) & " PORT=55655 " & "PASSWORD=test" & " SERVICENAME=PlazDB" MessageBox.Show("msiexec.exe" & installString) Dim oProcess As New Process() Dim oStartInfo As New ProcessStartInfo("msiexec.exe", installString) If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista or higher check oProcess.StartInfo.Verb = "runas" End If oStartInfo.UseShellExecute = True oStartInfo.RedirectStandardOutput = False oProcess.StartInfo = oStartInfo oProcess.Start() oProcess.WaitForExit()
xml Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <asmv1:assembly manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"> <asmv1:assemblyIdentity name="PlazSalesConfigWizard" version="1.2.0.0" type="win32" processorArchitecture="x86"/> <asmv2:trustInfo> <asmv2:security> <asmv2:requestedPrivileges> <asmv2:requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </asmv2:requestedPrivileges> </asmv2:security> </asmv2:trustInfo> </asmv1:assembly>
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Apr 13th, 2017, 08:44 AM
#2
Re: Run As Administrator
You don't need a manifest file at all...
I have posted something like this here....
 Originally Posted by i00
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:
If NativeMethod.IsProcessElevated = False Then
btnNext.FlatStyle = FlatStyle.System
NativeMethod.SendMessage(btnNext.Handle, NativeMethod.BCM_SETSHIELD,
0, New IntPtr(1))
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:
Dim proc As New ProcessStartInfo
proc.UseShellExecute = True
proc.WorkingDirectory = Environment.CurrentDirectory
proc.FileName = Application.ExecutablePath
proc.Verb = "runas"
proc.Arguments = "/ServiceAction Uninstall" & " /ServiceName " & ServiceItem.Name & " /ServiceDesc " & ServiceItem.Description & " /ServicePath " & ServiceItem.path
Dim AppProcess As New Process
AppProcess.StartInfo = proc
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
-
Apr 13th, 2017, 12:07 PM
#3
Thread Starter
Fanatic Member
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:
' Elevate the process if it is not run as administrator. If (Not Me.IsRunAsAdmin) Then ' Launch itself as administrator Dim proc As New ProcessStartInfo proc.UseShellExecute = True proc.WorkingDirectory = Environment.CurrentDirectory proc.FileName = Application.ExecutablePath proc.Verb = "runas" Try Process.Start(proc) Catch ' The user refused the elevation. ' Do nothing and return directly ... Return End Try Application.Exit() ' Quit itself Else MessageBox.Show("The process is running as administrator", "UAC") End If
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Apr 13th, 2017, 06:53 PM
#4
Re: Run As Administrator
 Originally Posted by rex64
[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
-
Apr 15th, 2017, 01:20 PM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|