I run under SYSTEM account the code posted below. My goal is run an
executable in a specific user context.
With Windows XP works fine but with Vista the executable doesn't run. anyone
can help me?
Thanks very much.
Here is the code:
-----------------------------------------------------------------
Public Sub createProcess(B yVal Percorso As String, ByVal Parametri As
String, ByVal Utente As String)
Dim server As IntPtr = IntPtr.Zero
Dim token As IntPtr = IntPtr.Zero
server = OpenServer("loc alhost")
Dim ppSessionInfo As IntPtr = IntPtr.Zero
Dim count As Int32 = 0
'gets the session ID
Dim retval As Int32 = WTSEnumerateSes sions(server, 0, 1,
ppSessionInfo, count)
Dim sa As New SECURITY_ATTRIB UTES()
Dim pi As New PROCESS_INFORMA TION()
Dim si As New STARTUPINFO()
For Each sessionID As UInt32 In ListSessions()
Dim ppBuffer As System.IntPtr
Dim pBytesReturned As UInteger
WTSQuerySession Information(Ope nServer("localh ost"), sessionID,
WTS_INFO_CLASS. WTSUserName, ppBuffer, pBytesReturned)
Dim WTSUserName As String = Marshal.PtrToSt ringAnsi(ppBuff er)
If WTSUserName = Utente Then
Dim gotToken As Boolean = WTSQueryUserTok en(sessionID, token)
Dim pSecurityAttrib utes As SECURITY_ATTRIB UTES
pSecurityAttrib utes.bInheritHa ndle = True
pSecurityAttrib utes.Length =
Marshal.SizeOf( pSecurityAttrib utes)
pSecurityAttrib utes.lpSecurity Descriptor = IntPtr.Zero
Dim tokenDup As IntPtr = IntPtr.Zero
DuplicateTokenE x(token, MAXIMUM_ALLOWED ,
pSecurityAttrib utes, SECURITY_IMPERS ONATION_LEVEL.S ecurityImperson ation,
TOKEN_TYPE.Toke nPrimary, tokenDup)
CreateProcessAs User(tokenDup, Percorso, Percorso & " " &
Parametri, pSecurityAttrib utes, pSecurityAttrib utes, False, 0, IntPtr.Zero,
My.Application. Info.DirectoryP ath, si, pi)
MsgBox((New
System.Componen tModel.Win32Exc eption).Message )
Exit For
End If
Next
End Sub
executable in a specific user context.
With Windows XP works fine but with Vista the executable doesn't run. anyone
can help me?
Thanks very much.
Here is the code:
-----------------------------------------------------------------
Public Sub createProcess(B yVal Percorso As String, ByVal Parametri As
String, ByVal Utente As String)
Dim server As IntPtr = IntPtr.Zero
Dim token As IntPtr = IntPtr.Zero
server = OpenServer("loc alhost")
Dim ppSessionInfo As IntPtr = IntPtr.Zero
Dim count As Int32 = 0
'gets the session ID
Dim retval As Int32 = WTSEnumerateSes sions(server, 0, 1,
ppSessionInfo, count)
Dim sa As New SECURITY_ATTRIB UTES()
Dim pi As New PROCESS_INFORMA TION()
Dim si As New STARTUPINFO()
For Each sessionID As UInt32 In ListSessions()
Dim ppBuffer As System.IntPtr
Dim pBytesReturned As UInteger
WTSQuerySession Information(Ope nServer("localh ost"), sessionID,
WTS_INFO_CLASS. WTSUserName, ppBuffer, pBytesReturned)
Dim WTSUserName As String = Marshal.PtrToSt ringAnsi(ppBuff er)
If WTSUserName = Utente Then
Dim gotToken As Boolean = WTSQueryUserTok en(sessionID, token)
Dim pSecurityAttrib utes As SECURITY_ATTRIB UTES
pSecurityAttrib utes.bInheritHa ndle = True
pSecurityAttrib utes.Length =
Marshal.SizeOf( pSecurityAttrib utes)
pSecurityAttrib utes.lpSecurity Descriptor = IntPtr.Zero
Dim tokenDup As IntPtr = IntPtr.Zero
DuplicateTokenE x(token, MAXIMUM_ALLOWED ,
pSecurityAttrib utes, SECURITY_IMPERS ONATION_LEVEL.S ecurityImperson ation,
TOKEN_TYPE.Toke nPrimary, tokenDup)
CreateProcessAs User(tokenDup, Percorso, Percorso & " " &
Parametri, pSecurityAttrib utes, pSecurityAttrib utes, False, 0, IntPtr.Zero,
My.Application. Info.DirectoryP ath, si, pi)
MsgBox((New
System.Componen tModel.Win32Exc eption).Message )
Exit For
End If
Next
End Sub