C++ 什么';这个管道通信代码有什么问题?

C++ 什么';这个管道通信代码有什么问题?,c++,pipeline,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) {

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

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;
}
我试图解决这个问题。但我没发现出了什么问题? 请帮帮我。我很感谢你的帮助


谢谢。原因很简单。因为您仅以读取模式打开文件

请修改如下:

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 

我希望这会有帮助。

有什么问题吗?请具体说明,并包括错误消息或任何其他无法正常工作的信息。哪些内容无法正常工作?更具体一点,解释你期望的是什么,以及为什么你会期望。