miércoles, 3 de marzo de 2010

VB.NET RUNAS

Hola,
Seguramente muchos nos hemos visto en la necesidad de crear una aplicación la cual ejecute otras aplicaciones con un usuario diferente. Sin la necesidad de tener privilegios de administración en PC.

Bueno acá esta el código como se realiza en VB.NET.
1.-Un form con los siguiente objetos:
  • Textbox1 = Para el dato del usuario.
  • Textbox2 = Para el dato del dominio.
  • Textbox3 = Para el dato la aplicación que queremos ejecutar.
  • Button = Para el generar el evento
2.- archivo runas.vb

Código del Button :

Imports System.Environment

Imports System.Diagnostics
Imports System.Runtime.InteropServices

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try

Dim proc As System.Diagnostics.Process = VastAbyss.RunAs.StartProcess(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)

Catch w32e As System.ComponentModel.Win32Exception

End Try

End Sub
Código archivo runas.vb
Imports System

Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Imports Consola_parametros.VastAbyss

Namespace VastAbyss
_

Public Class RunAs
_

Private Shared Function CreateProcessWithLogonW(ByVal lpszUsername As [String], ByVal lpszDomain As [String], ByVal lpszPassword As [String], ByVal dwLogonFlags As Integer, ByVal applicationName As String, ByVal commandLine As StringBuilder, _

ByVal creationFlags As UInteger, ByVal environment As IntPtr, ByVal currentDirectory As String, ByRef sui As StartUpInfo, ByRef processInfo As ProcessInformation) As Boolean

End Function

_

Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean

End Function

_

Public Structure StartUpInfo

Public cb As Integer

_

Public lpReserved As String

_

Public lpDesktop As String

_

Public lpTitle As String

Public dwX As Integer

Public dwY As Integer

Public dwXSize As Integer

Public dwYSize As Integer

Public dwXCountChars As Integer

Public dwYCountChars As Integer

Public dwFillAttribute As Integer

Public dwFlags As Integer

Public wShowWindow As Short

Public cbReserved2 As Short

Public lpReserved2 As IntPtr

Public hStdInput As IntPtr

Public hStdOutput As IntPtr

Public hStdError As IntPtr

End Structure

_

Public Structure ProcessInformation

Public hProcess As IntPtr

Public hThread As IntPtr

Public dwProcessId As Integer

Public dwThreadId As Integer

End Structure

_

Public Enum FillAttributes

BackgroundIntensity = 128

BackgroundRed = 64

BackgroundGreen = 32

BackgroundBlue = 16

ForegroundIntensity = 8

ForegroundRed = 4

ForegroundGreen = 2

ForegroundBlue = 1

End Enum

_

Public Enum LogonFlags

WithProfile = 1

NetworkCredentialsOnly = 2

End Enum

_

Public Enum CreationFlags

Suspended = &H4

NewConsole = &H10

NewProcessGroup = &H200

SeperateWOWVDM = &H800

UnicodeEnvironment = &H400

DefaultErrorMode = &H4000000

End Enum

_

Public Enum PriorityFlags

NormalPriority = &H20

IdlePriority = &H40

HighPriority = &H80

RealTimePriority = &H100

BelowNormalPriority = &H4000

AboveNormalPriority = &H8000

End Enum

_

Public Enum StartUpInfoFlags As UInteger

UseShowWindow = &H1

UseSize = &H2

UsePosition = &H4

UseCountChars = &H8

UseFillAttribute = &H10

RunFullScreen = &H20

ForceOnFeedback = &H40

ForceOffFeedback = &H80

UseStandardHandles = &H100

UseHotKey = &H200

UseMonitor = &H400

UseIcon = &H400

TitleShortcut = &H800

Screensaver = &H8000000

End Enum

'=====>>>>> carlos onocuica

Public Shared Function StartProcess(ByVal userName As String, ByVal domain As String, ByVal password As String, ByVal logonFlags As LogonFlags, ByVal applicationName As String, ByVal commandLine As String, _

ByVal creationFlags As CreationFlags, ByVal environment As IntPtr, ByVal currentDirectory As String, ByRef startupInfo As StartUpInfo, ByRef processInfo As ProcessInformation) As System.Diagnostics.Process

Dim cl As New StringBuilder(commandLine.Length)

cl.Append(commandLine)

Dim retval As Boolean = CreateProcessWithLogonW(userName, domain, password, CInt(logonFlags), applicationName, cl, _

CUInt(creationFlags), environment, currentDirectory, startupInfo, processInfo)

If Not retval Then

Throw New System.ComponentModel.Win32Exception()

Else

CloseHandle(processInfo.hProcess)

CloseHandle(processInfo.hThread)

Return System.Diagnostics.Process.GetProcessById(processInfo.dwProcessId)

End If

End Function

Public Shared Function StartProcess(ByVal userName As String, ByVal domain As String, ByVal password As String, ByVal commandLine As String) As System.Diagnostics.Process

Dim processInfo As ProcessInformation

Dim startupInfo As New StartUpInfo()

startupInfo.cb = Marshal.SizeOf(startupInfo)

startupInfo.lpTitle = Nothing

startupInfo.dwFlags = CInt(StartUpInfoFlags.UseCountChars)

startupInfo.dwYCountChars = 50

Return StartProcess(userName, domain, password, LogonFlags.WithProfile, Nothing, commandLine, _

CreationFlags.NewConsole, IntPtr.Zero, Nothing, startupInfo, processInfo)

End Function

Private _userName As String

Public Property UserName() As String

Get

Return _userName

End Get

Set(ByVal value As String)

_userName = value

End Set

End Property

Private _domain As String

Public Property Domain() As String

Get

Return _domain

End Get

Set(ByVal value As String)

_domain = value

End Set

End Property

Private _password As String

Public Property Password() As String

Get

Return _password

End Get

Set(ByVal value As String)

_password = value

End Set

End Property

Private _logonFlags As LogonFlags

Public Property LogonFlagsInstance() As LogonFlags

Get

Return _logonFlags

End Get

Set(ByVal value As LogonFlags)

_logonFlags = value

End Set

End Property

Private _applicationName As String

Public Property ApplicationName() As String

Get

Return _applicationName

End Get

Set(ByVal value As String)

_applicationName = value

End Set

End Property

Private _commandLine As String

Public Property CommandLine() As String

Get

Return _commandLine

End Get

Set(ByVal value As String)

_commandLine = value

End Set

End Property

Private _creationFlags As CreationFlags

Public Property CreationFlagsInstance() As CreationFlags

Get

Return _creationFlags

End Get

Set(ByVal value As CreationFlags)

_creationFlags = value

End Set

End Property

Private _currentDirectory As String

Public Property CurrentDirectory() As String

Get

Return _currentDirectory

End Get

Set(ByVal value As String)

_currentDirectory = value

End Set

End Property

Private _startupInfo As StartUpInfo

Private _processInfo As ProcessInformation

Public Property ProcessInfo() As ProcessInformation

Get

Return _processInfo

End Get

Set(ByVal value As ProcessInformation)

_processInfo = value

End Set

End Property

Private _environment As IntPtr

Public Property Environment() As IntPtr

Get

Return _environment

End Get

Set(ByVal value As IntPtr)

_environment = value

End Set

End Property

Public Sub New()

_userName = System.Environment.UserName

_domain = System.Environment.UserDomainName

_password = ""

_logonFlags = LogonFlags.WithProfile

_commandLine = System.Environment.CommandLine

_creationFlags = CreationFlags.NewConsole

_currentDirectory = System.Environment.CurrentDirectory

_startupInfo = New StartUpInfo()

_startupInfo.cb = Marshal.SizeOf(_startupInfo)

_startupInfo.dwFlags = CInt(StartUpInfoFlags.UseCountChars)

_startupInfo.dwYCountChars = 50

Using cp As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess()

_applicationName = cp.StartInfo.FileName

_startupInfo.lpTitle = cp.MainWindowTitle

End Using

_processInfo = New ProcessInformation()

_environment = IntPtr.Zero

End Sub

Public Function StartProcess() As System.Diagnostics.Process

Return StartProcess(UserName, Domain, Password, LogonFlagsInstance, ApplicationName, CommandLine, _

CreationFlagsInstance, Environment, CurrentDirectory, _startupInfo, _processInfo)

End Function

End Class

End Namespace
  
En caso necesiten capturar su dominio por defaul ya saben:

TextBox1.Text = UserName


TextBox2.Text = UserDomainName

Saludos,
Carlos Onocuica

No hay comentarios:

Publicar un comentario