|
begin process at 2008 08 08 21:24:53
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
EXECUTER EN TANT QUE EN .NET
Information sur la source
Description
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
- 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
-
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
Historique
- 26 octobre 2004 03:32:23 :
- Mise en page.
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | | | | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|