Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include "MCCLSharedMemory.h"
- HANDLE hMapFile;
- LPSTR hMapView;
- //Function : MCCLCreateSharedMemory
- //Description: Creates a shared memory with given name and size
- //Input : MapSize = Size of the shared memory (if it's zero, we'll use a defual size)
- //Output: Void
- void MCCLCreateSharedMemory(int MapSize)
- {
- if (MapSize == 0)
- MapSize = 1048576;
- hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE,
- 0,
- (DWORD)MapSize,
- "Global\\MyFileMappingObject");
- hMapView = (LPSTR) MapViewOfFile(hMapFile,
- FILE_MAP_ALL_ACCESS,
- 0,
- 0,
- 0);
- }
- //Function : MCCLOpenSharedMemory
- //Description : Opens the shared memory with the given name if it exists
- //Input : None
- //Output : Void
- void MCCLOpenSharedMemory()
- {
- hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,
- FALSE,
- "Global\\MyFileMappingObject");
- hMapView = (LPSTR) MapViewOfFile(hMapFile,
- FILE_MAP_ALL_ACCESS, //FILE_MAP_WRITE | FILE_MAP_READ,
- 0,
- 0,
- 0);
- }
- //Function : MCCLDestroySharedMemory
- //Destorys the shared memory object, handles, etc
- //Input : None
- //Output : Void
- void MCCLDestroySharedMemory ()
- {
- UnmapViewOfFile(hMapView)
- CloseHandle(hMapFile);
- }
- //Function : WriteOnSharedMemory
- //Description : Writes a block of data of given size into the shared memory
- //Input : data = The block of data to be written to the shared memory
- // WriteSize = size of the data to be written into the shared memory in bytes
- //Output : void
- void WriteOnSharedMemory(char* data, int WriteSize)
- {
- CopyMemory( (PVOID)hMapView, (const VOID *)data, (SIZE_T)WriteSize);
- }
- //Function : ReadFromSharedMemory
- //Description : Reads a block of data of given size from the shared memory
- //Input : data = The block of data to be read from the shared memory
- // ReadSize = size of the data to be read from the shared memory in bytes
- //Output : void
- void ReadFromSharedMemory(char *data, int ReadSize)
- {
- CopyMemory( (PVOID)data, (const VOID *)hMapView, (SIZE_T)ReadSize);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement