Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #pragma comment(lib, "Wininet")
  2. #include <Windows.h>
  3. #include <WinInet.h>
  4. #include <tlhelp32.h>
  5. #include <iostream>
  6. #include <tchar.h>
  7. #include <conio.h>
  8. #include <tlhelp32.h>
  9. #include <stdio.h>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13.  
  14. using namespace std;
  15.  
  16. #define BUF_SIZE 5000
  17.  
  18. DWORD* buffer;
  19.  
  20.  
  21. HANDLE OpenAndReturnFileMapping(const char* name)
  22. {
  23. HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
  24. if (hMap == NULL)
  25. {
  26. printf("OpenFileMapping failed with error 0x%08x\n", GetLastError());
  27. return NULL;
  28. }
  29. return hMap;
  30. }
  31.  
  32.  
  33. HANDLE OpenAndReturnMutex(HANDLE hMap, const char* name)
  34. {
  35. HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, name);
  36. if (hMutex == NULL)
  37. {
  38. printf("OpenMutex failed with error 0x%08x\n", GetLastError());
  39. CloseHandle(hMap);
  40. return NULL;
  41. }
  42. return hMutex;
  43. }
  44.  
  45. DWORD* CreateAndReturnMapViewOfFile(HANDLE hMap, HANDLE hSync)
  46. {
  47. DWORD* pMap = (DWORD*)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, BUF_SIZE);
  48. if (pMap == NULL)
  49. {
  50. printf("MapViewOfFile failed with error 0x%08x\n", GetLastError());
  51. CloseHandle(hMap);
  52. CloseHandle(hSync);
  53. return NULL;
  54. }
  55. return pMap;
  56. }
  57.  
  58.  
  59. void MutexReadFromMappedFile()
  60. {
  61. HANDLE hMap = OpenAndReturnFileMapping("ProjectFile");
  62. if (hMap == NULL) return;
  63.  
  64. HANDLE hMutex = OpenAndReturnMutex(hMap, "shared_mutex");
  65. if (hMutex == NULL) return;
  66.  
  67. int iteration = 0;
  68. int numbersRead = 0;
  69. while (1)
  70. {
  71. if (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) {
  72. printf("WaitForSingleObject failed with error 0x%08x\n", GetLastError());
  73. return;
  74. }
  75.  
  76. buffer = CreateAndReturnMapViewOfFile(hMap, hMutex);
  77. if (buffer == NULL) return;
  78.  
  79. ReleaseMutex(hMutex);
  80. }
  81.  
  82.  
  83. CloseHandle(hMutex);
  84. CloseHandle(hMap);
  85. UnmapViewOfFile(buffer);
  86. }
  87.  
  88. DWORD WINAPI secondProcess(LPVOID lpParam) {
  89. cout << "process start" << endl;
  90.  
  91. MutexReadFromMappedFile();
  92. return 0;
  93. }
  94.  
  95. int main() {
  96. secondProcess(NULL);
  97. system("pause");
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement