这个需求可以通过使用Windows API编写一个简单的程序来实现。
以下是一个基本的示例代码:#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hPipe;
char buffer[1024];
DWORD dwRead;
// 创建一个命名管道,并打开它以进行读取操作
hPipe = CreateNamedPipe("\\\\.\\pipe\\myPipe", PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT, 1, 0, 0, NMPWAIT_USE_DEFAULT_WAIT, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
printf("Error creating named pipe\n");
return 1;
}
printf("Waiting for client to connect...\n");
// 等待客户端连接
if (!ConnectNamedPipe(hPipe, NULL))
{
printf("Error connecting to client\n");
CloseHandle(hPipe);
return 1;
}
printf("Client connected\n");
// 循环读取并显示来自管道的数据
while (1)
{
if (ReadFile(hPipe, buffer, sizeof(buffer), &dwRead, NULL))
{
buffer[dwRead] = '\0';
printf("Received: %s", buffer);
}
}
// 关闭管道句柄
CloseHandle(hPipe);
return 0;
}
|