Adagio
Well-known member
- Joined
- Dec 12, 2005
- Messages
- 162
- Programming Experience
- Beginner
As probably many has done before me, I'm trying to start another application with elevated rights. I have tried many different things, but so far I have only gotten it working with domain Administrators
Here's the code I have seen posted most places:
The above code doesn't work. It does start cmd.exe as the local administrator (which I tested by going through cmd to C:\Users\SomeLocalAdmin, this I was allowed to do in cmd. If I started cmd as the user I'm currently logged in with I would of course not be allowed to go there). It does start the file as this administrator, but for some reason I'm not getting Administrator rights (which I tested by going to C:\Windows\ and ran the command "md test" to make a new directory. If I started the application by right-clicking on cmd.exe and used "run as" and logged in as SomeLocalAdmin I was allowed to do this)
The bottom line is: I can only run an application with elevated rights if I use a domain Admin, but if I do it as a local Admin I'm not getting any admin rights
The problem is that where this program needs to run, there is no domain. It's just some normal home computer that just needs to be allowed to run a certain program as Administrator
Of course I'm not going to let the application start cmd.exe with Administrator, that's just the program I'm using to test
Here's the code I have seen posted most places:
VB.NET:
Function ConvertToSecureString(ByVal str As String) As SecureString
Dim password As New SecureString
For Each c As Char In str.ToCharArray
password.AppendChar(c)
Next
Return password
End Function
Public Sub DoSomething()
Dim process As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo()
processStartInfo.FileName = "C:\Windows\System32\cmd.exe"
If System.Environment.OSVersion.Version.Major >= 6 Then ' Windows Vista or higher
processStartInfo.Verb = "runas"
Else
' No need to prompt to run as admin
End If
processStartInfo.UserName = "SomeLocalAdmin"
processStartInfo.Password = ConvertToSecureString("SuperSecurePassword")
processStartInfo.UseShellExecute = False
Try
process = System.Diagnostics.Process.Start(processStartInfo)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If Not (process Is Nothing) Then
process.Dispose()
End If
End Try
End Sub
The above code doesn't work. It does start cmd.exe as the local administrator (which I tested by going through cmd to C:\Users\SomeLocalAdmin, this I was allowed to do in cmd. If I started cmd as the user I'm currently logged in with I would of course not be allowed to go there). It does start the file as this administrator, but for some reason I'm not getting Administrator rights (which I tested by going to C:\Windows\ and ran the command "md test" to make a new directory. If I started the application by right-clicking on cmd.exe and used "run as" and logged in as SomeLocalAdmin I was allowed to do this)
The bottom line is: I can only run an application with elevated rights if I use a domain Admin, but if I do it as a local Admin I'm not getting any admin rights
The problem is that where this program needs to run, there is no domain. It's just some normal home computer that just needs to be allowed to run a certain program as Administrator
Of course I'm not going to let the application start cmd.exe with Administrator, that's just the program I'm using to test