Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346200
  • 博文数量: 88
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-06 15:48
个人简介

喜欢美食, 旅行..

文章分类

全部博文(88)

文章存档

2014年(2)

2013年(12)

2012年(14)

2010年(8)

2009年(52)

我的朋友

分类: C/C++

2012-11-11 21:24:36

总的来讲也没有什么好讲的, msdn里面什么都有. 有兴趣的可以去查阅相关内容的更详细信息.
AIP列表:
CreatePipe
CreateProcess
PeekNamedPipe
ReadFile

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <memory>
  5. #include <ctime>
  6. using namespace std;
  7. #include "windows.h"
  8. int PipeTest()
  9. {
  10.     HANDLE hRead(NULL), hWrite(NULL);
  11.     SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES)};
  12.     sa.bInheritHandle = TRUE;
  13.     if ( !CreatePipe(&hRead, &hWrite, &sa, 0) )
  14.     {
  15.         cout << "create pipe failed." << endl;

  16.         CloseHandle(hWrite); hWrite = NULL;
  17.         CloseHandle(hRead); hRead = NULL;
  18.         return -1;
  19.     }

  20.     STARTUPINFO si = {sizeof(STARTUPINFO)};
  21.     PROCESS_INFORMATION pi = {0};
  22.     GetStartupInfo(&si);
  23.     si.hStdError = hWrite;
  24.     si.hStdOutput = hWrite;
  25.     si.dwFlags = STARTF_USESTDHANDLES;

  26.     wchar_t * strExecCmd = new wchar_t[1024];
  27.     {
  28.         std::auto_ptr<wchar_t> autoDelete(strExecCmd);
  29.         wchar_t file[1024] = L"D:\\src-project\\basicTest\\basicTest\\Test.exe";
  30.         memcpy(strExecCmd,file,sizeof(file));
  31.         if ( ! CreateProcess(
  32.             NULL, strExecCmd, NULL, NULL,
  33.             TRUE, CREATE_NO_WINDOW/*CREATE_NEW_CONSOLE*/, NULL, NULL, &si, &pi) )
  34.         {
  35.             cout << "create process failed." << endl;
  36.             CloseHandle(hWrite); hWrite = NULL;
  37.             CloseHandle(hRead); hRead = NULL;
  38.             return -1;
  39.         }
  40.         wcout << autoDelete.get() << endl;
  41.     }
  42.     wcout << strExecCmd << endl;

  43.     CloseHandle(hWrite); hWrite = NULL;

  44.     std::ofstream ofile("123.txt");
  45.     while (true)
  46.     {
  47.         // DWORD dwAvail = 0;
  48.         // if (!::PeekNamedPipe(hRead, NULL, 0, NULL, &dwAvail, NULL) || !dwAvail)
  49.         // {
  50.         // break;
  51.         // }

  52.         const DWORD BufferSize = 4096;
  53.         char pszBuffer[BufferSize] = {0};
  54.         DWORD bytesRead = 0;
  55.         if ( !ReadFile(hRead, pszBuffer, BufferSize, &bytesRead, NULL) )
  56.         {
  57.             cout << "read file failed." << endl;
  58.             break;
  59.         }
  60.         char * p = pszBuffer;
  61.         for ( int i = 0; i != bytesRead ; ++i ,++p )
  62.         {
  63.             ofile.put(*p);
  64.         }
  65.     }

  66.     CloseHandle(hWrite); hWrite = NULL;
  67.     CloseHandle(hRead); hRead = NULL;
  68.     return 0;
  69. }

阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~