Не происходит запись в именованный канал Windows

Пытаюсь создать именованный канал для чтения/записи, но без создания двух отдельных программ (сервера и клиента). Можно ли так сделать? В коде прохожу этап записи в канал (WriteFile), но на чтении из канала программа зависает, т.к. в канале 0 байт. С WinAPI никогда не сталкивался.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>

#define BUFSIZE 512

int main(void)
{
    BOOL fConnected = FALSE;
    HANDLE hPipeServer = INVALID_HANDLE_VALUE;
    LPCTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe");

    _tprintf(TEXT("\nPipe Server: %s\n"), lpszPipename);
    hPipeServer = CreateNamedPipe (
                lpszPipename,
                PIPE_ACCESS_DUPLEX,
                PIPE_TYPE_MESSAGE |
                PIPE_READMODE_MESSAGE |
                PIPE_WAIT,
                PIPE_UNLIMITED_INSTANCES,
                BUFSIZE,
                BUFSIZE,
                0,
                NULL);
    if (hPipeServer == INVALID_HANDLE_VALUE) {
        _tprintf(TEXT("CreateNamedPipe failed, GLE=%d.\n"), GetLastError());
        return -1;
    }

    HANDLE hPipeClient;
    LPTSTR lpvMessage = TEXT("Defaul message from client.");
    TCHAR chBuf[BUFSIZE];
    BOOL cbRead, cbWritten, cbToWrite, dwMode;
    BOOL fSuccess = FALSE;

    hPipeClient = CreateFile(
                      lpszPipename,
                      GENERIC_READ |
                      GENERIC_WRITE,
                      0,
                      NULL,
                      OPEN_EXISTING,
                      0,
                      NULL);

    if (hPipeClient == INVALID_HANDLE_VALUE) {
        printf("PipeClient invalid\n");
        return -1;
    }

    fConnected = ConnectNamedPipe(hPipeServer, NULL) ?
                     TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

    if (fConnected) {
        printf("Client connected\n");

        cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof (TCHAR);
        _tprintf(TEXT("Sending %d byte message to server: \"%s\"\n"), cbToWrite, lpvMessage);

        fSuccess = WriteFile(
                       hPipeClient,
                       lpvMessage,
                       cbToWrite,
                       &cbWritten,
                       NULL);

        if (!fSuccess) {
            _tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
            return -1;
        }

        printf("\nMessage sent to server, receiving reply as follows:\n");

        do
        {
        // Read from the pipe.

           fSuccess = ReadFile(
              hPipeClient,    // pipe handle
              chBuf,    // buffer to receive reply
              BUFSIZE*sizeof(TCHAR),  // size of buffer
              &cbRead,  // number of bytes read
              NULL);    // not overlapped

           if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
              break;

           _tprintf( TEXT("\"%s\"\n"), chBuf );
        } while ( ! fSuccess);  // repeat loop if ERROR_MORE_DATA

        if (!fSuccess) {
            _tprintf(TEXT("ReadFile rom pipe failed. GLE=%d\n"), GetLastError());
            return -1;
        }

        printf("\n<End of message, press ENTER to terminate connection and exit>");
        _getch();

    }

    CloseHandle(hPipeClient);
    CloseHandle(hPipeServer);

    return 0;
}

Ориентировался на примеры из https://docs.microsoft.com/ru-ru/windows/win32/ipc/using-pipes

Вывод программы следующий Вывод программы


Ответы (0 шт):