title: Connection to named pipe
body: <p>I tried using named pipes in C++ to transfer a message from one computer to another. But the client could not connect to the server, it returned error 1326 (invalid name or password), although the server name was specified correctly.</p>
  
  <p>What does the password have to do with the channel? And if there are no errors in the code, then what else could be causing the connection error to the channel?</p>
  
  <p>Also, on the Internet, I saw such a form recording the channel name <code>\\servername\pipe\[path] pipename</code>. <code>servername</code> is the name of the server, <code>pipe</code> determines that it is a channel, and <code>pipename</code> is the name of the channel, but what is <code>[path]</code>?</p>
  
  <p>Server</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  int main()
  {
      HANDLE hNamedPipe;
      DWORD dwBytesRead; 
      DWORD dwBytesWrite; 
      char pchMessage[80];
      int nMessageLength;
  
      PSECURITY_DESCRIPTOR psd;
      SECURITY_ATTRIBUTES sa;
      psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
      InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
      SetSecurityDescriptorDacl(psd, true, NULL, false);
      sa.nLength = sizeof(sa);
      sa.bInheritHandle = true;
      sa.lpSecurityDescriptor = psd;
  
      hNamedPipe= CreateNamedPipe("\\\\.\\pipe\\pipe", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | WRITE_OWNER, PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE| PIPE_ACCEPT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, 2048, 2048, INFINITE, &amp;sa);
      if (hNamedPipe == INVALID_HANDLE_VALUE)
      {
              cerr &lt;&lt; "Create named pipe failed." &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
  
          return 0;
      }
      cout &lt;&lt; "The server is waiting for connection with a client." &lt;&lt; endl;
      if (!ConnectNamedPipe(
          hNamedPipe,
          NULL 
      ))
      {
          cerr &lt;&lt; "Connect named pipe failed." &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          CloseHandle(hNamedPipe);
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
  
          return 0;
      }
      if (!ReadFile(
          hNamedPipe,
          pchMessage,
          sizeof(pchMessage),
          &amp;dwBytesRead, 
          NULL))
      {
          cerr &lt;&lt; "Data reading from the named pipe failed." &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          CloseHandle(hNamedPipe);
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
  
          return 0;
      }
  
      cout &lt;&lt; "The server received the message from a client: "
          &lt;&lt; endl &lt;&lt; '\t' &lt;&lt; pchMessage &lt;&lt; endl;
  
      cout &lt;&lt; "Input a string: ";
      cin.getline(pchMessage, 80);
      nMessageLength = strlen(pchMessage) + 1;
  
          if (!WriteFile(
              hNamedPipe,
              pchMessage,
              nMessageLength,
              &amp;dwBytesWrite,
              NULL
          ))
          {
              cerr &lt;&lt; "Write file failed." &lt;&lt; endl
                  &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
              CloseHandle(hNamedPipe);
              cout &lt;&lt; "Press any key to exit.";
              cin.get();
  
              return 0;
          }
  
      cout &lt;&lt; "The server sent the message to a client: "
          &lt;&lt; endl &lt;&lt; '\t' &lt;&lt; pchMessage &lt;&lt; endl;
  
      CloseHandle(hNamedPipe);
      cout &lt;&lt; "Press any key to exit.";
      cin.get();
  
      return 0;
  }
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main()
  {
      string machineName;
      getline(cin, machineName, '\n');
      machineName= "\\\\"+machineName+"\\pipe\\pipe";
      cout &lt;&lt; machineName &lt;&lt; endl;
      char pipeName[80];
      strcpy_s(pipeName, machineName.c_str());
      HANDLE hNamedPipe;
      DWORD dwBytesWritten;
      DWORD dwBytesRead;
      char pchMessage[80];
      int nMessageLength;
  
      hNamedPipe = CreateFile(
          pipeName, // имя канала
          GENERIC_READ | GENERIC_WRITE, 
          FILE_SHARE_READ | FILE_SHARE_WRITE, 
          NULL, 
          OPEN_EXISTING, 
          FILE_ATTRIBUTE_NORMAL, 
          NULL);
  
      if (hNamedPipe == INVALID_HANDLE_VALUE)
      {
          cerr &lt;&lt; "Connection with the named pipe failed." &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
          return 0;
      }
      cin.get();
      cout &lt;&lt; "Input a string: ";
      cin.getline(pchMessage, 80);
      nMessageLength = strlen(pchMessage) + 1;
      if (!WriteFile(
          hNamedPipe, 
          pchMessage, 
          nMessageLength,
          &amp;dwBytesWritten,
          NULL)) 
      {
              cerr &lt;&lt; "Write file failed: " &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          CloseHandle(hNamedPipe);
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
          return 0;
      }
          cout &lt;&lt; "The client sent the message to a server: "
          &lt;&lt; endl &lt;&lt; '\t' &lt;&lt; pchMessage &lt;&lt; endl;
      if (!ReadFile(
          hNamedPipe, 
          pchMessage,
          sizeof(pchMessage), 
          &amp;dwBytesRead,
          NULL))
      {
          cerr &lt;&lt; "Read file failed: " &lt;&lt; endl
              &lt;&lt; "The last error code: " &lt;&lt; GetLastError() &lt;&lt; endl;
          CloseHandle(hNamedPipe);
          cout &lt;&lt; "Press any key to exit.";
          cin.get();
          return 0;
      }
      cout &lt;&lt; "The client received the message from a server: "
          &lt;&lt; endl &lt;&lt; '\t' &lt;&lt; pchMessage &lt;&lt; endl;
      CloseHandle(hNamedPipe);
      cout &lt;&lt; "Press any key to exit.";
      cin.get();
      return 0;
  }
  </code></pre>