XE版本 运行cmd命令,并取得输出字符

procedure CheckResult(b: Boolean);
begin
  if not b then
    raise Exception.Create(SysErrorMessage(GetLastError));
end;

function RunDOS(const CommandLine: string): string;
var
  HRead, HWrite: THandle;
  StartInfo: TStartupInfoA;
  ProceInfo: TProcessInformation;
  b: Boolean;
  sa: TSecurityAttributes;
  inS: THandleStream;
  sRet: TStrings;
begin
  Result := '';
  FillChar(sa, sizeof(sa), 0);
  // 设置允许继承,否则在NT和2000下无法取得输出结果
  sa.nLength := sizeof(sa);
  sa.bInheritHandle := True;
  sa.lpSecurityDescriptor := nil;
  b := CreatePipe(HRead, HWrite, @sa, 0);
  CheckResult(b);

  FillChar(StartInfo, sizeof(StartInfo), 0);
  StartInfo.cb := sizeof(StartInfo);
  StartInfo.wShowWindow := SW_HIDE;
  // 使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
  StartInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
  StartInfo.hStdError := HWrite;
  StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE); // HRead;
  StartInfo.hStdOutput := HWrite;

  b := CreateProcessA(nil, // lpApplicationName: PChar
    PAnsiChar(AnsiString(CommandLine)), // lpCommandLine: PAnsiChar
    nil, // lpProcessAttributes: PSecurityAttributes
    nil, // lpThreadAttributes: PSecurityAttributes
    True, // bInheritHandles: BOOL
    CREATE_NEW_CONSOLE, nil, nil, StartInfo, ProceInfo);

  CheckResult(b);
  WaitForSingleObject(ProceInfo.hProcess, INFINITE);

  inS := THandleStream.Create(HRead);
  if inS.Size > 0 then
  begin
    sRet := TStringList.Create;
    sRet.LoadFromStream(inS);
    Result := sRet.Text;
    sRet.Free;
  end;
  inS.Free;

  CloseHandle(HRead);
  CloseHandle(HWrite);
end;



测试

memo1.text := RunDOS('ping www.baidu.com');  


D7改为
b := CreateProcess(nil, //lpApplicationName: PChar
    PChar(CommandLine), //lpCommandLine: PChar
    nil, //lpProcessAttributes: PSecurityAttributes
    nil, //lpThreadAttributes: PSecurityAttributes
    True, //bInheritHandles: BOOL
    CREATE_NEW_CONSOLE,
    nil,
    nil,
    StartInfo,
    ProceInfo);


XE强制 AnsiChar 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在LCD1602上显示字符串和汉字,需要先初始化LCD1602,然后将字符和汉字转换为相应的ASCII码并发送到LCD1602的数据端口。以下是一个简单的例子: ```c #include <reg51.h> //定义LCD1602的I/O端口 sbit RS=P2^0; sbit RW=P2^1; sbit EN=P2^2; //向LCD发送一个指令 void LcdWriteCmd(unsigned char cmd) { RS=0; //选择命令模式 RW=0; //选择写入模式 EN=1; //打开使能 P0=cmd; //发送指令 EN=0; //关闭使能 } //向LCD发送一个字符 void LcdWriteData(unsigned char dat) { RS=1; //选择数据模式 RW=0; //选择写入模式 EN=1; //打开使能 P0=dat; //发送数据 EN=0; //关闭使能 } //初始化LCD1602 void InitLcd() { LcdWriteCmd(0x38); //设置16*2显示,5*7点阵,8位数据接口 LcdWriteCmd(0x0c); //设置显示开,光标关,闪烁关 LcdWriteCmd(0x06); //设置文字在显示器上的移动方向,每次写入后光标加1,整屏不移动 LcdWriteCmd(0x01); //清屏 } //显示字符串 void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str) { unsigned char i; if(y==0) LcdWriteCmd(0x80+x); //在第一行显示 else LcdWriteCmd(0xc0+x); //在第二行显示 for (i=0;str[i]!=0;i++) //逐个发送字符 { LcdWriteData(str[i]); } } //显示汉字 void LcdShowChinese(unsigned char x, unsigned char y, unsigned char *str) { unsigned char i; if(y==0) LcdWriteCmd(0x80+x); //在第一行显示 else LcdWriteCmd(0xc0+x); //在第二行显示 for (i=0;str[i]!=0;i+=2) //逐个发送汉字 { LcdWriteData(str[i]); //发送高字节 LcdWriteData(str[i+1]); //发送低字节 } } void main() { InitLcd(); //初始化LCD1602 LcdShowStr(0, 0, "Hello, world!"); //显示字符串 LcdShowChinese(0, 1, "\xba\xba\xd7\xd6\xce\xf6\xb2\xe5\xd0\xde\xcf\xf1"); //显示汉字 while(1); } ``` 在上面的代码中,`\xba\xba\xd7\xd6\xce\xf6\xb2\xe5\xd0\xde\xcf\xf1` 是“你好世界”的ASCII码,需要先将其转换为相应的汉字再发送到LCD1602上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值