Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib, "Wininet")
- #include <Windows.h>
- #include <WinInet.h>
- #include <tlhelp32.h>
- #include <iostream>
- #include <tchar.h>
- #include <conio.h>
- #include <tlhelp32.h>
- #include <stdio.h>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #define BUF_SIZE 5000
- DWORD* buffer;
- HANDLE OpenAndReturnFileMapping(const char* name)
- {
- HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
- if (hMap == NULL)
- {
- printf("OpenFileMapping failed with error 0x%08x\n", GetLastError());
- return NULL;
- }
- return hMap;
- }
- HANDLE OpenAndReturnMutex(HANDLE hMap, const char* name)
- {
- HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, name);
- if (hMutex == NULL)
- {
- printf("OpenMutex failed with error 0x%08x\n", GetLastError());
- CloseHandle(hMap);
- return NULL;
- }
- return hMutex;
- }
- DWORD* CreateAndReturnMapViewOfFile(HANDLE hMap, HANDLE hSync)
- {
- DWORD* pMap = (DWORD*)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, BUF_SIZE);
- if (pMap == NULL)
- {
- printf("MapViewOfFile failed with error 0x%08x\n", GetLastError());
- CloseHandle(hMap);
- CloseHandle(hSync);
- return NULL;
- }
- return pMap;
- }
- void MutexReadFromMappedFile()
- {
- HANDLE hMap = OpenAndReturnFileMapping("ProjectFile");
- if (hMap == NULL) return;
- HANDLE hMutex = OpenAndReturnMutex(hMap, "shared_mutex");
- if (hMutex == NULL) return;
- int iteration = 0;
- int numbersRead = 0;
- while (1)
- {
- if (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) {
- printf("WaitForSingleObject failed with error 0x%08x\n", GetLastError());
- return;
- }
- buffer = CreateAndReturnMapViewOfFile(hMap, hMutex);
- if (buffer == NULL) return;
- ReleaseMutex(hMutex);
- }
- CloseHandle(hMutex);
- CloseHandle(hMap);
- UnmapViewOfFile(buffer);
- }
- DWORD WINAPI secondProcess(LPVOID lpParam) {
- cout << "process start" << endl;
- MutexReadFromMappedFile();
- return 0;
- }
- int main() {
- secondProcess(NULL);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement