|
-
Feb 23rd, 2008, 12:26 PM
#1
[RESOLVED] [2005] programmatically set program to 'run as administrator'
how can i programmatically set my program to 'run as administrator' without having to manually open the programs properties + selecting 'run as administrator' ?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 12:45 PM
#2
Fanatic Member
Re: [2005] programmatically set program to 'run as administrator'
First of all, you should design your program so it works without. Not everyone will have admin permissions.
Now I know this can be a problem in vista especially when writing data to "program files" if tahts where your app is installed.
Search the forums for "vista manifest"
-
Feb 23rd, 2008, 01:05 PM
#3
Re: [2005] programmatically set program to 'run as administrator'
some programs will require administrator privileges, however you design them
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 02:49 PM
#4
Re: [2005] programmatically set program to 'run as administrator'
You need to compile your application with a manifest to do this.
First, create a manifest file (basically "MyApplication.exe.manifest") and put this in it:
Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="My Application" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Then add this to your project's post-build event: "$(DevEnvDir)..\..\SDK\v2.0\bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest" –outputresource:"$(TargetDir)$(TargetFileName)";#1
As long as the manifest is included in your project, this should work with little to no adjustments.
-
Feb 23rd, 2008, 02:52 PM
#5
Re: [2005] programmatically set program to 'run as administrator'
 Originally Posted by masfenix
First of all, you should design your program so it works without. Not everyone will have admin permissions.
Now I know this can be a problem in vista especially when writing data to "program files" if tahts where your app is installed.
Search the forums for "vista manifest"
Yes, usage of the administrator should be extremely limited. The only reasons to modify areas of the file system that require administrator access are updaing your application, saving system configurations (typical if you're working on a utility), or possibly installing plugins (though, arguably, you could make a case to install them in the all users folders instead).
-
Feb 23rd, 2008, 03:30 PM
#6
Re: [2005] programmatically set program to 'run as administrator'
ok i copied the manifest code + saved it to my bin/debug directory. + added it to my project. was i supposed to make any changes to that or save it as it is?
question 2: i couldn't find a post build event anywhere. am i missing something?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 03:46 PM
#7
Re: [2005] programmatically set program to 'run as administrator'
 Originally Posted by .paul.
ok i copied the manifest code + saved it to my bin/debug directory. + added it to my project. was i supposed to make any changes to that or save it as it is?
Well I wouldn't suggest putting anything in your bin directory since any of it could be replaced. I always included it in my project and had it output to bin. But other than that, make sure the correct name of your application is in the manifest.
 Originally Posted by .paul.
question 2: i couldn't find a post build event anywhere. am i missing something?
Hmmm... I did this in C#. I have no clue if it's different for VB. maybe someone else will have an idea.
-
Feb 23rd, 2008, 03:59 PM
#8
Re: [2005] programmatically set program to 'run as administrator'
i converted it to vb2008.
it was a manifest file.
thanks
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 5th, 2010, 10:30 AM
#9
New Member
Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'
Then add this to your project's post-build event: "$(DevEnvDir)..\..\SDK\v2.0\bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest" –outputresource:"$(TargetDir)$(TargetFileName)";#1
I am new to this concept.Could you please explain what should I replace "(DevEnvDir)..\..\" with? Also the "$(TargetDir)$(TargetFileName)" will be a folder in the program file where my application is installed??
Thank you.
-
Jan 9th, 2017, 03:56 AM
#10
New Member
Re: [2005] programmatically set program to 'run as administrator'
There is no mt.exe file under SDK\v2.0\bin
-
Jan 11th, 2017, 07:04 AM
#11
Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'
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
On the button click you can go:
VB.Net Code:
Try NativeMethod.ElevateProcess(Interaction.Command) Catch ex As Exception i00CodeLib.MsgBox(Me, "The following error occurred while elevating the installer:" & vbCrLf & ex.Message, MsgBoxStyle.Critical) End Try
If I have needed to ensure that it runs as administrator I just do this @ startup and execute itself again with admin rights and command line switches... gets rid of the need for a manifest file.
Kris
-
Apr 13th, 2017, 11:55 AM
#12
Fanatic Member
Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'
Last edited by rex64; Apr 13th, 2017 at 12:08 PM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Apr 13th, 2017, 11:57 AM
#13
Fanatic Member
Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'
Last edited by rex64; Apr 13th, 2017 at 12:07 PM.
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
|