Results 1 to 13 of 13

Thread: [RESOLVED] [2005] programmatically set program to 'run as administrator'

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Resolved [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' ?

  2. #2
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    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"

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] programmatically set program to 'run as administrator'

    some programs will require administrator privileges, however you design them

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] programmatically set program to 'run as administrator'

    Quote 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).
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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?

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] programmatically set program to 'run as administrator'

    Quote 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.
    Quote 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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  8. #8

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] programmatically set program to 'run as administrator'

    i converted it to vb2008.
    it was a manifest file.
    thanks

  9. #9
    New Member
    Join Date
    Aug 2010
    Posts
    1

    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.

  10. #10
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Re: [2005] programmatically set program to 'run as administrator'

    There is no mt.exe file under SDK\v2.0\bin

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

    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:
    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

    On the button click you can go:

    VB.Net Code:
    1. Try
    2.     NativeMethod.ElevateProcess(Interaction.Command)
    3. Catch ex As Exception
    4.     i00CodeLib.MsgBox(Me, "The following error occurred while elevating the installer:" & vbCrLf & ex.Message, MsgBoxStyle.Critical)
    5. 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

  12. #12
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'

    Wrong post. I meant to post in here: http://www.vbforums.com/showthread.p...61#post5159761
    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.

  13. #13
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: [RESOLVED] [2005] programmatically set program to 'run as administrator'

    Wrong post.
    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
  •  



Click Here to Expand Forum to Full Width