Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Named Pipe C++ Windows

I am trying to create a simple comunication between 2 processes in C++ ( Windows ) like FIFO in linux. This is my server:

int main() {     HANDLE pipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"), GENERIC_READ, 0, NULL, OPEN_EXISTING,    FILE_FLAG_OVERLAPPED, NULL);     ConnectNamedPipe(pipe, NULL);     while(TRUE){         string data;         DWORD numRead =1 ;         ReadFile(pipe, &data, 1024, &numRead, NULL);         cout << data << endl;  }     CloseHandle(pipe);     return 0; } 

And this is my client:

int main() {     HANDLE pipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);     ConnectNamedPipe(pipe, NULL);     string message = "TEST";     DWORD numWritten;     WriteFile(pipe, message.c_str(), message.length(), &numWritten, NULL);     return 0; } 

The code does't work , how can i fixed it to like FIFO ?

like image 680
user3052078 Avatar asked Oct 25 '14 10:10

user3052078


People also ask

How do you make a named pipe in Windows?

To create an instance of a named pipe by using CreateNamedPipe, the user must have FILE_CREATE_PIPE_INSTANCE access to the named pipe object. If a new named pipe is being created, the access control list (ACL) from the security attributes parameter defines the discretionary access control for the named pipe.

How do you create a named pipe in C++?

A simple exampleCall CreateNamedPipe(..) to create an instance of a named pipe. Call ConnectNamedPipe(..) to wait for the client program to connect. Call WriteFile(..) to send data down the pipe. Call CloseHandle(..) to disconnect and close the pipe instance.

What is named pipes in Windows?

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication.

Where are named pipes on Windows?

Every pipe is placed in the root directory of the named pipe filesystem (NPFS), mounted under the special path \\. \pipe\ (that is, a pipe named "foo" would have a full path name of \\. \pipe\foo ).


1 Answers

You cannot create a named pipe by calling CreateFile(..).

Have a look at the pipe examples of the MSDN. Since these examples are quite complex I've quickly written a VERY simple named pipe server and client.

int main(void) {     HANDLE hPipe;     char buffer[1024];     DWORD dwRead;       hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\Pipe"),                             PIPE_ACCESS_DUPLEX,                             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,   // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...                             1,                             1024 * 16,                             1024 * 16,                             NMPWAIT_USE_DEFAULT_WAIT,                             NULL);     while (hPipe != INVALID_HANDLE_VALUE)     {         if (ConnectNamedPipe(hPipe, NULL) != FALSE)   // wait for someone to connect to the pipe         {             while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)             {                 /* add terminating zero */                 buffer[dwRead] = '\0';                  /* do something with data in buffer */                 printf("%s", buffer);             }         }          DisconnectNamedPipe(hPipe);     }      return 0; } 

And here is the client code:

int main(void) {     HANDLE hPipe;     DWORD dwWritten;       hPipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"),                         GENERIC_READ | GENERIC_WRITE,                         0,                        NULL,                        OPEN_EXISTING,                        0,                        NULL);     if (hPipe != INVALID_HANDLE_VALUE)     {         WriteFile(hPipe,                   "Hello Pipe\n",                   12,   // = length of string + terminating '\0' !!!                   &dwWritten,                   NULL);          CloseHandle(hPipe);     }      return (0); } 

You should replace the name of the pipe TEXT("\\\\.\\pipe\\Pipe") by a #define which is located in a commonly used header file.

like image 72
Lukas Thomsen Avatar answered Oct 07 '22 16:10

Lukas Thomsen