总的来讲也没有什么好讲的, msdn里面什么都有. 有兴趣的可以去查阅相关内容的更详细信息.
AIP列表:
CreatePipe
CreateProcess
PeekNamedPipe
ReadFile
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <memory>
- #include <ctime>
- using namespace std;
- #include "windows.h"
- int PipeTest()
- {
- HANDLE hRead(NULL), hWrite(NULL);
- SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES)};
- sa.bInheritHandle = TRUE;
- if ( !CreatePipe(&hRead, &hWrite, &sa, 0) )
- {
- cout << "create pipe failed." << endl;
- CloseHandle(hWrite); hWrite = NULL;
- CloseHandle(hRead); hRead = NULL;
- return -1;
- }
- STARTUPINFO si = {sizeof(STARTUPINFO)};
- PROCESS_INFORMATION pi = {0};
- GetStartupInfo(&si);
- si.hStdError = hWrite;
- si.hStdOutput = hWrite;
- si.dwFlags = STARTF_USESTDHANDLES;
- wchar_t * strExecCmd = new wchar_t[1024];
- {
- std::auto_ptr<wchar_t> autoDelete(strExecCmd);
- wchar_t file[1024] = L"D:\\src-project\\basicTest\\basicTest\\Test.exe";
- memcpy(strExecCmd,file,sizeof(file));
- if ( ! CreateProcess(
- NULL, strExecCmd, NULL, NULL,
- TRUE, CREATE_NO_WINDOW/*CREATE_NEW_CONSOLE*/, NULL, NULL, &si, &pi) )
- {
- cout << "create process failed." << endl;
- CloseHandle(hWrite); hWrite = NULL;
- CloseHandle(hRead); hRead = NULL;
- return -1;
- }
- wcout << autoDelete.get() << endl;
- }
- wcout << strExecCmd << endl;
- CloseHandle(hWrite); hWrite = NULL;
- std::ofstream ofile("123.txt");
- while (true)
- {
- // DWORD dwAvail = 0;
- // if (!::PeekNamedPipe(hRead, NULL, 0, NULL, &dwAvail, NULL) || !dwAvail)
- // {
- // break;
- // }
- const DWORD BufferSize = 4096;
- char pszBuffer[BufferSize] = {0};
- DWORD bytesRead = 0;
- if ( !ReadFile(hRead, pszBuffer, BufferSize, &bytesRead, NULL) )
- {
- cout << "read file failed." << endl;
- break;
- }
- char * p = pszBuffer;
- for ( int i = 0; i != bytesRead ; ++i ,++p )
- {
- ofile.put(*p);
- }
- }
- CloseHandle(hWrite); hWrite = NULL;
- CloseHandle(hRead); hRead = NULL;
- return 0;
- }
阅读(1322) | 评论(0) | 转发(0) |