-
Mar 25th, 2010, 12:08 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Run as administrator automatically
Hello,
I want to open my program Run as administrator automatically ,what should i do
-
Mar 25th, 2010, 12:21 PM
#2
Lively Member
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
-
Mar 25th, 2010, 12:33 PM
#3
Thread Starter
Addicted Member
Re: Run as administrator automatically
Originally Posted by C0der
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
-
Mar 25th, 2010, 12:43 PM
#4
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
-
Mar 25th, 2010, 12:58 PM
#5
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.
-
Mar 25th, 2010, 12:58 PM
#6
Thread Starter
Addicted Member
Re: Run as administrator automatically
Originally Posted by si_the_geek
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.
-
Mar 25th, 2010, 01:27 PM
#7
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
-
Mar 25th, 2010, 01:28 PM
#8
Re: Run as administrator automatically
Then you're opening them in a loop or in a timer.
-
Mar 25th, 2010, 01:31 PM
#9
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
-
Mar 25th, 2010, 02:05 PM
#10
Thread Starter
Addicted Member
-
Mar 25th, 2010, 02:30 PM
#11
Re: Run as administrator automatically
Read more and search the forums and internet, plenty of examples around.
-
Mar 25th, 2010, 02:39 PM
#12
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
-
Mar 25th, 2010, 02:48 PM
#13
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.
-
Mar 25th, 2010, 03:05 PM
#14
Thread Starter
Addicted Member
-
Mar 25th, 2010, 03:57 PM
#15
Re: Run as administrator automatically
Originally Posted by baja_yu
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.
-
Mar 25th, 2010, 04:03 PM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|