Domanda

Sto cercando di scrivere un programma ciao tipo mondo per l'utilizzo di canali virtuali nelle finestre client di Servizi terminal.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }
    IntPtr mHandle = IntPtr.Zero;
    private void Form1_Load(object sender, EventArgs e)
    {
        mHandle = NativeMethods.WTSVirtualChannelOpen(IntPtr.Zero, -1, "TSCRED");
        if (mHandle == IntPtr.Zero)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        uint bufferSize = 1024;
        StringBuilder buffer = new StringBuilder();
        uint bytesRead;
        NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
        if (bytesRead == 0)
        {
            MessageBox.Show("Got no Data");
        }
        else
        {
            MessageBox.Show("Got data: " + buffer.ToString());
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (mHandle != System.IntPtr.Zero)
        {
            NativeMethods.WTSVirtualChannelClose(mHandle);
        }
        base.Dispose(disposing);

    }
}

internal static class NativeMethods
{
    [DllImport("Wtsapi32.dll")]
    public static extern IntPtr WTSVirtualChannelOpen(IntPtr server,
        int sessionId, [MarshalAs(UnmanagedType.LPStr)] string virtualName);

    //[DllImport("Wtsapi32.dll", SetLastError = true)]
    //public static extern bool WTSVirtualChannelRead(IntPtr channelHandle, long timeout,
    //        byte[] buffer, int length, ref int bytesReaded);

    [DllImport("Wtsapi32.dll")]
    public static extern bool WTSVirtualChannelClose(IntPtr channelHandle);


    [DllImport("Wtsapi32.dll", EntryPoint = "WTSVirtualChannelRead")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSVirtualChannelRead(
        [In()] System.IntPtr hChannelHandle
        , uint TimeOut
        , [Out()] [MarshalAs(UnmanagedType.LPStr)] 
          System.Text.StringBuilder Buffer
        , uint BufferSize
        , [Out()] out uint pBytesRead);

}

io mando i dati dall'oggetto MSTSC COM e controll ActiveX.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rdp.Server = "schamberlainvm";
        rdp.UserName = "TestAcct";
        IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
        secured.ClearTextPassword = "asdf";
        rdp.CreateVirtualChannels("TSCRED");
        rdp.Connect();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        rdp.SendOnVirtualChannel("TSCRED", "Hello World!");
    }
}
//Designer code
// 
// rdp
// 
this.rdp.Enabled = true;
this.rdp.Location = new System.Drawing.Point(12, 12);
this.rdp.Name = "rdp";
this.rdp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("rdp.OcxState")));
this.rdp.Size = new System.Drawing.Size(1092, 580);
this.rdp.TabIndex = 0;

Sto ottenendo un execption ogni volta che viene eseguito NativeMethods.WTSVirtualChannelRead

Qualsiasi aiuto su questo sarebbe molto apprezzato.

Modifica - mHandle ha un valore diverso da zero quando viene eseguita la funzione. codice aggiornato per aggiungere quel controllo.

EDIT2 - ho usato il P / Invoke Interop Assistente e generato un nuovo sigiture

    [DllImport("Wtsapi32.dll", EntryPoint = "WTSVirtualChannelRead")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSVirtualChannelRead(
        [In()] System.IntPtr hChannelHandle
        , uint TimeOut
        , [Out()] [MarshalAs(UnmanagedType.LPStr)] 
          StringBuilder Buffer
        , uint BufferSize
        , [Out()] out uint pBytesRead);

ora riceve la stringa di testo (Sì!) Ma ottiene solo la prima lettera della mia stringa di prova (Boo!). Tutte le idee su ciò che sta andando male?

EDIT 3 --- Dopo la chiamata che dovrebbe di leggere il mondo ciao;

  

BytesRead = 24

     

Buffer.Length = 1; Buffer.Capacity = 16; Buffer.m_StringValue = "H";

È stato utile?

Soluzione

Bene, il problema è che si sta inviando una stringa 16bit unicode nel lato l'invio e la lettura di una stringa ANSI dall'altra in modo che il livello di smistamento sta terminando il buffer di stringa al primo carattere NUL. Si potrebbe o cambiando la UnmanagedType.LPStr a UnmanagedType.LPWStr o maresciallo come un array di byte e poi convertire in una stringa utilizzando una classe di Unicode codifica.

Qualcosa di simile potrebbe funzionare (NOTA: codice non testato come non ho un server per test):

public static extern int WTSVirtualChannelRead(IntPtr hChannel, 
        uint Timeout, 
        [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] byte[] Buffer, 
        uint BufferSize, 
        out uint BytesRead);

string DoRead(IntPtr hChannel)
{ 
    byte[] buf = new byte[1024];
    uint bytesRead;
    if (WTSVirtualChannelRead(hChannel, 0, buf, (uint)buf.Length, out bytesRead) != 0)
    {
        return Encoding.Unicode.GetString(buf, 0, (int)bytesRead);
    }
    else
    {
        return "";
    }
}

Altri suggerimenti

mi sento come fare la doccia dopo aver scritto questo, ma ...

private void button1_Click(object sender, EventArgs e)
{
    uint bufferSize = 2;
    StringBuilder buffer = new StringBuilder();
    StringBuilder final = new StringBuilder();
    uint bytesRead;
    NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
    while (bytesRead != 0)
    {
        final.Append(buffer);
        NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
    }
        MessageBox.Show("Got data: " + final.ToString());
}

Se qualcun altro in grado di fornire una soluzione migliore al problema che trasmette solo un personaggio che sarà lieto di accettare il fatto che al posto di questo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top