Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 830521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:03:26+00:00 2026-05-15T04:03:26+00:00

I appear to have a named pipes 101 issue. I have a very simple

  • 0

I appear to have a named pipes 101 issue. I have a very simple set up to connect a simplex named pipe transmitting from a C++ unmanaged app to a C# managed app. The pipe connects, but I cannot send a “message” through the pipe unless I close the handle which appears to flush the buffer and pass the message through. It’s like the message is blocked. I have tried reversing the roles of client/server and invoking them with different Flag combinations without any luck. I can easily send messages in the other direction from C# managed to C++ unmanaged. Does anyone have any insight. Can any of you guys successfully send messages from C++ unmanaged to C# managed? I can find plenty of examples of intra amanged or unmanaged pipes but not inter managed to/from unamanged – just claims to be able to do it.

In the listings, I have omitted much of the wrapper stuff for clarity. The key bits I believe that are relevant are the pipe connection/creation/read and write methods. Don’t worry too much about blocking/threading here.

C# Server side

    // This runs in its own thread and so it is OK to block
    private void ConnectToClient()
    {
        // This server will listen to the sending client
        if (m_InPipeStream == null)
        {
            m_InPipeStream =
                new NamedPipeServerStream("TestPipe", PipeDirection.In, 1);
        }

        // Wait for client to connect to our server
        m_InPipeStream.WaitForConnection();

        // Verify client is running
        if (!m_InPipeStream.IsConnected)
        {
            return;
        }

        // Start listening for messages on the client stream
        if (m_InPipeStream != null && m_InPipeStream.CanRead)
        {
            ReadThread = new Thread(new ParameterizedThreadStart(Read));
            ReadThread.Start(m_InPipeStream);
        }
    }


    // This runs in its own thread and so it is OK to block
    private void Read(object serverObj)
    {
        NamedPipeServerStream pipeStream = (NamedPipeServerStream)serverObj;
        using (StreamReader sr = new StreamReader(pipeStream))
        {
            while (true)
            {
                string buffer = "" ;
                try
                {
                    // Blocks here until the handle is closed by the client-side!!
                    buffer = sr.ReadLine();   // <<<<<<<<<<<<<<  Sticks here
                }
                catch
                {
                    // Read error
                    break;
                }

                // Client has disconnected?
                if (buffer == null || buffer.Length == 0)
                    break;

                // Fire message received event if message is non-empty
                if (MessageReceived != null && buffer != "")
                {
                    MessageReceived(buffer);
                }
            }
        }
    }

C++ client side

    // Static - running in its own thread.
    DWORD CNamedPipe::ListenForServer(LPVOID arg)
    {
        // The calling app (this) is passed as the parameter
        CNamedPipe* app = (CNamedPipe*)arg;

        // Out-Pipe: connect as a client to a waiting server
        app->m_hOutPipeHandle =
        CreateFile("\\\\.\\pipe\\TestPipe",
               GENERIC_WRITE,
               0,
               NULL,
               OPEN_EXISTING,
               FILE_ATTRIBUTE_NORMAL,
               NULL);
        // Could not create handle
        if (app->m_hInPipeHandle == NULL ||
            app->m_hInPipeHandle == INVALID_HANDLE_VALUE)
        {
            return 1;
        }

        return 0;
    }


    // Sends a message to the server
    BOOL CNamedPipe::SendMessage(CString message)
    {
    DWORD dwSent;

        if (m_hOutPipeHandle == NULL ||
            m_hOutPipeHandle == INVALID_HANDLE_VALUE)
        {
            return FALSE;
        }
        else
        {
            BOOL bOK =
                WriteFile(m_hOutPipeHandle,
                          message, message.GetLength()+1, &dwSent, NULL);
            //FlushFileBuffers(m_hOutPipeHandle);             // <<<<<<< Tried this
            return (!bOK || (message.GetLength()+1) != dwSent) ? FALSE : TRUE;
        }
    }


    // Somewhere in the Windows C++/MFC code...
    ...
    // This write is non-blocking. It just passes through having loaded the pipe.
    m_pNamedPipe->SendMessage("Hi de hi");
    ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:03:27+00:00Added an answer on May 15, 2026 at 4:03 am

    sr.ReadLine() expects to see a newline character(s) to know the end of the line. Because it receives neither new-line nor end-of-stream, it waits for more.
    Try:

    m_pNamedPipe->SendMessage("Hi de hi\n")
    

    or some of the sr.Read() methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem where I have to read from a named pipe. I
I have a very strange, very repeatable leak that doesn't appear to have anything
I have two WCF apps communicating one-way over named pipes. All is nice, except
I'm doing a simple game in Java. I have one Class named Drawer to
I have two named instances of SQL Server 2008 and am trying to set
I have a super simple python script as defined here import multiprocessing from multiprocessing
The .NET MemoryStream does not appear to have a .Reset or .Clear method. I
I'm developing an Android app in Eclipse Helios and I appear to have accidentally
I have the following HTML that can appear but only one at a time:
I have a form that i want to appear at the top of every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.