// The read method, reads data from the virtual channel private void read(out byte[] data) { byte[] readInData = new byte[1536]; GCHandle pinned = GCHandle.Alloc(readInData, GCHandleType.Pinned); IntPtr address = pinned.AddrOfPinnedObject(); uint bytesread = 0; int returnValue = WTSapi32.WTSVirtualChannelRead(handleFenrir, int.MaxValue, readInData, 1536, out bytesread); pinned.Free(); byte[] correct = new byte[bytesread]; Array.Copy(readInData, correct, bytesread); data = correct; Array.Clear(readInData, 0, readInData.Length); Application.DoEvents(); }
public void sendToVC(String line, IntPtr handle) { byte[] bBeginning = System.Text.Encoding.Unicode.GetBytes("Start of Request"); int bytesforBeginning = 0; WTSapi32.WTSVirtualChannelWrite(handle, bBeginning, bBeginning.Length, ref bytesforBeginning); int incomingOffset = 0; byte[] bData = System.Text.Encoding.Unicode.GetBytes(line); byte[] bChunk = new byte[1024]; while (incomingOffset < bData.Length) { int length = Math.Min(bChunk.Length, bData.Length - incomingOffset); Buffer.BlockCopy(bData, incomingOffset, bChunk, 0, length); incomingOffset += length; // Transmit outbound buffer try { //neeed to split this down to less than 1.5k byte[] title; read(out title); string sReceived = ""; foreach (byte b in title) { if (b != 0) { char c = Convert.ToChar(b); sReceived += c; } else { } } if (sReceived == "Received") { int bytesWritten = 0; WTSapi32.WTSVirtualChannelWrite(handle, bChunk, bChunk.Length, ref bytesWritten); Array.Clear(bChunk, 0, bChunk.Length); } } catch { Console.WriteLine("Error" + Environment.NewLine); } } byte[] bTerminator = System.Text.Encoding.Unicode.GetBytes("End of Request"); int bytes = 0; WTSapi32.WTSVirtualChannelWrite(handle, bTerminator, bTerminator.Length, ref bytes); //The read section to retrieve the response is below here!!!!!!! sFullResponse = ""; while (true) { try { byte[] bResponse; read(out bResponse); string sResponse = ""; foreach (byte b in bResponse) { if (b != 0) { char c = Convert.ToChar(b); sResponse += c; } else { } } if (sResponse == "End of Response") { break; } else if (sResponse == "Received") { //do nothing } else { sFullResponse += sResponse; } } catch { Console.WriteLine("Error" + Environment.NewLine); } } }