SYD8821是具有全球领先低功耗(RX 2.4mA @-94.5dBm灵敏度,TX 4.3mA @0dBm输出功率)的蓝牙低功耗SOC芯片,在极低电流下实现了优异的射频性能,搭配176kB SRAM,512kB flash,非常适合中高阶可穿戴、智能家居、物联网等低功耗应用。具体可咨询:http://www.sydtek.com/
本文摘录于:https://blog.csdn.net/yzt33/article/details/44516493,这里只是作为一个我的一个抄录版,绝对没有盗用前辈知识的意图。
匿名管道通信——重定向CMD.EXE
匿名管道
匿名管道无非就是系统内核管理的一块内存区域,把这个文件的句柄分为读写,以程序使用。
实现原理
所以重定向cmd.exe的标准输入输出分别为两个匿名管道的输入输出即可。在GUI中对应接收和写入内容(不要搞错啦这里,否则就读不到正确的东西)。
程序大致过程
①、匿名管道创建
-
BOOL WINAPI CreatePipe(
-
_Out_ PHANDLE hReadPipe, //匿名管道读句柄
-
_Out_ PHANDLE hWritePipe, //匿名管道写句柄
-
_In_opt_ LPSECURITY_ATTRIBUTES lpPipeAttributes, //这里需要设置这个结构体(见下)
-
_In_ DWORD nSize //管道空间,设为0即默认大小
-
);
-
typedef struct _SECURITY_ATTRIBUTES {
-
DWORD nLength;
-
LPVOID lpSecurityDescriptor;
-
BOOL bInheritHandle;
-
} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
②、打开cmd.exe子进程
-
BOOL WINAPI CreateProcess(
-
_In_opt_ LPCTSTR lpApplicationName,//执行模块位置
-
_Inout_opt_ LPTSTR lpCommandLine,//执行模块传入命令
-
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
-
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
-
_In_ BOOL bInheritHandles,//继承句柄
-
_In_ DWORD dwCreationFlags,
-
_In_opt_ LPVOID lpEnvironment,
-
_In_opt_ LPCTSTR lpCurrentDirectory,
-
_In_ LPSTARTUPINFO lpStartupInfo,//启动信息(见下)
-
_Out_ LPPROCESS_INFORMATION lpProcessInformation//进程信息(为空即可)
-
);
-
typedef struct _STARTUPINFO {//此处只写本次索要用到的
-
DWORD cb;//大小
-
LPTSTR lpReserved;
-
LPTSTR lpDesktop;
-
LPTSTR lpTitle;
-
DWORD dwX;
-
DWORD dwY;
-
DWORD dwXSize;
-
DWORD dwYSize;
-
DWORD dwXCountChars;
-
DWORD dwYCountChars;
-
DWORD dwFillAttribute;
-
DWORD dwFlags;//STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW显示自己设置的模式
-
WORD wShowWindow;
-
WORD cbReserved2;
-
LPBYTE lpReserved2;
-
HANDLE hStdInput;//标准输入句柄
-
HANDLE hStdOutput;//标准输出句柄
-
HANDLE hStdError;//标准错误句柄
-
} STARTUPINFO, *LPSTARTUPINFO;
到这里其实已经都配置完了,无非是自己再写个多线程让UI和WORKER分开运行,读取和写入。值得提醒的是,对于cmd.exe的写入时,需要在字符串后+“\r\n”,刷新缓冲。这里把CreatePipe写出来,防止弄混:GUI读取句柄+CMD.EXE写入句柄,GUI写入句柄+CMD.EXE读取句柄,这样两对组成两个匿名管道。
源码下载链接
http://download.csdn.net/detail/yzt33/8521139
很喜欢编程,喜欢深入了解一切的实现,作为新人,希望前辈们多多指正,多多鼓励,谢谢。
这个例程是我测试过绝对可以跑起来的代码!
不过,该例程是C++语言编写的!
C#语言实现
C#也是可以实现DOC回显的,可看如下代码(摘录于:https://blog.csdn.net/windowsvipcuvs/article/details/38143939):
// 起初打算采用CreatePipe创建匿名管道来着,不过却无法得到int&hReadPipe返回参数
// 后来只有利用Net库自身来实现这个东西了
// 下面是一个CreatePipe函数参数的定义形式
-
namespace Pipe
-
{
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.IO;
-
using System.Threading;
-
using System.Diagnostics;
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
Thread Target = null;
-
string lastline = null;
-
Process Pipe = new Process();
-
delegate void SetTextCallBack(string text);
-
void Form1_Load(object sender, EventArgs e) {
-
CheckForIllegalCrossThreadCalls = true;
-
Pipe.StartInfo.FileName = "cmd";
-
Pipe.StartInfo.CreateNoWindow = true;
-
Pipe.StartInfo.UseShellExecute = false;
-
Pipe.StartInfo.RedirectStandardError = true;
-
Pipe.StartInfo.RedirectStandardInput = true;
-
Pipe.StartInfo.RedirectStandardOutput = true;
-
Pipe.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
-
Pipe.Start();
-
Pipe.StandardInput.WriteLine("");
-
Target = new Thread(new ThreadStart(this.target));
-
Target.Start();
-
}
-
void target() {
-
while (true) {
-
var line = Pipe.StandardOutput.ReadLine();
-
if (this.textBox1.InvokeRequired) {
-
this.BeginInvoke(new SetTextCallBack(this.SetText), new object[] { "\r\n" + line });
-
}
-
Thread.Sleep(50);
-
}
-
}
-
void SetText(string text) {
-
lock (this.textBox1) {
-
if (this.textBox1.TextLength <= 0) {
-
text = text.Replace("\r\n", null);
-
}
-
if (this.lastline != text.Substring(2)) {
-
this.textBox1.AppendText(text);
-
}
-
}
-
}
-
void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
-
if (e.KeyChar == (char)Keys.Enter) {
-
this.lastline = this.textBox1.Text.Substring(this.textBox1.GetFirstCharIndexOfCurrentLine());
-
var str = this.lastline.Substring(this.lastline.IndexOf('>') + 1);
-
Pipe.StandardInput.WriteLine(str);
-
e.Handled = true;
-
}
-
else if(e.KeyChar == (char)Keys.Back) {
-
var str = this.textBox1.Text.Substring(this.textBox1.GetFirstCharIndexOfCurrentLine());
-
if (str.Length > 1) {
-
if (str.Substring(str.Length - 1) == ">") {
-
e.Handled = true;
-
}
-
}
-
}
-
}
-
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
-
Target.Abort();
-
Pipe.Kill(); // Pipe.WaitForExit();
-
}
-
}