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

What's wrong with this pipeline communication code?

本文关键字:什么 问题 代码 管道 通信      更新时间:2023-10-16

我试图在我的代码中实现管道通信。构建成功,但我的代码不工作。我的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;
}

我试图解决这个问题。但我没发现怎么了?请帮帮我。我很感激你的帮助。

谢谢。

原因很简单。因为你打开的文件只有读模式

请这样修改:

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