标题:windows 匿名管道通信 返回结果不一样
取消只看楼主
feione66
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-5-12
结帖率:0
已结贴  问题点数:20 回复次数:0 
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 反应不全
搜索更多相关主题的帖子: windows 管道 sizeof NULL Flash 
2019-01-18 11:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492742-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.108877 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved