Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2643544
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-10-20 17:06:04

参考:
由于要做QProcess与dos exe程序通信,一直处理摸索中,下面程序基本解决,在dos下要满足dos程序首先是个控制台程序才启作用

// sample.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
//
//
//VOID ErrorExit (LPTSTR lpszMessage);
//
//int main()
//{
// int size=1024;
// char chBuf[1024];
// DWORD retval;
// DWORD dwRead, dwWritten;
//
// HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
// HANDLE hOutputFile = CreateFile( "d:/dev/vc/003Test/pipeChild/pipeChild/sample.out", GENERIC_READ|GENERIC_WRITE,
//  FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//
// fprintf(stdout, "stdout world a=%d, b=%d\t", 8, 8);
// fflush(stdout);
// sprintf(chBuf,  "file cmd a=%d, b=%d\n", 1, 2);
// WriteFile( hOutputFile, chBuf, strlen(chBuf), &dwWritten, NULL) ;
// while(1)// (int n=0; n<50; n++)
// {
///*  if(!PeekNamedPipe(hStdin, NULL, size, &retval, NULL, NULL) || !retval){
//   //sprintf(chBuf,  "PeekNamedPipe quit\n");
//   //WriteFile( hOutputFile, chBuf, strlen(chBuf), &dwWritten, NULL) ;
//   Sleep(1000);
//   continue;
//  }
//  if(retval > size)retval=size;
//  ReadFile(stdin, chBuf, retval, NULL, NULL);
//*/
//
//  if (!ReadFile(stdin, chBuf, size, NULL, NULL))
//  {
//   Sleep(1000);
//   continue;
//  }
//
//  BOOL fSuccess = WriteFile( hOutputFile, chBuf, strlen(chBuf), &dwWritten, NULL) ;
//  Sleep(1000);
//  break;
// }
//
// CloseHandle(hOutputFile);
//
// if(retval)return retval;
// return -1;
//
//}
//
//VOID ErrorExit (LPTSTR lpszMessage)
//{
// MessageBox( 0, lpszMessage, 0, 0 );
//}
#include
#include
#include
#include
#include
#define    Ctrl(x)     ((x) & 0x37)
//typedef struct _CONSOLE_READCONSOLE_CONTROL {
// IN ULONG nLength;           // sizeof( CONSOLE_READCONSOLE_CONTROL )
// IN ULONG nInitialChars;
// IN ULONG dwCtrlWakeupMask;
// OUT ULONG dwControlKeyState;
//} CONSOLE_READCONSOLE_CONTROL,

int main (void)
{
 HANDLE  hInput, hOutput;
 WCHAR   buf [0x100];
 char   sbuf [0x100];
 CONSOLE_READCONSOLE_CONTROL  param;
 setlocale (LC_ALL, ".ACP");
 memset (¶m, 0, sizeof (param));
 param.nLength = sizeof (param);
 hInput       = GetStdHandle (STD_INPUT_HANDLE);
 hOutput    = GetStdHandle (STD_OUTPUT_HANDLE);
 buf [0] = 0;
 while (wcscmp (buf, L"quit") != 0)
 {
  DWORD read, written;
  printf ("\n$");
  param.nLength      = 0;
  param.nInitialChars    = 0;
  //  我们使用^F和^D来进行自动补齐
  param.dwCtrlWakeupMask  = (1 << Ctrl ('F')) | (1 << Ctrl ('D'));
again:
  if (!ReadFile(hInput, sbuf, 0x100, &read, NULL))
  {
   Sleep(1000);
   continue;
  }
  if (sbuf [read-1] == '\n')
   --read;
  if (sbuf [read-1] == '\r')
   --read;
  sbuf [read] = 0;
  fprintf (stdout, "test inputed: [%s]\n", sbuf);
  fflush(stdout);
  if (ReadConsoleW (hInput, buf, 0x100, &read, ¶m))
  {
   if (buf [read-1] == Ctrl ('F'))
   {    //     用户按下了^F键, 自动补齐字符串"fff",
    //     然后继续等待输入
    wcscpy (buf+read-1, L"fff");
    WriteConsoleW (hOutput, L"fff", 3, &written, NULL);
    param.nLength = read - 1 + 3;
    goto again;
   }
   else if (buf [read-1] == Ctrl ('D'))
   {    //     用户按下了^D键, 自动补齐字符串"ddd",
    //     然后继续等待输入
    wcscpy (buf+read-1, L"ddd");
    WriteConsoleW (hOutput, L"ddd", 3, &written, NULL);
    param.nLength = read-1+3;
    goto again;
   };

   //  去掉回车换行
   if (buf [read-1] == '\n')
    --read;
   if (buf [read-1] == '\r')
    --read;
   buf [read] = 0;
   fprintf (stdout, "you inputed: [%S]\n", buf);
   fflush(stdout);
  }
  else
  {
   printf ("ReadConsole failed with error %d\n",
    GetLastError ());
   break;
  };
 };
};

再次测试,感觉与控制台程序没有关系,下面在与QProcess中通信也正常
#include "stdafx.h"
#include
#include
#include
#include
int main (void)
{
    char   sbuf [0x100];
    HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
    sbuf [0] = 0;
    while (strcmp (sbuf, "quit") != 0)
    {
        DWORD read;
        if (!ReadFile(hInput, sbuf, 0x100, &read, NULL))
        {
            Sleep(1000);
            continue;
        }
        if (sbuf [read-1] == '\n')
            --read;
        if (sbuf [read-1] == '\r')
            --read;
        sbuf [read] = 0;
        fprintf (stdout, "test inputed: [%s]\n", sbuf);
        fflush(stdout);
    };
};
阅读(1126) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~