C++ 管道

// PipeServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;
#pragma warning(disable:4996)

HANDLE hClose = NULL;

unsigned int _stdcall ProcessPipe(void *lp)
{
	HANDLE hPipeServer = (HANDLE)lp;
	BYTE btRead[MAX_PATH] = {0};

	DWORD dwTime = GetTickCount();
	while (GetTickCount() - dwTime < 10000){
		DWORD dwRead = 0;
		BOOL bRet = ReadFile(hPipeServer, btRead, MAX_PATH, &dwRead, NULL);
		if (!bRet){
			if (GetLastError() == ERROR_BROKEN_PIPE ){
				break;
			}
		}
		else{
			printf("%s\n", btRead);
			ZeroMemory(btRead, MAX_PATH);
			strcpy((char*)btRead, "RecvInfo");
			DWORD dwWrite = 0;
			bRet = WriteFile(hPipeServer, btRead, strlen((char*)btRead), &dwWrite, NULL);
		}
		Sleep(20);
	}

	SetEvent(hClose);
	DWORD dwExitCode = 0;
	GetExitCodeThread(GetCurrentThread(), &dwExitCode);
	_endthreadex(dwExitCode);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hPipeServer = CreateNamedPipe(L"\\\\.\\pipe\\pipeServer", 
		PIPE_ACCESS_DUPLEX, 
		PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 
		4096,
		4096,
		NMPWAIT_USE_DEFAULT_WAIT, 
		NULL
		);
	if (hPipeServer == INVALID_HANDLE_VALUE){
		printf("CreateNamedPipe failed with error %d", GetLastError());
		return 0;
	}

	BOOL bRet = ConnectNamedPipe(hPipeServer, NULL);
	if (!bRet){
		printf("ConnectNamedPipe failed with error %d", GetLastError());
		return 0;
	}

	hClose = CreateEvent(NULL, true, false, NULL);
	HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ProcessPipe, (LPVOID)hPipeServer, 0, NULL);
	CloseHandle(hThread);

	WaitForSingleObject(hClose, INFINITE);
	CloseHandle(hPipeServer);

	return 0;
}

.cpp

// PipeClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <process.h>
using namespace std;
#pragma warning(disable:4996)

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hPipe = CreateFile( 
		L"\\\\.\\pipe\\pipeServer",   // pipe name 
		GENERIC_READ | GENERIC_WRITE, 
		0,              // no sharing 
		NULL,           // default security attributes
		OPEN_EXISTING,  // opens existing pipe 
		0,              // default attributes 
		NULL);   

	char strInfo[MAX_PATH] = {0};
	strcpy(strInfo, "12sdfgsgdfg");
	DWORD dwWrite = 0;
	WriteFile(hPipe, strInfo, strlen(strInfo), &dwWrite, NULL);
	Sleep(500);
	ZeroMemory(strInfo, MAX_PATH);
	DWORD dwRead = 0;
	ReadFile(hPipe, strInfo, MAX_PATH, &dwRead, NULL);
	printf("%s\n", strInfo);
	Sleep(8000);
	CloseHandle(hPipe);
	return 0;
}


Visual C++ 是一种集成开发环境(IDE),用于开发基于 C++ 的应用程序。在 Visual C++ 中,我们可以使用管道功能来模拟输入。 管道是一种进程间通信的方式,它允许一个进程的输出作为另一个进程的输入。在模拟输入的情况下,我们可以创建一个进程,将其标准输出(stdout)连接到一个管道,并将另一个进程的标准输入(stdin)连接到该管道。这样,当我们向第一个进程写入数据时,数据会通过管道传递给第二个进程,从而实现模拟输入。 在 Visual C++ 中,我们可以使用 CreatePipe 函数来创建管道。该函数有两个参数,分别是指向管道读取端和写入端的句柄的指针。我们可以使用这两个句柄来读取和写入管道的数据。 为了使用管道进行模拟输入,我们需要首先创建一个进程,并将其标准输出连接到一个管道。然后,我们可以使用 WriteFile 函数向管道写入数据,从而实现模拟输入。接下来,我们可以创建另一个进程,并将其标准输入连接到同一个管道。这样,当我们读取第二个进程的输入时,它将接收到我们之前写入的数据。 需要注意的是,使用管道进行模拟输入需要正确设置进程的标准输入和标准输出。我们还需要适当地处理错误和关闭管道,以确保程序的正常运行和资源的正确释放。 总之,Visual C++ 中的管道功能可以很方便地实现模拟输入。通过创建管道、写入数据和读取输入,我们可以将一个进程的输出作为另一个进程的输入,从而实现模拟输入的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值