Results 1 to 16 of 16

Thread: [RESOLVED] Run as administrator automatically

  1. #1

    Thread Starter
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Resolved [RESOLVED] Run as administrator automatically

    Hello,
    I want to open my program Run as administrator automatically ,what should i do

  2. #2
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: Run as administrator automatically

    Could this do the job?

    Code:
    Dim I As Long
    Shell "runas.exe /user:administrator ""Your-App.exe""", vbNormalFocus
    On Error Resume Next
    Do
    Sleep 100
    If Err = 0 Then
    SendKeys "{enter}", True
    Exit Do
    Else
    Err.Clear
    End If
    I = I + 1
    Loop Until I = 50

  3. #3

    Thread Starter
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Run as administrator automatically

    Quote Originally Posted by C0der View Post
    Could this do the job?

    Code:
    Dim I As Long
    Shell "runas.exe /user:administrator ""Your-App.exe""", vbNormalFocus
    On Error Resume Next
    Do
    Sleep 100
    If Err = 0 Then
    SendKeys "{enter}", True
    Exit Do
    Else
    Err.Clear
    End If
    I = I + 1
    Loop Until I = 50
    No That's Not Worked

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Run as administrator automatically

    We aren't mind readers, so please don't give us just virtually meaningless comments like "not worked" - explain what is (or isn't) happening, including details of errors if apt.


    Here is an alternative method:
    Code:
    'declarations:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Const SW_SHOWNORMAL = 1
    
    'code:
      ShellExecute 0, "runas", "c:\folder\yourapp.exe", Command, vbNullString, SW_SHOWNORMAL

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Run as administrator automatically

    If you mean that you have an application that you made and you want it to automatically run as as Admin when someone double-clicks it, you need to create a manifest file for your application. Search the forums and Google for that term and you should find a lot of info on it.

  6. #6

    Thread Starter
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Run as administrator automatically

    Quote Originally Posted by si_the_geek View Post

    Here is an alternative method:
    Code:
    'declarations:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Const SW_SHOWNORMAL = 1
    
    'code:
      ShellExecute 0, "runas", "c:\folder\yourapp.exe", Command, vbNullString, SW_SHOWNORMAL
    it's worked but when i ran my program i see n of my program open and they dont end
    Last edited by Pc Monk; Mar 25th, 2010 at 01:04 PM.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Run as administrator automatically


    It just goes to show that you should think of the implications of the code, rather than just pasting and running it!

    There are various ways to deal with that issue, including:
    • adding something extra to the command line, and only run the ShellExecute if the command line doesn't include it.
    • check via an API (I think IsUserAdmin is one) if your program is already running as an Admin.
    • check by some other means if the program has admin rights (the program I took the example from list processes, and checks if any are owned by "system")

    The first could be done like this:
    Code:
      If InStr(Command, "/admin") = 0 Then
        ShellExecute 0, "runas", "c:\folder\yourapp.exe", Command & "/admin", vbNullString, SW_SHOWNORMAL
      End If

  8. #8
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Run as administrator automatically

    Then you're opening them in a loop or in a timer.

  9. #9
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Run as administrator automatically

    Or you added that line in your program and pointed it to it's own Exe file so every time it launches it starts one more instance of itself.

    Read my first post. If you want to have it run as admin when it is started for the first time, you will need to create a manifest file for your applicaiton.

    Look at these articles:

    http://en.wikipedia.org/wiki/User_Ac...ting_elevation
    http://helpware.net/VistaCompat.htm

  10. #10

    Thread Starter
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Run as administrator automatically

    i read those but i dont understand anything i'm on hang now
    but you're right it will be open 2 of my project

  11. #11
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Run as administrator automatically

    Read more and search the forums and internet, plenty of examples around.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Run as administrator automatically

    You can make my version only have one version open by unloading the first just before the End If, eg:
    Code:
        ShellExecute 0, "runas", "c:\folder\yourapp.exe", Command & "/admin", vbNullString, SW_SHOWNORMAL
        Unload Me
        Exit Sub
      End If

  13. #13
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Run as administrator automatically

    Actually, Si, that will result in a similar problem. The next instance will then also open a new one and close it self. He will get the effect of the application constantly reloading.

    The proper way to do this is to make and use a manifest.

  14. #14

    Thread Starter
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Run as administrator automatically

    Thanks si_the_geek and about what you say baja_yu maybe you're right. but my problem is RESOLVED

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Run as administrator automatically

    Quote Originally Posted by baja_yu View Post
    Actually, Si, that will result in a similar problem. The next instance will then also open a new one and close it self.
    Actually it wont, due to the If statement and appended command line. Only the initial instance will do it.

    The proper way to do this is to make and use a manifest.
    Possibly, but it depends on the circumstances... only recent versions of Windows support manifests, and that method is only apt if you want to always run it as Admin.

  16. #16
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] Run as administrator automatically

    Oh, I didn't look at the If statement in the previous post. I already stopped 'supporting' Windows versions older than XP, so I didn't bother to research if manifests would be supported or not. Guess you're right that he should take that into account too.

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