进程间通信的四种方式

进程间通信的四种方式
剪贴板
匿名管道
命名管道
邮槽

 

 

剪贴板

#include <stdio.h>
#include <windows.h>
#include <tchar.h>


int main (void)
{	
	//设置剪贴板
	if(OpenClipboard(GetForegroundWindow()))
	{
		HANDLE hClip;
		TCHAR szText[100]={0};
		EmptyClipboard();
		scanf("%s",szText);
		hClip=GlobalAlloc(GMEM_MOVEABLE,ARRAYSIZE(szText)+1);
		TCHAR *pBuf=(TCHAR*)GlobalLock(hClip);
		_tcscpy(pBuf,szText);
		GlobalUnlock(hClip);
		SetClipboardData(CF_TEXT,hClip);
		CloseClipboard();
	}

	//读取剪贴板
	if(OpenClipboard(GetForegroundWindow()))
	{
		if(IsClipboardFormatAvailable(CF_TEXT))
		{
			HANDLE hClip;
			TCHAR *pBuf;
			hClip=GetClipboardData(CF_TEXT);
			pBuf=(TCHAR*)GlobalLock(hClip);
			GlobalUnlock(hClip);
			printf("%s\n",pBuf);
			CloseClipboard();
		}
	}

	return 0;
}


 

 

匿名管道

#include <stdio.h>
#include <windows.h>
#include <tchar.h>

int main (void)
{
	//创建匿名管道
	HANDLE hWrite=NULL;
	HANDLE hRead=NULL;
	SECURITY_ATTRIBUTES sa;
	sa.bInheritHandle=TRUE;
	sa.lpSecurityDescriptor=NULL;
	sa.nLength=sizeof(SECURITY_ATTRIBUTES);
	if(!CreatePipe(&hRead,&hWrite,&sa,0))
	{
		MessageBox(NULL,TEXT("创建匿名管道失败!"),NULL,MB_OK);
		return 0;
	}
	STARTUPINFO sui;
	PROCESS_INFORMATION pi;
	ZeroMemory(&sui,sizeof(STARTUPINFO));
	sui.cb=sizeof(STARTUPINFO);
	sui.dwFlags=STARTF_USESTDHANDLES;
	sui.hStdInput=hRead;
	sui.hStdOutput=hWrite;
	sui.wShowWindow=SW_SHOW;
	sui.hStdError=GetStdHandle(STD_ERROR_HANDLE);

	if(!CreateProcess(TEXT("..\\debug\\Child.exe"),NULL,NULL,NULL,
		TRUE,0,NULL,NULL,&sui,&pi))
	{
		CloseHandle(hRead);
		CloseHandle(hWrite);
		hRead=NULL;
		hWrite=NULL;
		MessageBox(NULL,TEXT("创建子进程失败!"),NULL,MB_OK);
		return 0;
	}
	else
	{
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
	//写入数据
	TCHAR buf[]=TEXT("Parent我是你的大皇冠");
	DWORD dwWrite;
	if(!WriteFile(hWrite,buf,_tcslen(buf)+1,&dwWrite,NULL))
	{
		MessageBox(NULL,TEXT("Parent写入数据失败!"),NULL,MB_OK);
		return 0;
	}

	//读取数据
	TCHAR bufRead[100]={0};
	DWORD dwRead;
	if(!ReadFile(hRead,bufRead,100,&dwRead,NULL))
	{
		MessageBox(NULL,TEXT("Parent读取数据失败!"),NULL,MB_OK);
		return 0;
	}
	printf("Parent%s\n",bufRead);

	
	CloseHandle(hRead);
	CloseHandle(hWrite);
	return 0;
}


 

#include <stdio.h>
#include <windows.h>
#include <tchar.h>


int main (void)
{
	_asm
	{
		int 3;
	}

	HANDLE hRead=GetStdHandle(STD_INPUT_HANDLE);
	HANDLE hWrite=GetStdHandle(STD_OUTPUT_HANDLE);
	//读取数据
	TCHAR bufRead[100]={};
	DWORD dwRead;
	if(!ReadFile(hRead,bufRead,ARRAYSIZE(bufRead),&dwRead,NULL))
	{
		MessageBox(NULL,TEXT("Child读取数据失败"),NULL,MB_OK);
		return 0;
	}
	printf("Child%s\n",bufRead);
	CloseHandle(hRead);

	//写入数据
	TCHAR buf[]=TEXT("Child匿名管道测试程序");
	DWORD dwWrite;
	if(!WriteFile(hWrite,buf,ARRAYSIZE(buf)+1,&dwWrite,NULL))
	{
		MessageBox(NULL,TEXT("Child写入数据失败"),NULL,MB_OK);
		return 0;
	}
	CloseHandle(hWrite);

	return 0;
}


 

 

命名管道

#include <stdio.h>
#include <windows.h>

int main (void)
{

	//创建命名管道
	HANDLE hPipe=CreateNamedPipe(TEXT("\\\\.\\pipe\\MyPipe"),
		PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
		0,1,1024,1024,0,NULL);
	if(INVALID_HANDLE_VALUE==hPipe)
	{
		hPipe=NULL;
		return	0;
	}
	HANDLE hEvent;
	hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
	if(!hEvent)
	{
		CloseHandle(hPipe);
		hPipe=NULL;
		return	0;
	}
	OVERLAPPED ovlap;
	ZeroMemory(&ovlap,sizeof(OVERLAPPED));
	ovlap.hEvent=hEvent;
	if(!ConnectNamedPipe(hPipe,&ovlap))
	{
		if(ERROR_IO_PENDING!=GetLastError())
		{
			MessageBox(NULL,TEXT("等待客户端连接失败!"),NULL,MB_OK);
			CloseHandle(hPipe);
			CloseHandle(hEvent);
			hPipe=NULL;
			return	0;
		}
	}
	if(WAIT_FAILED==WaitForSingleObject(hEvent,INFINITE))
	{
		MessageBox(NULL,TEXT("等待对象失败!"),NULL,MB_OK);
		CloseHandle(hPipe);
		CloseHandle(hEvent);
		hPipe=NULL;
		return	0;
	}
	CloseHandle(hEvent);

	//写入数据
	TCHAR bufWrite[]=TEXT("我是你的大皇冠");
	DWORD dwWrite;
	if(!WriteFile(hPipe,bufWrite,ARRAYSIZE(bufWrite)+1,&dwWrite,NULL))
	{
		MessageBox(NULL,TEXT("写入数据失败!"),NULL,MB_OK);
		return	0;
	}

	//读取数据
	TCHAR buf[100]={0};
	DWORD dwRead;
	if(!ReadFile(hPipe,buf,ARRAYSIZE(buf),&dwRead,NULL))
	{
		MessageBox(NULL,TEXT("读取数据失败!"),NULL,MB_OK);
		return	0;
	}
	printf("%s\n",buf);



	return 0;
}


 

#include <stdio.h>
#include <windows.h>

int main (void)
{

	//连接操作
	if(!WaitNamedPipe(TEXT("\\\\.\\pipe\\MyPipe"),NMPWAIT_WAIT_FOREVER))
	{
		MessageBox(NULL,TEXT("当前没有可利用的命名管道实例!"),NULL,MB_OK);
		return	0;
	}
	HANDLE hPipe=CreateFile(TEXT("\\\\.\\pipe\\MyPipe"),GENERIC_READ | GENERIC_WRITE,
		0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hPipe)
	{
		MessageBox(NULL,TEXT("打开命名管道失败!"),NULL,MB_OK);
		hPipe=NULL;
		return	0;
	}
	
	//读取数据
	TCHAR buf[100]={0};
	DWORD dwRead;
	if(!ReadFile(hPipe,buf,ARRAYSIZE(buf),&dwRead,NULL))
	{
		MessageBox(NULL,TEXT("读取数据失败!"),NULL,MB_OK);
		return	0;
	}
	printf("%s\n",buf);

	//写入数据
	TCHAR bufWrite[]=TEXT("命名管道测试程序");
	DWORD dwWrite;
	if(!WriteFile(hPipe,buf,ARRAYSIZE(bufWrite)+1,&dwWrite,NULL))
	{
		MessageBox(NULL,TEXT("写入数据失败!"),NULL,MB_OK);
		return	0;
	}
	CloseHandle(hPipe);
	return 0;
}


 

邮槽

#include <stdio.h>
#include <windows.h>


int main (void)
{
	HANDLE hMailslot;
	hMailslot=CreateMailslot(TEXT("\\\\.\\mailslot\\my125096"),0,MAILSLOT_WAIT_FOREVER,NULL);
	if(INVALID_HANDLE_VALUE==hMailslot)
	{
		MessageBox(NULL,TEXT("创建油槽失败!"),NULL,MB_OK);
		return	0;
	}
	char buf[100];
	DWORD dwRead;
	if(!ReadFile(hMailslot,buf,100,&dwRead,NULL))
	{
		MessageBox(NULL,TEXT("读取数据失败!"),NULL,MB_OK);
		CloseHandle(hMailslot);
		return 0;
	}
	printf("%s\n",buf);
	CloseHandle(hMailslot);
	getchar();
	getchar();
	return 0;
}


 

#include <stdio.h>
#include <Windows.h>

int main (void)
{
	HANDLE hMailslot;
	hMailslot=CreateFile(TEXT("\\\\.\\mailslot\\my125096"),GENERIC_WRITE,
		FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hMailslot)
	{
		MessageBox(NULL,TEXT("打开油槽失败!"),NULL,MB_OK);
		return	0;
	}
	TCHAR buf[]=TEXT("打你PG我不乖");
	DWORD dwWrite;
	if(!WriteFile(hMailslot,buf,ARRAYSIZE(buf)+1,&dwWrite,NULL))
	{
		MessageBox(NULL,TEXT("写入数据失败!"),NULL,MB_OK);
		CloseHandle(hMailslot);
		return	0;
	}
	CloseHandle(hMailslot);

	getchar();
	getchar();
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值