Share via


How to set "Run as administrator" property to any exe thru code

Question

Monday, November 4, 2013 6:59 AM

Hi everyone,

I am trying to set "Run as administrator" flag for a third party application thru my VB.Net application, and needs help in this regard. Can anyone please help.

Thanks & Regards,

Maverick

All replies (13)

Monday, November 4, 2013 10:50 AM ✅Answered

Thank you all,

I understand that "

startInfo.Verb = "runas"

would launch the 3rd party application with administrator right thru my application, but in my case if the user makes a desktop shortcut of that using my application interface. and try to launch the application from shortcut, the application would be launch under standard privileges therefore what I want is to set the 3rd party application's property to "Run as administrator" thru code. so that even if the user launches that application from desktop shortcut, the application still runs under administrative privileges and the user don't need to manually interact with file's property.

I hope, I clarify my requirement and scenario.Please don't hesitate to ask in case of any confusion.

Thanks & Regards,

Maverick

You've already clarified your requirement which you did not need to do again. As it is not possible to do so since Windows security will not allow that to happen. If the user does not have administrative privileges they will never be able to launch an app on their desktop with admin privileges without getting a UAC prompt and filling in the required user name and password that does have admin privileges. And if they do have admin privileges the only way to launch a program without a UAC prompt is to use task scheduler to allow the app to launch without a UAC prompt. Even if a user with admin privileges right clicks on an applications shortcut and sets it to always run with admin privileges, before it will actually appear a UAC prompt will ask if it is okay for the app to launch with admin privileges. Unless you have configured task scheduler, WHICH YOU CAN NOT DO THROUGH CODE, to allow it to happen without a UAC prompt.

I can be no clearer than that in my explanation to you.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, November 4, 2013 12:52 PM ✅Answered | 1 vote

You do that thru the registry

"HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"

"HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"

This shows the possible settings

http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/

Luc


Monday, November 4, 2013 1:19 PM ✅Answered

Haroon,

Would it not be crazy if Microsoft did spent so much money to create software which needs a special user action to run it as Administrator and in the same time then gives a possibility for those who would create malicious code to make it senseless.

For those who have not seen this link not yet

http://social.msdn.microsoft.com/Forums/vstudio/en-US/053a0a87-e9b8-42e7-b483-d6d47c41278e/contributors-how-to-avoid-aiding-the-development-of-malicious-code?forum=vbgeneral

If it is for software in your company then let your administrator install it in a way that it runs always as administrator

Success
Cor


Monday, November 4, 2013 7:40 AM | 1 vote

Hi Haroon,

did you try it already with the manifest-file described here:

http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7

Thomas Claudius Huber

"If you can´t make your app run faster, make it at least look & feel extremly fast"

twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


Monday, November 4, 2013 7:57 AM

Dear Thomas Claudius Huber,

Thank you for such prompt reply. Actually I need to force third party application to run as admin, i don't have the source code for the application.

Thanks & Regards,

Maverick.


Monday, November 4, 2013 8:20 AM | 1 vote

is it a 3rd party app, which will be started by your app?

the Process class has a .StartInfo.Verb property where you can specify "runas"

thanks for any help


Monday, November 4, 2013 8:31 AM | 1 vote

Hi everyone,

I am trying to set "Run as administrator" flag for a third party application thru my VB.Net application, and needs help in this regard. Can anyone please help.

Thanks & Regards,

Maverick

If your program is running with admin privileges then using this code it can launch another app at that level. You just need the apps path and filename unless it's registered well enough to run with just the name like notepad is. However just because you are logged on with admin privileges does not mean your app is running with those privileges. Unless your manifest requested that level on load, in which case you would have received a UAC prompt, then perhaps your app is running with admin privileges. Or unless you correctly registered your program with task scheduler to run with highest privileges for the user logged ons privilege level so a user logged on with admin privileges, if they launch your app, it will run with admin privileges.

Anyhow here's the code and when I run it in Visual Studio without Visual Studio running elevated then I get a UAC prompt to run notepad with admin privileges. If VS is running elevated then notepad runs with admin privileges without a UAC prompt occuring.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim startInfo As New ProcessStartInfo()
        Dim myprocess As New Process()
        startInfo.FileName = "Notepad"
        startInfo.Verb = "runas"
        startInfo.Arguments = "/env /user:" + "Administrator" + " cmd"
        myprocess.StartInfo = startInfo
        myprocess.Start()
End Sub

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, November 4, 2013 8:42 AM

Actually in that code I provided it was originally launching a command prompt to run as admin so the letters " cmd" next to "Administrator" + may need to be changed to whatever your apps .exe name is. Apparently Notepad will not run as admin so I'm going to attempt to use it to launch another Visual Studio with admin privileges and then I'll post if that " cmd" word had to be changed in order to do it.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, November 4, 2013 8:47 AM | 1 vote

Well that entire line was switch statements for the command prompt. This code worked to get the UAC prompt to appear for launching Visual Studio with admin privileges. And once I clicked OKAY then VS launched running with admin privileges.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim startInfo As New ProcessStartInfo()
        Dim myprocess As New Process()
        startInfo.FileName = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"
        startInfo.Verb = "runas"
        myprocess.StartInfo = startInfo
        myprocess.Start()
End Sub

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, November 4, 2013 8:57 AM

I don't know what you mean by a "flag" but there's nothing you can set programatically that will elevate a program to run with admin privileges without a UAC prompt unless the program has been added to task scheduler to do so and the user attempting to run the program is logged on with admin privileges. As that would be the highest privilege task scheduler will allow the program to be launched for that user. If the user does not have admin privileges then task scheduler will only allow the program to launch with whatever the highest privilege level the user logged on has.

In fact you can not set Task Scheduler programatically to my knolwedge to allow a program to run with highest privileges of the logged on user.

Just as Microsoft disallows an app to use sendkeys to send enter to the UAC prompt, if you tried to do that, to make okay click on the UAC prompt. They only allow direct interaction by human for various security related functions.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, November 4, 2013 9:05 AM | 1 vote

Thank you all,

I understand that "

startInfo.Verb = "runas"

would launch the 3rd party application with administrator right thru my application, but in my case if the user makes a desktop shortcut of that using my application interface. and try to launch the application from shortcut, the application would be launch under standard privileges therefore what I want is to set the 3rd party application's property to "Run as administrator" thru code. so that even if the user launches that application from desktop shortcut, the application still runs under administrative privileges and the user don't need to manually interact with file's property.

I hope, I clarify my requirement and scenario.Please don't hesitate to ask in case of any confusion.

Thanks & Regards,

Maverick


Monday, November 4, 2013 8:10 PM

Haroon,

Would it not be crazy if Microsoft did spent so much money to create software which needs a special user action to run it as Administrator and in the same time then gives a possibility for those who would create malicious code to make it senseless.

For those who have not seen this link not yet

http://social.msdn.microsoft.com/Forums/vstudio/en-US/053a0a87-e9b8-42e7-b483-d6d47c41278e/contributors-how-to-avoid-aiding-the-development-of-malicious-code?forum=vbgeneral

If it is for software in your company then let your administrator install it in a way that it runs always as administrator

Success
Cor

Wow. I suppose Crazypennie is probably correct and I never looked at the registry after using Task Scheduler to see if it changed anything. That is really weird that Microsoft would allow that although I havent verified what Crazypennie posted I'm 100% sure it's accurate or Crazypennie wouldn't have posted it. That absolutely is the dumbest thing I think that Microsoft could allow as a security violation. Good catch about the malicious intent possibilites Mr. Cor Ligthert (or should I call you Mr. Always Right). :)

Oh, duh, I thought that would bypass the UAC prompt which is inaccurate I suppose. Whoops!

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Monday, January 22, 2018 4:43 PM

Updating this to include the C# implementation. Set the requireAdministrator flag in app manifest.