Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a process/function as administrator 1

Status
Not open for further replies.

UdoScheuvens

Programmer
Apr 12, 2001
42
DE
Hello,

I'm want to have my application run with non-admin users logged on. But sometimes I need administrative rights, e.g. to access write-protected registry areas.

Assuming that the software knew about a local administrator's account info, is there a way to execute a process or function "as admin"?

Or can I somehow start a pre-defined scheduled task being set up to run as an administrator?

Any info in this direction would be helpful.
Udo
 
You can and it is called impersonation

Code:
 <SecurityPermissionAttribute(SecurityAction.Demand, ControlPrincipal:=True, UnmanagedCode:=True)> Private Shared Function GetWindowsIdentity(ByVal UserName As String, ByVal Domain As String, ByVal Password As String) As WindowsIdentity
            Dim SecurityToken As Integer
            Dim Success As Boolean
            Success = LogonUser(UserName, Domain, Password, Logon.NetworkCleartext, Provider.Windows2000, SecurityToken)
            If Not Success Then
                Throw New System.Exception("Logon Failed. Error: " & GetLastError())
            End If
            Return New WindowsIdentity(New IntPtr(SecurityToken))
        End Function

        Public Shared Sub ImpersonateAdministrator(ByVal Impersonate As Boolean)
            Dim newidentity As WindowsIdentity
            Dim oldidentity As WindowsIdentity
            If Impersonate = True Then
                newidentity = GetWindowsIdentity("adminlogonname", "domain", "password")
                objNewContext = newidentity.Impersonate
            Else
                objNewContext.Undo()
            End If
            newidentity = Nothing
            oldidentity = Nothing
        End Sub

<DllImport("advapi32.dll")> _
    Private Shared Function LogonUser(ByVal lpszUsername As String, _
        ByVal lpszDomain As String, ByVal lpszPassword As String, _
        ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
    ByRef phToken As Integer) As Boolean
        End Function

Christiaan Baes
Belgium

"My new site" - Me
 
Thanks, that did it!

A note for other people trying to work with this function: The user currently logged on needs to have the right "Act as part of the operating system" in the local security settings in order to execute the function "LogonUser".
 
There's still a problem remaining:

I'm trying to reboot using the WMI method "Win32Shutdown". This works pretty fine as administrator, but in combination with the technique described above I get an exception "Class not registered".

How to get around this?
Udo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top