Skip to content

Commit 3e3e948

Browse files
save file
1 parent 71a9aeb commit 3e3e948

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
3+
4+
#include <windows.h>
5+
#include <stdio.h>
6+
7+
8+
int main(void) {
9+
// Named pipe path
10+
const char *pipeName = "\\\\.\\pipe\\mypipe";
11+
12+
// Create the named pipe
13+
HANDLE hPipe = CreateNamedPipeA(
14+
pipeName,
15+
PIPE_ACCESS_OUTBOUND, // Server writes, client reads
16+
PIPE_TYPE_BYTE | PIPE_WAIT, // Byte stream, blocking mode
17+
1, // Max instances
18+
1024, // Out buffer size
19+
1024, // In buffer size
20+
0, // Default timeout
21+
NULL // Security attributes
22+
);
23+
24+
if(hPipe==INVALID_HANDLE_VALUE){
25+
printf("CreateNamedPipe failed with error %lu\n",GetLastError());
26+
return 1;
27+
}
28+
29+
printf("Waiting for client to connect...\n");
30+
31+
// Wait for a client (Node.js) to connect
32+
BOOL connected = ConnectNamedPipe(hPipe,NULL) ? TRUE : (GetLastError()==ERROR_PIPE_CONNECTED);
33+
34+
if (!connected){
35+
printf("ConnectNamedPipe failed with error %lu\n", GetLastError());
36+
CloseHandle(hPipe);
37+
return 1;
38+
}
39+
40+
// Send a test message
41+
const char *msg = "Hello from C over Windows Named Pipe!";
42+
DWORD written;
43+
WriteFile(hPipe,msg,(DWORD)strlen(msg),&written,NULL);
44+
45+
printf("Sent message to client.\n");
46+
47+
// Close pipe
48+
CloseHandle(hPipe);
49+
return 0;
50+
51+
}//main
52+
53+

0 commit comments

Comments
 (0)