CreateProcessAsUser and Windows Vista trouble

Giganews Newsgroups
Subject: CreateProcessAsUser and Windows Vista trouble
Posted by:  Bobil (Bob…@discussions.microsoft.com)
Date: Tue, 29 Jul 2008

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(ByVal 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("localhost")

        Dim ppSessionInfo As IntPtr = IntPtr.Zero

        Dim count As Int32 = 0
        'gets the session ID
        Dim retval As Int32 = WTSEnumerateSessions(server, 0, 1,
ppSessionInfo, count)

        Dim sa As New SECURITY_ATTRIBUTES()
        Dim pi As New PROCESS_INFORMATION()
        Dim si As New STARTUPINFO()

        For Each sessionID As UInt32 In ListSessions()
            Dim ppBuffer As System.IntPtr
            Dim pBytesReturned As UInteger
            WTSQuerySessionInformation(OpenServer("localhost"), sessionID,
WTS_INFO_CLASS.WTSUserName, ppBuffer, pBytesReturned)
            Dim WTSUserName As String = Marshal.PtrToStringAnsi(ppBuffer)

            If WTSUserName = Utente Then

                Dim gotToken As Boolean = WTSQueryUserToken(sessionID, token)
                Dim pSecurityAttributes As SECURITY_ATTRIBUTES
                pSecurityAttributes.bInheritHandle = True
                pSecurityAttributes.Length =
Marshal.SizeOf(pSecurityAttributes)
                pSecurityAttributes.lpSecurityDescriptor = IntPtr.Zero

                Dim tokenDup As IntPtr = IntPtr.Zero
                DuplicateTokenEx(token, MAXIMUM_ALLOWED,
pSecurityAttributes, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
TOKEN_TYPE.TokenPrimary, tokenDup)

                CreateProcessAsUser(tokenDup, Percorso, Percorso & " " &
Parametri, pSecurityAttributes, pSecurityAttributes, False, 0, IntPtr.Zero,
My.Application.Info.DirectoryPath, si, pi)
                                MsgBox((New
System.ComponentModel.Win32Exception).Message)
                Exit For
            End If
        Next

End Sub

Replies