这个管道通信代码有什么问题?

时间:2016-09-03 07:51:37

标签: c++ pipeline

我正在尝试使用管道通信在我的代码中实现。 建立成功,但我的代码不起作用。 我的管道通信代码是:

int CTssPipeClient::Start(VOID) 
{ 
    m_uThreadId = 0;
    m_hThread = (HANDLE)_beginthreadex(NULL, 0, ProcessPipe, LPVOID(this), 0, &m_uThreadId);
    return 1; 
}

VOID CTssPipeClient::Stop(VOID) 
{ 
    m_bRunning = FALSE;
    if(m_hThread)
    {
        WaitForSingleObject(m_hThread, INFINITE);
        CloseHandle(m_hThread);
    }
    if(m_hPipe)
    {
        CloseHandle(m_hPipe); 
    }
}

UINT CTssPipeClient::Run()
{
    BOOL fSuccess; 
    DWORD dwMode; 
    LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\tssnamedpipe"); 

    m_vMsgList.clear();
    m_bRunning = TRUE;
    // Try to open a named pipe; wait for it, if necessary. 
    while (m_bRunning) 
    { 
        m_hPipe = CreateFile( 
            lpszPipename,   // pipe name 
            GENERIC_READ   // read and write access 
            0,              // no sharing 
            NULL,           // default security attributes
            OPEN_EXISTING,  // opens existing pipe 
            0,              // default attributes 
            NULL);          // no template file 

        // Break if the pipe handle is valid.
        if (m_hPipe == INVALID_HANDLE_VALUE) 
        {
            Sleep(1000);
            continue; 
        }
        dwMode = PIPE_READMODE_MESSAGE; 
        fSuccess = SetNamedPipeHandleState( 
            m_hPipe,    // pipe handle 
            &dwMode,  // new pipe mode 
            NULL,     // don't set maximum bytes 
            NULL);    // don't set maximum time 
        if (!fSuccess) 
        {
            continue;
        }
        while(fSuccess)
        {
            if(m_vMsgList.size() > 0)
            {
                DWORD cbWritten; 
                // Send a message to the pipe server. 

                fSuccess = WriteFile( 
                    m_hPipe,                  // pipe handle 
                    m_vMsgList[0].c_str(),                    // message 
                    (m_vMsgList[0].length() + 1)*sizeof(TCHAR), // message length 
                    &cbWritten,             // bytes written 
                    NULL);                  // not overlapped 
                m_vMsgList.erase(m_vMsgList.begin());
                if (!fSuccess) 
                {
                    break;
                }
            }
            Sleep(200);
        }
        CloseHandle(m_hPipe);
    }
    _endthreadex(0);
    return 0;
}

DWORD CTssPipeClient::WriteMsg(LPCTSTR szMsg)
{
    if(!m_bRunning)
    {
        Start();
    }
    wstring wstr(szMsg);
    m_vMsgList.push_back(wstr);
    return 0;
}

我试过修复这个问题。但我没有发现什么错? 请帮我。我很感谢你的帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

原因很简单。因为您只打开一个只读模式的文件。

请修改如下:

m_hPipe = CreateFile( 
            lpszPipename,   // pipe name 
            GENERIC_READ |  // read and write access 
            GENERIC_WRITE, 
            0,              // no sharing 
            NULL,           // default security attributes
            OPEN_EXISTING,  // opens existing pipe 
            0,              // default attributes 
            NULL);          // no template file 

我希望有所帮助。