title: Named pipe and "GetNamedPipeHandleState"
body: <p>If I run pipe client for testing <code>GetNamedPipeHandleState</code> function, I get error <code>Error 87: The parameter is incorrect</code>.</p>
  
  <p>Here my code, with client and server:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include "windows.h"
  
  using namespace std;
  
  void showError(const char *text) {
      char Buf[256];
      DWORD errNo = GetLastError();
      FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), Buf,
                    sizeof(Buf), NULL);
      fprintf(stdout, "%s, error %d, %s\nPress any key to Exit", text, (int) errNo, Buf);
      _getch();
  }
  
  int client_GetNamedPipeHandleState() {
  
      DWORD dwState;
      DWORD dwCurInstances;
      DWORD dwMaxCollectionCount;
      DWORD dwCollectDataTimeout;
      TCHAR chUserName[255];
  
      char pipeName[] = "\\\\.\\pipe\\demo_pipe";
      HANDLE hNamedPipe;
  
      hNamedPipe = CreateFileA(pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                               NULL);
  
      if (hNamedPipe == INVALID_HANDLE_VALUE) {
          showError("Create File failed");
          return 0;
      }
  
      if (!GetNamedPipeHandleState(
          hNamedPipe, &amp;dwState, &amp;dwCurInstances, &amp;dwMaxCollectionCount,&amp;dwCollectDataTimeout, chUserName, 255)) {
          //hNamedPipe, &amp;dwState, &amp;dwCurInstances, NULL, NULL, NULL, 0)) {
          CloseHandle(hNamedPipe);
          showError("get state failed");
          return 0;
      }
  
      cout &lt;&lt; "State: " &lt;&lt; dwState &lt;&lt; ", ";
      switch (dwState) {
          case (PIPE_NOWAIT):
              cout &lt;&lt; "PIPE_NOWAIT" &lt;&lt; endl;
              break;
          case (PIPE_READMODE_MESSAGE):
              cout &lt;&lt; "PIPE_READMODE_MESSAGE" &lt;&lt; endl;
              break;
          case (PIPE_NOWAIT | PIPE_READMODE_MESSAGE):
              cout &lt;&lt; "PIPE_NOWAIT and PIPE_READMODE_MESSAGE" &lt;&lt; endl;
              break;
          default:
              cout &lt;&lt; "Unknown state." &lt;&lt; endl;
              break;
      }
  
      cout &lt;&lt; "Current instances: " &lt;&lt; dwCurInstances &lt;&lt; endl
      &lt;&lt; "Max collection count: " &lt;&lt; dwMaxCollectionCount &lt;&lt; endl
      &lt;&lt; "Collection data timeout: " &lt;&lt; dwCollectDataTimeout &lt;&lt; endl
      &lt;&lt; "User name: " &lt;&lt; chUserName &lt;&lt; endl;
  
      CloseHandle(hNamedPipe);
      cout &lt;&lt; "Press any key to exit.";
      cin.get();
      return 0;
  }
  
  int server() {
      HANDLE hNamedPipe;
      DWORD dwBytesRead;
      DWORD dwBytesWrite;
      char pchMessage[80];
      DWORD nMessageLength;
  
      hNamedPipe = CreateNamedPipeA(
              "\\\\.\\pipe\\demo_pipe",
              PIPE_ACCESS_DUPLEX,
              PIPE_TYPE_MESSAGE | PIPE_WAIT,
              1,
              0,
              0,
              INFINITE,
              NULL
      );
  
      if (hNamedPipe == INVALID_HANDLE_VALUE) {
          showError("Create named pipe failed");
          return 0;
      }
  
      printf("Waiting client...\n");
      if (!ConnectNamedPipe(hNamedPipe, NULL)) {
          showError("Connect to name pipe failed");
          CloseHandle(hNamedPipe);
          return 0;
      }
      if (!ReadFile(hNamedPipe, pchMessage, sizeof(pchMessage), &amp;dwBytesRead, NULL)) {
          CloseHandle(hNamedPipe);
          showError("Read pipe failed");
          return 0;
      }
  
      printf("The server received the message from client: %s\n", pchMessage);
      cout &lt;&lt; "Input a string: ";
      cin.getline(pchMessage, 80);
      nMessageLength = strlen(pchMessage) + 1;
  
      //Answer to client
      if (!WriteFile(hNamedPipe, pchMessage, nMessageLength, &amp;dwBytesWrite, NULL)) {
          CloseHandle(hNamedPipe);
          showError("Write file failed.");
          return 0;
      }
      printf("The Server send the message to the client: %s\n", pchMessage);
      CloseHandle(hNamedPipe);
      printf("Press any key to exit");
      cin.get();
      return 0;
  }
  
  int main(int argc, char *argv[]) {
      if (argc == 1) return 0;
  
      if (!strcmp(argv[1], "client")) {
          client_GetNamedPipeHandleState();
      }
  
      if (!strcmp(argv[1], "server")) {
          server();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This is project which I make in Clion. Can be run as "app client" or "app server".</p>
  
  <p>What is the reason of the error?</p>