Create RDP application on VB.NET

Stefano Fattori 21 Reputation points
2021-02-25T17:05:14.893+00:00

Hello, is there a new way to create RDP application with .NET 5.0? I tried COM RDP components v12 and MS Terminal Service 1 and they don't work on visual studio 2019. I tried WTSQuerySessionInformation classes and they don't work. What can I use for this project? Thank you so much Stefano

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,264 questions
{count} votes

Accepted answer
  1. Castorix31 81,061 Reputation points
    2021-02-26T09:23:12.53+00:00

    The test I did (I used ATL to host the control like I do it in C++) =>

    (Add COM Reference : C:\Windows\System32\MsTscAx.dll)

    Imports System.Runtime.InteropServices
    Imports MSTSCLib
    
    Public Class Form1
        Public Enum HRESULT As Integer
            S_OK = 0
            S_FALSE = 1
            E_NOINTERFACE = &H80004002
            E_NOTIMPL = &H80004001
            E_FAIL = &H80004005
            E_UNEXPECTED = &H8000FFFF
            E_OUTOFMEMORY = &H8007000E
        End Enum
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxWinInit() As Boolean
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetControl(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetHost(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function CreateWindowEx(ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As IntPtr, ByVal hMenu As IntPtr, ByVal hInstance As IntPtr, ByVal lpParam As IntPtr) As IntPtr
        End Function
    
        Public Const WS_OVERLAPPED = &H0
        Public Const WS_BORDER = &H800000
        Public Const WS_POPUP = &H80000000
        Public Const WS_CHILD = &H40000000
        Public Const WS_MINIMIZE = &H20000000
        Public Const WS_VISIBLE = &H10000000
        Public Const WS_DISABLED = &H8000000
    
        Public Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
            If IntPtr.Size = 4 Then
                Return GetWindowLong32(hWnd, nIndex)
            End If
            Return GetWindowLongPtr64(hWnd, nIndex)
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLong", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLong32(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLongPtr", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLongPtr64(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        Public Const GWL_HINSTANCE = -6
    
        Private hWndContainer As IntPtr = IntPtr.Zero
        Dim pMsTscNonScriptable As IMsTscNonScriptable = Nothing
        Dim pMsRdpClient2 As IMsRdpClient2 = Nothing
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If (AtlAxWinInit()) Then
                ' CLSID_MsTscAxNotSafeForScripting
                hWndContainer = CreateWindowEx(0, "AtlAxWin", "{A41A4187-5A86-4E26-B40A-856F9035D9CB}", WS_VISIBLE Or WS_CHILD Or WS_BORDER, 10, 10, 1180, 780, Handle, CType(10, IntPtr), CType(GetWindowLong(Me.Handle, GWL_HINSTANCE), IntPtr), IntPtr.Zero)
            End If
            Dim pUnknown As Object = Nothing
            Dim hr As HRESULT = AtlAxGetControl(hWndContainer, pUnknown)
            If (hr = HRESULT.S_OK) Then
                pMsTscNonScriptable = CType(pUnknown, IMsTscNonScriptable)
                pMsRdpClient2 = CType(pUnknown, IMsRdpClient2)
                If (pMsRdpClient2 IsNot Nothing And pMsTscNonScriptable IsNot Nothing) Then
                        ' Test with Demo Server 
                    ' https://www.secure-od.com/demo-server/
                    pMsTscNonScriptable.ClearTextPassword = "D3m02014*Test"
                    pMsRdpClient2.UserName = "demo2"
                    pMsRdpClient2.Server = "109.168.97.222"
                    pMsRdpClient2.DesktopWidth = 1920
                    pMsRdpClient2.DesktopHeight = 1080
                    pMsRdpClient2.Connect()
                End If
            End If
            Me.ClientSize = New System.Drawing.Size(1200, 800)
            CenterToScreen()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If (pMsRdpClient2 IsNot Nothing) Then
                pMsRdpClient2.Disconnect()
            End If
        End Sub
    End Class
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Stefano Fattori 21 Reputation points
    2021-02-26T07:39:01.863+00:00

    How did you import dll in vb project?
    Are the controls rdp on toolbox section disabled?


  2. Stefano Fattori 21 Reputation points
    2021-02-26T09:10:14.58+00:00

    Please, can I also ask you how did you add rdp control to the form? I can't find right class of it...
    My code:

    Dim frmTemp As New Form
    
    Dim rdpView As New MSTSCLib.MsTscAxNotSafeForScripting
    
    rdpView.Server = "ip server"
    rdpView.UserName = "user"
    
    frmTemp.Controls.Add(rdpView)
    frmTemp.Show()
    rdpView.Connect()
    

    But it doesn't work, I'm not sure that MSTSCLib.MsTscAxNotSafeForScripting is the right control to use.

    0 comments No comments

  3. Stefano Fattori 21 Reputation points
    2021-02-26T09:54:04.097+00:00

    A lot of code... :)

    I found other example like this 354007-vbnet-terminal-server-application

    using this code:

    Dim rdpconnection As New AxMsRdpClient9NotSafeForScripting
               Dim newTabPage As New DevExpress.XtraTab.XtraTabPage
               newTabPage.Text = txtserver.Text
               XtraTabControl1.TabPages.Add(newTabPage)
               newTabPage.Controls.Add(rdpconnection)
    
               rdpconnection.Dock = DockStyle.Fill
               rdpconnection.UserName = txtusername.Text
               rdpconnection.Domain = "AKD"
               rdpconnection.Server = txtserver.Text
    
               Dim secured As MSTSCLib.IMsTscNonScriptable = DirectCast(rdpconnection.GetOcx(), MSTSCLib.IMsTscNonScriptable)
               secured.ClearTextPassword = txtpassword.Text
    
               rdpconnection.Connect()
    

    but I can't understand what is "AxMsRdpClient9NotSafeForScripting", I can't find it in MSTSCLib.


  4. Stefano Fattori 21 Reputation points
    2021-03-05T15:43:07.607+00:00

    Hi everybody,
    I found a way to connect to windows 10 using this property:

    RdpClient.AdvancedSettings9.EnableCredSspSupport = True
    
    0 comments No comments