-
-
Save yaowenqiang/e1ec66bb488760fc83d5c8edc0acb7f1 to your computer and use it in GitHub Desktop.
Example of NamedPipe in c Windows (based from https://stackoverflow.com/questions/2640642/c-implementing-named-pipes-using-the-win32-api)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <tchar.h> | |
#define MAX_BUF 1024 | |
#define pipename "\\\\.\\pipe\\LogPipe" | |
int main() | |
{ | |
HANDLE pipe = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); | |
if (pipe == INVALID_HANDLE_VALUE) | |
{ | |
printf("Error: %d", GetLastError()); | |
} | |
char message [] = "Hi"; | |
DWORD numWritten; | |
WriteFile(pipe, message, 3, &numWritten, NULL); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <tchar.h> | |
#define BUFSIZE 512 | |
#define pipename "\\\\.\\pipe\\LogPipe" | |
int main(int argc, TCHAR *argv[]) | |
{ | |
while (true) | |
{ | |
HANDLE pipe = CreateNamedPipe(pipename, PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL); | |
if (pipe == INVALID_HANDLE_VALUE) | |
{ | |
printf("Error: %d", GetLastError()); | |
} | |
char data[1024]; | |
DWORD numRead; | |
ConnectNamedPipe(pipe, NULL); | |
ReadFile(pipe, data, 1024, &numRead, NULL); | |
if (numRead > 0) | |
printf("%s\n", data); | |
CloseHandle(pipe); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment