Chapter-1-code1
#include "stdafx.h"
#include <iostream>
void _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Welcome C++ Multithreading CookBook!\t" << std::endl;
system("pause");
}
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
STARTUPINFO startupInfo = { 0 };
PROCESS_INFORMATION processInformation = { 0 };
BOOL bSuccess = CreateProcess(TEXT("C:/Windows/notepad.exe"), NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &startupInfo, &processInformation);
if (bSuccess)
{
cout << "Process started." << endl
<< "Process ID:\t"
<< processInformation.dwProcessId << endl;
}
else
{
cout << "Cannot start process!" << endl
<< "Error Code:\t" << GetLastError() << endl;
}
return system("pause");
}
#include "stdafx.h"
#include <Windows.h>
#include <Winternl.h>
#include <iostream>
using namespace std;
typedef NTSTATUS(NTAPI* QEURYINFORMATIONPROCESS)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO startupInfo = { 0 };
PROCESS_INFORMATION processInformation = { 0 };
BOOL bSuccess = CreateProcess(TEXT("C:/Windows/notepad.exe"), NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &startupInfo, &processInformation);
if (bSuccess)
{
cout << "Process started." << endl
<< "Process ID:\t"
<< processInformation.dwProcessId << endl;
PROCESS_BASIC_INFORMATION pbi;
ULONG uLength = 0;
HMODULE hDll = LoadLibrary(TEXT("C:/Windows/System32/ntdll.dll"));
if (hDll)
{
QEURYINFORMATIONPROCESS QueryInformationProcess = (QEURYINFORMATIONPROCESS)GetProcAddress(hDll, "NtQueryInformationProcess");
if (QueryInformationProcess)
{
NTSTATUS ntStatus= QueryInformationProcess(
processInformation.hProcess,
PROCESSINFOCLASS::ProcessBasicInformation,
&pbi, sizeof(pbi), &uLength);
if (NT_SUCCESS(ntStatus))
{
cout << "Process ID (from PCB):\t"
<< pbi.UniqueProcessId << endl;
}
else
{
cout << "Cannot open PCB@" << endl
<< "Error Code" << GetLastError() << endl;
}
}
}
else
{
cout << "Cannot load ntdll.dll" << endl
<< "Error Code" << GetLastError() << endl;
}
}
else
{
cout << "Cannot start process!" << endl
<< "Error Code:\t" << GetLastError() << endl;
}
return system("pause");
}
```cpp
#include "stdafx.h"
#include <Windows.h>
#define COMMUNICATION_OBJECT_NAME TEXT("__FILE_MAPPING__")
#define SYNCHRONIZING_MUTEX_NAME TEXT("__TEXT_MUTEX__")
typedef struct _tagCOMMUNICATIONOBJECT
{
HWND hWndClient;
BOOL bExitLoop;
LONG lSleepTimeout;
}COMMUNICATIONOBJECT,*PCOMMUNICATIONOBJECT;
int _tmain(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;
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 "stdafx.h"
#include <Windows.h>
#include <iostream>
#define COMMUNICATION_OBJECT_NAME TEXT("__FILE_MAPPING__")
#define SYNCHRONIZING_MUTEX_NAME TEXT("__TEXT_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[])
{
HWND hWnd = InitializeWnd();
HANDLE hMutex = CreateMutex(NULL,FALSE,SYNCHRONIZING_MUTEX_NAME);
hMapping = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, sizeof(COMMUNICATIONOBJECT), COMMUNICATION_OBJECT_NAME);
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/Chapter2-code-3.exe"),TEXT("blue"),NULL,NULL,FALSE,0,NULL,NULL,&startupInfoBlue,&processInformationBlue);
bSuccess = CreateProcess(TEXT("../Debug/Chapter2-code-3.exe"), TEXT("red"), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfoRed, &processInformationRed);
MSG msg = { 0 };
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(WINDOW_CLASS_NAME, GetModuleHandle(NULL));
CloseHandle(hMapping);
CloseHandle(hMutex);
return 0;
}
LRESULT CALLBACK WndProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case BUTTON_CLOSE:
{
PostMessage(hDlg,WM_CLOSE,0,0);
break;
}
default:
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(hDlg, uMsg, wParam, lParam);
}
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,(HMENU)BUTTON_CLOSE,wndEx.hInstance,NULL);
HWND hStatic = CreateWindow(TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE, 10, 10, 365, 205, hWnd, NULL, wndEx.hInstance, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return hStatic;
}
``
|