public void Disconnect() { IsConnected = false; if (reader != null) { reader.Close(); } var ret = WtsApi32.WTSVirtualChannelClose(mHandle); }
public void Connect() { mHandle = WtsApi32.WTSVirtualChannelOpen(IntPtr.Zero, -1, ChannelMessage.ChannelName); if (mHandle == IntPtr.Zero) { Log("RDP Virtual channel Open Failed: " + new Win32Exception().Message); return; } try { var stream = WtsApi32.WTSVirtualChannelQuery_WTSVirtualFileHandle(mHandle); reader = new BinaryReader(new BufferedStream(stream)); var len = reader.ReadInt32(); var buff = reader.ReadBytes(len); string result = System.Text.Encoding.Default.GetString(buff); } catch (Win32Exception ex) { Log("RDP Virtual channel Query Failed: " + ex.Message); return; } IsConnected = true; Action process = Process; process.BeginInvoke(process.EndInvoke, null); //process(); Action hello = () => { while (!SeenHello && IsConnected) { WriteMessage(new HelloRequest()); Thread.Sleep(200); } }; hello.BeginInvoke(hello.EndInvoke, null); //hello(); }
public bool WriteMessage(ChannelMessage msg) { var data = msg.ToByteArray(); int written; var ret = WtsApi32.WTSVirtualChannelWrite(mHandle, data, data.Length, out written); if (ret) { return(true); } var ex = new Win32Exception(); if (!SeenHello && ex.NativeErrorCode == 1 /* Incorrect Function */) { return(false); } Log("RDP Virtual channel Write Failed: " + ex.Message); return(false); }
public void Connect(KeyboardHookLib _keyboardHook) { mHandle = WtsApi32.WTSVirtualChannelOpen(IntPtr.Zero, -1, ChannelMessage.ChannelName); if (mHandle == IntPtr.Zero) { //Log("RDP Virtual channel Open Failed: " + new Win32Exception().Message); return; } try { byte[] buf = new byte[1024]; uint bytesRead; string text = ""; while (true) { if (WtsApi32.WTSVirtualChannelRead(mHandle, 0, buf, (uint)buf.Length, out bytesRead) != 0) { text = Encoding.Unicode.GetString(buf, 0, (int)bytesRead); } if (!string.IsNullOrEmpty(text)) { #region old /*string result = ""; * for (int i = 0; i < text.Length; i++) * { * if ((int)text[i] > 32 && (int)text[i] < 127) * { * result += text[i].ToString(); * } * else * { * result += string.Format("\\u{0:x4}", (int)text[i]); * } * } * * string tmpResult = new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( * result, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)));*/ /* //1、获取桌面窗口的句柄 * IntPtr desktopPtr = WinHelper.GetDesktopWindow(); * Write(desktopPtr.ToString() + ":" + GetHandleInfo(desktopPtr) + "\r\n"); * //2、获得一个子窗口(这通常是一个顶层窗口,当前活动的窗口) * IntPtr winPtr = WinHelper.GetWindow(desktopPtr, GetWindowCmd.GW_CHILD); * Write(winPtr.ToString() + ":" + GetHandleInfo(winPtr) + "\r\n"); * //3、循环取得桌面下的所有子窗口 * while (winPtr != IntPtr.Zero) * { * //4、继续获取下一个子窗口 * winPtr = WinHelper.GetWindow(winPtr, GetWindowCmd.GW_HWNDNEXT); * Write(winPtr.ToString() + ":" + GetHandleInfo(winPtr) + "\r\n"); * if (GetHandleInfo(winPtr).Contains("记事本")) * { * break; * } * }*/ #endregion if (text.Contains("使用服务端输入法")) { //解除键盘监控钩子 if (_keyboardHook != null) { _keyboardHook.UninstallHook(); } } else if (text.Contains("使用客户端输入法")) { } else { WinHelper.SendText(text, WinHelper.foreGroundHandle); } WinHelper.SetForegroundWindow(WinHelper.foreGroundHandle); } } } catch (Win32Exception ex) { Log("RDP Virtual channel Query Failed: " + ex.Message); return; } //IsConnected = true; //Action process = Process; //process.BeginInvoke(process.EndInvoke, null); ////process(); //Action hello = () => //{ // while (!SeenHello && IsConnected) // { // WriteMessage(new HelloRequest()); // Thread.Sleep(200); // } //}; //hello.BeginInvoke(hello.EndInvoke, null); ////hello(); }