title: Windows: Check existence of named pipe
body: <p>I am writing a C++ program that makes use of named pipes on Windows. I can create and work with them quite fine. The only piece missing to the puzzle is a function to check for a pipe's existence.</p>
  <p>Coming from the Unix world I originally tried <code>std::filesystem::exists(&quot;\\\\.\\pipe\\myPipe&quot;)</code> but this is not reliable and often errors with <code>ERROR_PIPE_BUSY</code>.</p>
  <p>While searching for an alternative way to check for a pipe's existence, I stumbled upon <a href="https://github.com/klemens-morgenstern/boost-process/issues/4" rel="nofollow noreferrer">this issue</a> on GitHub (Boost process) and from there I take it that Boos process circumvents the problem by using a special naming scheme and a counter and then keeping track of that internally (only seems to work for pipes created via Boost process though).</p>
  <p>Furthermore according to <a href="https://stackoverflow.com/questions/258701/how-can-i-get-a-list-of-all-open-named-pipes-in-windows">How can I get a list of all open named pipes in Windows?</a> it seems that there are ways to list the existing named pipes. These solutions are not using C++ though and I did not find a way to port that over.</p>
  <p>After having read the <a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea" rel="nofollow noreferrer">documentation of CreateNamedPipe</a>, I now assembled the following solution to my problem:</p>
  <pre><code>bool NamedPipe::exists(const std::filesystem::path &amp;pipePath) {
      if (pipePath.parent_path() != &quot;\\\\.\\pipe&quot;) {
          // This can't be a pipe, so it also can't exist
          return false;
      }
  
      // Attempt to create a pipe with FILE_FLAG_FIRST_INSTANCE so that the creation will fail
      // if the pipe already exists
      HANDLE pipeHandle = CreateNamedPipe(pipePath.string().c_str(),
                                          PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE,
                                          PIPE_TYPE_BYTE | PIPE_WAIT,
                                          1,   // # of allowed pipe instances
                                          0,   // Size of outbound buffer
                                          0,   // Size of inbound buffer
                                          0,   // Use default wait time
                                          NULL // Use default security attributes
      );
  
      if (pipeHandle == INVALID_HANDLE_VALUE) {
          // Creation has failed
          // It has failed (most likely) due to there alredy existing a pipe with
          // that name
          return true;
      } else {
          // The creation has succeeded
          if(!CloseHandle(pipeHandle)) {
              throw PipeException&lt; DWORD &gt;(GetLastError(), &quot;CheckExistance&quot;);
          }
  
          return false;
      }
  }
  </code></pre>
  <p>However attempting to create a named pipe only to check whether there already exists one with that name already seems like a lot of unnecessary overhead. Furthermore I am unsure of whether this solution is universally applicable or only works if the pipe tested for was also created with <code>FILE_FLAG_FIRST_PIPE_INSTANCE</code>.</p>
  <p>Therefore my question is: Is there a better way to check whether a named pipe with the given name already exists in Windows?</p>