赞
踩
进程间通信示例:程序一开始就有2个进程,它们在一个普通窗口中完成绘制矩形的任务。2个进程相互通信,一个进程再画矩形时,另一个进程要等待
#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; }
#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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。