当前位置:   article > 正文

C++ IPC进程间的通信《C++多线程编程实战》_cpp-ipc

cpp-ipc
  • 临界值:程序中能被访问共享内存的部分
  • 2个CPP文件需要在同一个解决方案中分别创建2个项目

进程间通信示例:程序一开始就有2个进程,它们在一个普通窗口中完成绘制矩形的任务。2个进程相互通信,一个进程再画矩形时,另一个进程要等待

  • IPCWorker.cpp
#include <Windows.h>
#include <tchar.h> //_tcscmp() _TCHAR*

#define COMMUNICATION_OBJECT_NAME TEXT("__FILE_MAPPING__")
#define SYNCHRONIZING_MUTEX_NAME TEXT("__TEST_MUTEX__")

typedef struct _tagCOMMUNICATIONOBJECT 
{
	HWND hWndClient;
	BOOL bExitLoop;
	LONG lSleepTimeout;
}COMMUNICATIONOBJECT, *PCOMMUNICATIONOBJECT;

int main(int argc, _TCHAR* argv[])
{
	HBRUSH hBrush = NULL;

	if (_tcscmp(TEXT("blue"), argv[0]) == 0)
	{
		hBrush = CreateSolidBrush(RGB(0, 0, 255));
	}
	else
	{
		hBrush = CreateSolidBrush(RGB(255, 0, 0));
	}

	HWND hWnd = NULL;
	HDC hDC = NULL;
	RECT rectClient = { 0 };
	LONG lWaitTimeout = 0;
	HANDLE hMapping = NULL;
	PCOMMUNICATIONOBJECT pCommObject = NULL;
	BOOL bContinueLoop = TRUE;

    //OpenMutex获得该已命名互斥量的句柄(如果有的话)
	HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, SYNCHRONIZING_MUTEX_NAME);
	hMapping = OpenFileMapping(FILE_MAP_READ, FALSE, COMMUNICATION_OBJECT_NAME);

	if (hMapping)
	{
		while (bContinueLoop)
		{
			WaitForSingleObject(hMutex, INFINITE);
			//获得指向文件映像对象的句柄
			pCommObject = (PCOMMUNICATIONOBJECT)MapViewOfFile(hMapping,
				FILE_MAP_READ, 0, 0, sizeof(COMMUNICATIONOBJECT));

			if (pCommObject)
			{
				bContinueLoop = !pCommObject->bExitLoop;
				hWnd = pCommObject->hWndClient;
				lWaitTimeout = pCommObject->lSleepTimeout;
				UnmapViewOfFile(pCommObject);
				hDC = GetDC(hWnd);
				if (GetClientRect(hWnd, &rectClient))
				{
					FillRect(hDC, &rectClient, hBrush);
				}

				ReleaseDC(hWnd, hDC);
				Sleep(lWaitTimeout); //故意的,因为记录的时候,进程切换太快根本注意不到
			}
			ReleaseMutex(hMutex); //释放互斥量的所有权,让其它进程可以获得互斥量,继续执行其他任务
		}
	}

	CloseHandle(hMapping);
	CloseHandle(hMutex);
	DeleteObject(hBrush);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

  • IPCDemo.cpp
#include <Windows.h>
#include <iostream>
#include <tchar.h> // _TCHAR*

using namespace std;

#define COMMUNICATION_OBJECT_NAME TEXT("__FILE_MAPPING__")
#define SYNCHRONIZING_MUTEX_NAME TEXT( "__TEST_MUTEX__" )
#define WINDOW_CLASS_NAME TEXT( "__TMPWNDCLASS__" )
#define BUTTON_CLOSE 100

typedef struct _tagCOMMUNICATIONOBJECT //用于整个文件映射过程中对象之间的通信
{
	HWND hWndClient;
	BOOL bExitLoop;
	LONG lSleepTimeout;
} COMMUNICATIONOBJECT, *PCOMMUNICATIONOBJECT;

LRESULT CALLBACK WndProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND InitializeWnd();
PCOMMUNICATIONOBJECT pCommObject = NULL;
HANDLE hMapping = NULL;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Interprocess communication demo." << endl;
	HWND hWnd = InitializeWnd();
	if (!hWnd)
	{
		cout << "Cannot create window!" << endl << "Error:\t" <<
			GetLastError() << endl;
		return 1;
	}
	HANDLE hMutex = CreateMutex(NULL, FALSE, SYNCHRONIZING_MUTEX_NAME);
	if (!hMutex)
	{
		cout << "Cannot create mutex!" << endl << "Error:\t" <<
			GetLastError() << endl;
		return 1;
	}
	hMapping = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0,
		sizeof(COMMUNICATIONOBJECT), COMMUNICATION_OBJECT_NAME);
	if (!hMapping)
	{
		cout << "Cannot create mapping object!" << endl << "Error:\t"
			<< GetLastError() << endl;
		return 1;
	}
	pCommObject = (PCOMMUNICATIONOBJECT)MapViewOfFile(hMapping,
		FILE_MAP_WRITE, 0, 0, 0);
	if (pCommObject)
	{
		pCommObject->bExitLoop = FALSE;
		pCommObject->hWndClient = hWnd;
		pCommObject->lSleepTimeout = 250;
		UnmapViewOfFile(pCommObject);
	}

	STARTUPINFO startupInfoRed = { 0 };
	PROCESS_INFORMATION processInformationRed = { 0 };
	STARTUPINFO startupInfoBlue = { 0 };
	PROCESS_INFORMATION processInformationBlue = { 0 };

	BOOL bSuccess = CreateProcess(TEXT("..\\Debug\\IPCWorker.exe"),
		(LPWSTR)TEXT("red"), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfoRed,
		&processInformationRed);
	if (!bSuccess)
	{
		cout << "Cannot create process red!" << endl << "Error:\t" <<
			GetLastError() << endl;
		return 1;
	}
	bSuccess = CreateProcess(TEXT("..\\Debug\\IPCWorker.exe"),
		(LPWSTR)TEXT("blue"), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfoBlue,
		&processInformationBlue);
	if (!bSuccess)
	{
		cout << "Cannot create process blue!" << endl << "Error:\t" <<
			GetLastError() << endl;
		return 1;
	}
	MSG msg = { 0 };
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	UnregisterClass(WINDOW_CLASS_NAME, GetModuleHandle(NULL));
	CloseHandle(hMapping);
	CloseHandle(hMutex);
	cout << "End program." << endl;
    return 0; 
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case BUTTON_CLOSE:
		{
			PostMessage(hWnd, WM_CLOSE, 0, 0);
			break;
		}
		default:
			break;
		}
		break;
	}
	case WM_DESTROY:
	{
		pCommObject = (PCOMMUNICATIONOBJECT)MapViewOfFile(hMapping,
			         FILE_MAP_WRITE, 0, 0, 0);
		if (pCommObject)
		{
			pCommObject->bExitLoop = TRUE;
			UnmapViewOfFile(pCommObject);
		}
		PostQuitMessage(0);
		break;
	}
	default:
	{
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
		break;
	}
	}

	return 0;
}

HWND InitializeWnd()
{
	WNDCLASSEX wndEx;
	wndEx.cbSize = sizeof(WNDCLASSEX);
	wndEx.style = CS_HREDRAW | CS_VREDRAW;
	wndEx.lpfnWndProc = WndProc;
	wndEx.cbClsExtra = 0;
	wndEx.cbWndExtra = 0;
	wndEx.hInstance = GetModuleHandle(NULL);
	wndEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wndEx.lpszMenuName = NULL;
	wndEx.lpszClassName = WINDOW_CLASS_NAME;
	wndEx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndEx.hIcon = LoadIcon(wndEx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wndEx.hIconSm = LoadIcon(wndEx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	if (!RegisterClassEx(&wndEx))
	{
		return NULL;
	}
	HWND hWnd = CreateWindow(wndEx.lpszClassName,
		TEXT("Interprocess communication Demo"),
		WS_OVERLAPPEDWINDOW, 200, 200, 400, 300, NULL, NULL,
		wndEx.hInstance, NULL);
	if (!hWnd)
	{
		return NULL;
	}
	HWND hButton = CreateWindow(TEXT("BUTTON"), TEXT("Close"),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
		275, 225, 100, 25, hWnd, NULL, wndEx.hInstance, NULL);
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	return hWnd;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169

  • 运行结果
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1000851
推荐阅读
相关标签
  

闽ICP备14008679号