Une classe qui vous permet d "executer en tant que" , c'est a dire executer un programme sous un user de windows en tant qu'un autre user, de manière transparente pour l'utilisateur (sans que le password n'apparaisse), en .net.
Il y avait deja une source pour faire ca sur le site, mais pas .net, et le code était pas exceptionnellement propre. (
http://www.vbfrance.com/code.aspx?ID=7555 )
Celui là est un peu plus propre a mon gout et c'est une classe.
Source / Exemple :
Imports System
Imports System.Runtime.InteropServices
Public Class clsRunAs
Public Const LOGON_WITH_PROFILE = &H1&
Public Const LOGON_NETCREDENTIALS_ONLY = &H2&
Public Const CREATE_DEFAULT_ERROR_MODE = &H4000000
Public Const CREATE_NEW_CONSOLE = &H10&
Public Const CREATE_NEW_PROCESS_GROUP = &H200&
Public Const CREATE_SEPARATE_WOW_VDM = &H800&
Public Const CREATE_SUSPENDED = &H4&
Public Const CREATE_UNICODE_ENVIRONMENT = &H400&
Public Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000&
Public Const BELOW_NORMAL_PRIORITY_CLASS = &H4000&
Public Const HIGH_PRIORITY_CLASS = &H80&
Public Const IDLE_PRIORITY_CLASS = &H40&
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const REALTIME_PRIORITY_CLASS = &H100&
Public Structure PROCESS_INFO
Public hProcess As IntPtr
Public hThread As IntPtr
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure
Public Structure STARTUP_INFO
Public cb As Integer
Public lpReserved As Integer
<MarshalAs(UnmanagedType.LPTStr)> _
Public lpDesktop As String
<MarshalAs(UnmanagedType.LPTStr)> _
Public lpTitle As String
Public dwX As Long
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 Integer
Public hStdInput As Integer
Public hStdOutput As Integer
Public hStdError As Integer
End Structure
<DllImport("C:\\Windows\\System32\\advapi32.dll")> _
Public Shared Function CreateProcessWithLogonW( _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpUsername As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpDomain As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpPassword As String, _
ByVal dwLogonFlags As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpApplicationName As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpCommandLine As String, _
ByVal lpCreationFlags As Integer, _
ByVal lpVoid As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpCurrentDirectory As String, _
ByRef lpStartupInfo As STARTUP_INFO, _
ByRef lpProcessInfo As PROCESS_INFO) As Boolean
End Function
Public Sub start(ByVal strCMD As String, ByVal strUser As String, ByVal strPass As String, Optional ByVal strArguments As String = "")
Dim strDomain As String
Try
strDomain = Split(strUser, "\")(0)
strUser = Split(strUser, "\")(1)
Catch
' no Domain given Try invoking users Domain
strDomain = System.Environment.UserDomainName
End Try
Dim lres As Long
Dim pStartInfo As STARTUP_INFO
Dim pProcessInfo As PROCESS_INFO
Dim LOGON_NETCREDENTIALS_ONLY As Long = LOGON_WITH_PROFILE
Dim CREATE_DEFAULT_ERROR_MODE As Long = CREATE_DEFAULT_ERROR_MODE
Dim lpUsername As String = strUser
Dim lpPassword As String = strPass
Dim lpApplicationName As String = strCMD
Dim lpdomainname As String = strDomain
Dim lpcommandline As String
If strArguments = "" Then
lpcommandline = VariantType.Null
Else
lpcommandline = strArguments
End If
Dim lpCurrentDirectory As String = "c:\"
pStartInfo.cb = Len(pStartInfo)
pStartInfo.lpTitle = "MowConsole"
pStartInfo.dwFlags = 0&
Try
lres = CreateProcessWithLogonW(lpUsername, lpdomainname, lpPassword, LOGON_NETCREDENTIALS_ONLY, lpApplicationName, lpcommandline, CREATE_DEFAULT_ERROR_MODE, 0, lpCurrentDirectory, pStartInfo, pProcessInfo)
Catch ec As Exception
MsgBox(ec.ToString)
End Try
End Sub
End Class
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.