进程通信

clipboard

#include <iostream>
#include <Windows.h>

int main()
{
	//CTRL C
	#if 0
	const char* str = "Hello 0xCC";

	//打开剪贴板 OpenClipboard
	if (!OpenClipboard(NULL)) return 0;

	//清空剪贴板 EmptyClipboard
	if (!EmptyClipboard())
	{
		CloseClipboard();
		return 0;
	}

	//分配内存区 GlobalAlloc
	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(str) + 1);
	if (hMem == NULL)
	{
		CloseClipboard();
		return 0;
	}

	PVOID pBuffer = GlobalLock(hMem);
	if (pBuffer == NULL)
	{
		CloseClipboard();
		return 0;
	}

	memcpy(pBuffer, str, strlen(str) + 1);
	GlobalUnlock(hMem);

	//设置剪贴板 SetClipboard
	SetClipboardData(CF_TEXT, hMem);

	//关闭剪贴板 CloseClipboard
	CloseClipboard();
	#endif 

	//CTRL V
	#if 0
	//打开剪贴板 OpenClipboard
	if (!OpenClipboard(NULL)) return 0;

	//获取剪贴板 GetClipboardData
	HANDLE hData = GetClipboardData(CF_TEXT);
	if (hData == NULL)
	{
		CloseClipboard();
		return 0;
	}

	PCHAR pStr = (PCHAR)GlobalLock(hData);
	std::cout << pStr << std::endl;
	GlobalUnlock(hData);

	//关闭剪贴板 CloseClipboard
	CloseClipboard();
	
	#endif

	return 0;
}

FileMapping

#include <iostream>
#include <windows.h>

int main()
{
	//创建文件映射对象
	HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,0xFF,TEXT("0xCCShare"));
	if (hMapFile == NULL)
	{
		std::cout << "CreateFileMapping ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	//映射对象视图
	LPVOID lpBuffer = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
	if (lpBuffer == NULL)
	{
		std::cout << "MapViewOfFile ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	//写入共享内存数据
	char szStr[] = "Exit";
	memcpy(lpBuffer, szStr, sizeof(szStr));

	//等待进程读取
	std::cout << "Success Share Mem " << std::endl;
	std::cin.get();

	//释放资源
	UnmapViewOfFile(lpBuffer);
	CloseHandle(hMapFile);

	return 0;
}
#include <iostream>
#include <windows.h>

int main()
{
	//打开存在文件映射对象
	HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("0xCCShare"));
	if (hMapFile == NULL)
	{
		std::cout << "OpenFileMapping ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	//映射对象视图
	LPVOID lpBuffer = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
	if (lpBuffer == NULL)
	{
		std::cout << "MapViewOfFile ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	std::cout << "Read Info -> " << static_cast<char*>(lpBuffer) << std::endl;
	std::cin.get();

	//获取共享内存数据
	if (strcmp((PCHAR)lpBuffer, "Exit") == 0)
	{
		ExitProcess(0);
	}

	//释放资源
	std::cout << "Free" << std::endl;
	UnmapViewOfFile(lpBuffer);
	CloseHandle(hMapFile);

	return 0;
}

NamedPipe

#include <iostream>
#include <Windows.h>

#define PIPE_NAME L"\\\\.\\pipe\\0xCCPipe"

int main()
{
	//创建命名管道
	HANDLE hNamedPipe = CreateNamedPipe(
		PIPE_NAME,
		PIPE_ACCESS_DUPLEX,
		PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
		PIPE_UNLIMITED_INSTANCES,
		4096,
		4096,
		NMPWAIT_USE_DEFAULT_WAIT,
		NULL);
	if (hNamedPipe == INVALID_HANDLE_VALUE)
	{
		std::cout << "CreateNamedPipe ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	//等待客户连接
	std::cout << "Waiting for client Connection" << std::endl;
	BOOL bRet = ConnectNamedPipe(hNamedPipe, NULL);
	if (!bRet)
	{
		std::cout << "ConnectNamedPipe ErrorCode -> " << GetLastError() << std::endl;
		CloseHandle(hNamedPipe);
		return 0;
	}
	std::cout << "Client Connected" << std::endl;

	//发送客户消息
	DWORD dwWrite = 0;
	char szBuffer[] = "Hello 0xCC";
	WriteFile(hNamedPipe, szBuffer, sizeof(szBuffer), &dwWrite, NULL);

	//接受客户消息
	CHAR szBuffer1[0xFF] = { 0 };
	DWORD dwRead = 0;
	ReadFile(hNamedPipe, szBuffer1, sizeof(szBuffer1), &dwRead, NULL);
	std::cout << szBuffer1 << std::endl;

	//清理资源
	CloseHandle(hNamedPipe);

	return 0;
}
#include <iostream>
#include <Windows.h>

#define PIPE_NAME L"\\\\.\\pipe\\0xCCPipe"

int main()
{
	//连接命名管道
	HANDLE hNamedPipe = CreateFile(
		PIPE_NAME,
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		NULL,
		NULL
	);
	if (hNamedPipe == INVALID_HANDLE_VALUE)
	{
		std::cout << "CreateNamedPipe ErrorCode -> " << GetLastError() << std::endl;
		return 0;
	}

	//接受服务消息
	CHAR szBuffer[0xFF] = { 0 };
	DWORD dwRead = 0;
	ReadFile(hNamedPipe, szBuffer, sizeof(szBuffer), &dwRead, NULL);
	std::cout << szBuffer << std::endl;

	//发送服务消息
	DWORD dwWrite = 0;
	char szBuffer1[] = "Hello 0xCC";
	WriteFile(hNamedPipe, szBuffer1, sizeof(szBuffer1), &dwWrite, NULL);

	//清理资源
	CloseHandle(hNamedPipe);

	return 0;
}

Mailslot

#include <iostream>
#include <windows.h>

int main() 
{
    //创建邮槽
    HANDLE hMailSlot = CreateMailslot(
        TEXT("\\\\.\\mailslot\\0xCCMailSlot"),
        0,
        MAILSLOT_WAIT_FOREVER,
        NULL
    );
    if (hMailSlot == INVALID_HANDLE_VALUE) return 0;

    //读取邮槽
    while (TRUE)
    {
        DWORD dwRead = 0;
        char szBuffer[0xFF] = { 0 };
        BOOL bRet = ReadFile(hMailSlot, szBuffer, sizeof(szBuffer), &dwRead, NULL);
        if (!bRet) break;
        std::cout << szBuffer << std::endl;
    }

    //清理资源
    CloseHandle(hMailSlot);
    
    return 0;
}
#include <iostream>
#include <Windows.h>

int main()
{
	// 打开邮槽
	HANDLE hMailSlot = CreateFile(
		TEXT("\\\\.\\mailslot\\0xCCMailSlot"),
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);
	if (hMailSlot == INVALID_HANDLE_VALUE) return 0;

	// 写入邮槽
	DWORD dwWrite = 0;
	char szBuffer[] = "Hello 0xCC";
	WriteFile(hMailSlot, szBuffer, sizeof(szBuffer), &dwWrite, NULL);

	//清理资源
	CloseHandle(hMailSlot);

	return 0;
}

上一篇:Redis 典型应用——分布式锁


下一篇:第5章:软件工程