Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.4 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: shared.cxx
  7. //
  8. // Contents: This file contains a set of routines for the management of
  9. // shared memory.
  10. //
  11. // Functions: SCHEDAllocShared: Allocates a handle (in a given process)
  12. // to a copy of a memory block in this process.
  13. //
  14. // SCHEDFreeShared: Releases the handle (and the copy of the
  15. // memory block)
  16. //
  17. // SCHEDLockShared: Maps a handle (from a given process) into
  18. // a memory block in this process. Has the option of
  19. // transfering the handle to this process, thereby deleting
  20. // it from the given process
  21. //
  22. // SCHEDUnlockShared: Opposite of SCHEDLockShared, unmaps the
  23. // memory block
  24. //
  25. // History: 4/1/1996 RaviR Created (stole from shell\dll\shared.c)
  26. //
  27. //____________________________________________________________________________
  28. #include "..\pch\headers.hxx"
  29. #pragma hdrstop
  30. HANDLE
  31. MapHandle(
  32. HANDLE hData,
  33. DWORD dwSource,
  34. DWORD dwDest,
  35. DWORD dwDesiredAccess,
  36. DWORD dwFlags)
  37. {
  38. HANDLE hSource = NULL;
  39. HANDLE hDest = NULL;
  40. HANDLE hNew = NULL;
  41. BOOL fOk;
  42. if (dwSource == GetCurrentProcessId())
  43. hSource = GetCurrentProcess();
  44. else
  45. hSource = OpenProcess( PROCESS_DUP_HANDLE, FALSE, dwSource);
  46. if (!hSource)
  47. goto DoExit;
  48. if (dwDest == GetCurrentProcessId())
  49. hDest = GetCurrentProcess();
  50. else
  51. hDest = OpenProcess( PROCESS_DUP_HANDLE, FALSE, dwDest);
  52. if (!hDest)
  53. goto DoExit;
  54. fOk = DuplicateHandle( hSource, hData,
  55. hDest, &hNew,
  56. dwDesiredAccess,
  57. FALSE, dwFlags | DUPLICATE_SAME_ACCESS);
  58. if (!fOk)
  59. hNew = (HANDLE)NULL;
  60. DoExit:
  61. if (hSource && dwSource != GetCurrentProcessId())
  62. CloseHandle(hSource);
  63. if (hDest && dwDest != GetCurrentProcessId())
  64. CloseHandle(hDest);
  65. return hNew;
  66. }
  67. HANDLE
  68. SCHEDAllocShared(
  69. LPCVOID lpvData,
  70. DWORD dwSize,
  71. DWORD dwDestinationProcessId)
  72. {
  73. HANDLE hData;
  74. SHMAPHEADER* lpmh;
  75. HANDLE hUsableData;
  76. //
  77. // Make a filemapping handle with this data in it.
  78. //
  79. hData = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,0,
  80. dwSize+sizeof(SHMAPHEADER),NULL);
  81. if (hData == NULL)
  82. {
  83. // DebugMsg...
  84. return NULL;
  85. }
  86. lpmh = (SHMAPHEADER *)MapViewOfFile(hData, FILE_MAP_READ | FILE_MAP_WRITE,
  87. 0, 0, 0);
  88. if (!lpmh)
  89. {
  90. // DebugMsg...
  91. CloseHandle(hData);
  92. return NULL;
  93. }
  94. lpmh->dwSize = dwSize;
  95. if (lpvData)
  96. memcpy((LPVOID)(lpmh+1),lpvData,dwSize);
  97. UnmapViewOfFile(lpmh);
  98. hUsableData = MapHandle(hData,
  99. GetCurrentProcessId(),
  100. dwDestinationProcessId,
  101. FILE_MAP_ALL_ACCESS,
  102. DUPLICATE_CLOSE_SOURCE);
  103. return hUsableData;
  104. }
  105. LPVOID
  106. SCHEDLockShared(
  107. HANDLE hData,
  108. DWORD dwSourceProcessId)
  109. {
  110. SHMAPHEADER* lpmh;
  111. HANDLE hUsableData;
  112. hUsableData = MapHandle(hData,dwSourceProcessId,GetCurrentProcessId(),FILE_MAP_ALL_ACCESS,0);
  113. //
  114. // Now map that new process specific handle and close it
  115. //
  116. lpmh = (SHMAPHEADER*)MapViewOfFile(hUsableData,
  117. FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  118. CloseHandle(hUsableData);
  119. if (!lpmh)
  120. return NULL;
  121. return (LPVOID)(lpmh+1);
  122. }
  123. BOOL
  124. SCHEDUnlockShared(
  125. LPVOID lpvData)
  126. {
  127. SHMAPHEADER* lpmh = (SHMAPHEADER*)lpvData;
  128. //
  129. // Now just unmap the view of the file
  130. //
  131. return UnmapViewOfFile(lpmh-1);
  132. }
  133. BOOL
  134. SCHEDFreeShared(
  135. HANDLE hData,
  136. DWORD dwSourceProcessId)
  137. {
  138. HANDLE hUsableData;
  139. //
  140. // The below call closes the original handle in whatever process it
  141. // came from.
  142. //
  143. hUsableData = MapHandle(hData,dwSourceProcessId,
  144. GetCurrentProcessId(),
  145. FILE_MAP_ALL_ACCESS,DUPLICATE_CLOSE_SOURCE);
  146. //
  147. // Now free up the local handle
  148. //
  149. return CloseHandle(hUsableData);
  150. }