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.

88 lines
1.9 KiB

  1. // ----------------------------------------------------------------------------
  2. //
  3. // MemFile.c
  4. //
  5. // Memory handling module
  6. //
  7. // Build: May-12-97
  8. //
  9. // Author: J. Eckhardt
  10. // This code was written for ECO Kommunikation Insight
  11. // Copyright (c) 1997-1999 Microsoft Corporation
  12. // ----------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <TCHAR.h>
  15. #include <WinSvc.h>
  16. #include "_UMTool.h"
  17. // -------------------------------------------------------------------
  18. // task independent memory
  19. // -------------------------------------------------------------------
  20. HANDLE CreateIndependentMemory(LPTSTR name, DWORD size, BOOL inherit)
  21. {
  22. LPSECURITY_ATTRIBUTES psa = NULL;
  23. obj_sec_attr_ts sa;
  24. HANDLE hFileMap;
  25. if (inherit)
  26. {
  27. psa = &sa.sa;
  28. InitSecurityAttributesEx(&sa, GENERIC_ALL, GENERIC_READ|GENERIC_WRITE);
  29. }
  30. hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE, 0, size,name);
  31. DBPRINTF(TEXT("CreateIndependentMemory: CreateFileMapping(%s) returns %d\r\n"), name, GetLastError());
  32. if (inherit)
  33. ClearSecurityAttributes(&sa);
  34. if (hFileMap == INVALID_HANDLE_VALUE)
  35. {
  36. return NULL;
  37. }
  38. return hFileMap;
  39. }
  40. // ---------------------------
  41. LPVOID AccessIndependentMemory(LPTSTR name, DWORD size, DWORD dwDesiredAccess, PDWORD_PTR accessID)
  42. {
  43. HANDLE hMap;
  44. LPVOID n;
  45. *accessID = 0;
  46. hMap = OpenFileMapping(dwDesiredAccess, FALSE, name);
  47. if (!hMap)
  48. {
  49. return NULL;
  50. }
  51. n = MapViewOfFile(hMap, dwDesiredAccess, 0, 0, size);
  52. if (!n)
  53. {
  54. CloseHandle(hMap);
  55. return NULL;
  56. }
  57. *accessID = (DWORD_PTR)hMap;
  58. return n;
  59. }
  60. // ---------------------------
  61. void UnAccessIndependentMemory(LPVOID data, DWORD_PTR accessID)
  62. {
  63. if (data)
  64. UnmapViewOfFile(data);
  65. if (accessID)
  66. CloseHandle((HANDLE)accessID);
  67. }
  68. // ---------------------------
  69. void DeleteIndependentMemory(HANDLE id)
  70. {
  71. if (id)
  72. CloseHandle(id);
  73. }