windows 匿名管道通信 返回结果不一样
代码如下:
程序代码:#include <iostream>
#include <windows.h>
#include <Shlwapi.h>
using namespace std;
#define BUFSIZE 10960
int main()
{
//变量声明
DWORD dwRead = 0;
DWORD dwAvail = 0;
DWORD len = 0;
char cbBuf[BUFSIZE] = { 0 };
//初始化
SECURITY_ATTRIBUTES sa1 = { 0 };
sa1.nLength = sizeof(SECURITY_ATTRIBUTES);
sa1.lpSecurityDescriptor = NULL;
sa1.bInheritHandle = TRUE;
//创建管道
HANDLE hReadPipe1 = NULL;
HANDLE hWritePipe1 = NULL;
CreatePipe(&hReadPipe1, &hWritePipe1, &sa1, 4048);
//重定向
STARTUPINFO si = { 0 };
::ZeroMemory(&si, sizeof(si));
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = si.hStdError = hWritePipe1; //输出由标准输出 -> 输出到管道1
si.cb = sizeof(si);
//创建子进程
PROCESS_INFORMATION pi = { 0 };
memset(&pi, 0, sizeof(PPROCESS_INFORMATION));
char *pCommandLine = new TCHAR[0x200];
//char szPath[] = "E:\\firerw\\Flash Programmer\\bin\\SmartRFProgConsole.exe X";
char szPath[] = "E:\\firerw\\Flash Programmer\\bin\\SmartRFProgConsole.exe S EPV F=\"D:\\Coordinator.hex\"";
memset(pCommandLine, 0, sizeof(szPath));
lstrcpy(pCommandLine, szPath);
if (!CreateProcess(NULL, pCommandLine, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
{
if (pCommandLine) {
delete pCommandLine;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hWritePipe1);
return 1;
}
//取出管道数据
Sleep(1000); //进程同步的问题
while (TRUE)
{
PeekNamedPipe(hReadPipe1, cbBuf, BUFSIZE, &dwRead, &dwAvail, NULL);
if (dwRead)
{
memset(cbBuf, 0, sizeof(cbBuf));
ReadFile(hReadPipe1, cbBuf, BUFSIZE, &dwRead, NULL);
puts(cbBuf);
break;
}
}
if (pCommandLine) {
delete pCommandLine;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hWritePipe1);
system("pause");
}每次编译的结果不同:
1:
程序代码:Texas Instruments SmartRF Flash Programmer v1.13.7-no-mfc --------------------------------------------------------- 1313: Erasing entire flash... 1313: Programming flash......................................................
2:
程序代码:Texas Instruments SmartRF Flash Programmer v1.13.7-no-mfc --------------------------------------------------------- Could not access the hardware (it is possibly in use by other application) 请按任意键继续. . .
每次执行 反应的内容都不一样
程序代码:PeekNamedPipe(hReadPipe1, cbBuf, BUFSIZE, &dwRead, &dwAvail, NULL);
if (dwRead)
{
memset(cbBuf, 0, sizeof(cbBuf));
ReadFile(hReadPipe1, cbBuf, BUFSIZE, &dwRead, NULL);
puts(cbBuf);
break;以上代码中的 dwread 和 dwavail 反应不全




