Advertisement
Guest User

Alireza

a guest
Apr 2nd, 2009
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6.  
  7. #include "MCCLSharedMemory.h"
  8.  
  9. HANDLE hMapFile;
  10. LPSTR hMapView;
  11.  
  12.  
  13.  
  14. //Function : MCCLCreateSharedMemory
  15. //Description: Creates a shared memory with given name and size
  16. //Input : MapSize = Size of the shared memory (if it's zero, we'll use a defual size)
  17. //Output: Void
  18. void MCCLCreateSharedMemory(int MapSize)
  19. {
  20.  
  21.     if (MapSize == 0)
  22.         MapSize = 1048576;
  23.  
  24.     hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
  25.         NULL,
  26.         PAGE_READWRITE,
  27.         0,
  28.         (DWORD)MapSize,
  29.         "Global\\MyFileMappingObject");
  30.  
  31.     hMapView = (LPSTR) MapViewOfFile(hMapFile,
  32.         FILE_MAP_ALL_ACCESS,  
  33.         0,
  34.         0,
  35.         0);
  36. }    
  37.  
  38. //Function : MCCLOpenSharedMemory
  39. //Description : Opens the shared memory with the given name if it exists
  40. //Input : None
  41. //Output : Void
  42. void MCCLOpenSharedMemory()
  43. {
  44.    
  45.    hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,
  46.         FALSE,
  47.         "Global\\MyFileMappingObject");
  48.  
  49.     hMapView = (LPSTR) MapViewOfFile(hMapFile,
  50.         FILE_MAP_ALL_ACCESS, //FILE_MAP_WRITE | FILE_MAP_READ,
  51.         0,
  52.         0,
  53.         0);
  54.  
  55. }
  56.  
  57. //Function : MCCLDestroySharedMemory
  58. //Destorys the shared memory object, handles, etc
  59. //Input : None
  60. //Output : Void
  61. void MCCLDestroySharedMemory ()
  62. {
  63.         UnmapViewOfFile(hMapView)
  64.  
  65.     CloseHandle(hMapFile);
  66. }
  67.  
  68. //Function : WriteOnSharedMemory
  69. //Description : Writes a  block of data of given size into the shared memory
  70. //Input : data = The block of data to be written to the shared memory
  71. //        WriteSize = size of the data to be written into the shared memory in bytes
  72. //Output : void
  73. void WriteOnSharedMemory(char* data, int WriteSize)
  74. {
  75.  
  76.     CopyMemory( (PVOID)hMapView, (const VOID *)data, (SIZE_T)WriteSize);
  77.  
  78. }
  79.  
  80. //Function : ReadFromSharedMemory
  81. //Description : Reads a  block of data of given size from the shared memory
  82. //Input : data = The block of data to be read from the shared memory
  83. //        ReadSize = size of the data to be read from the shared memory in bytes
  84. //Output : void
  85.  
  86. void ReadFromSharedMemory(char *data, int ReadSize)
  87. {
  88.  
  89.     CopyMemory( (PVOID)data, (const VOID *)hMapView, (SIZE_T)ReadSize);
  90.  
  91. }
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement