A complete shared memory class

xiaoxiao2021-03-06  105

/ ************************************************** ***************** file name: ffmclass.h description: Share memory class -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------- Author: fei ZHAODONG Create Time: 2004/3/25 10:00:00 Version: 1.1 Modifier: Activities: Update List: 2004/3/30 PM 02:59:45 **** *********************************************************** ************* /

// ffmclass.h: interface for the cffmclass.////

#ifndef fzd_ffmclass_h # define fzd_ffmclass_h

#include

//// macro definition

/ / The following is a shared memory definition

#define default_filename null // Default file name #define default_mapname "_fzd_map_" // Default shared memory name #define default_mapsize (0xfffff 1) // Default shared memory size

Const DWORD NETRGUSER_CFM_CODE = 0x1211dBFF; // Check code, used for command data const dword netrguser_cmd_none = 0; // Initialization command code, no instruction

/ / The following is the error code definition

#define error_len 256 // Error Description Length

#define error_invalid_cmdcode 0xe00001FF // Exquisite shared memory #define error_no_mapfile 0xe00002FF // Unallocated Shared memory file #define error_INVALID_CFMCODE 0xE00003FF // check code does not match

//// memory file format definition

#pragma pack (1)

// Memory file format for storing command data type type; // check code DWORD NCOMMANDER {DWORD DWORD NCOMMANDER {DWORD DWORD NCOMMANDER {DWORD DWORDT NCOMMANDER {dword DWORD NCOMMANDER {dword dword nCommandcode; // Directive ID; // Data size

Byte dwreserved [19]; ​​// Reserved BYTE BINFO [1]; // Data start address

_TagData_Header () {dwconfirmcode = NETRGUSER_CFM_CODE; NCOMMANDCODE = NETRGUSER_CMD_NONE; DWDATASIZE = 0;

ZeromeMory (dwreserved, 19); zeromemory (binfo, 1);}} Data_Header, * LPDATA_HEADER;

TypedEf DWORD (WinAPI * Psentriesinacl) (Ulong, PEXPLICIT_ACCESS, PACL, PACL *);

// The shared memory file format for storing response data (not) typef struct _tagans_header {} ans_header, * lpans_header;

#pragma pack () //// Class definition, shared memory server class cffmserver {public: cffmserver (); Virtual ~ cffmserver (); cffmserver (char * szfilename, char * szmapname, dWord dwsize);

Protected: psentriesinacl m_fnpsetentriesinacl; handle m_hfile; // Map file handle handle m_hfilemap; // memory file handle LPVOID M_LPFILEMAPBUFFER; / / Buffer pointer

Char * m_pfilename; // Mapping file name char * m_pmapname; // memory file name DWORD M_DWSIZE; / / Buffer size

BOOL M_BCREATEFLAG; // Have you created shared memory DWORD M_DWLASTERROR; / / Error Code

Private: void _init (); // Initialization parameter BOOL _ISWINNTLATER (); // Judgment the current operating system

public: BOOL Create (char * szFileName = DEFAULT_FILENAME, char * szMapName = DEFAULT_MAPNAME, DWORD dwSize = DEFAULT_MAPSIZE); // Create a shared memory LPVOID GetBuffer (); // get memory file pointer DWORD GetSize (); // get memory file size Void destory (); // Destroy existing shared memory

Bool Writecmddata (// Write Command Data DWORD NCOMMANDCODE, DWORD DWDATASIZE, Const LPVOID PBUF;

//// class definition, the shared memory on the client class CFFMClient {public: CFFMClient (); virtual ~ CFFMClient (); CFFMClient (DWORD dwAccess, char * szMapName, DWORD dwSize); protected: HANDLE m_hFileMap; // file handle memory LPVOID m_lpfilemapbuffer; // Memory file pointer char * m_pmapname; // memory file name

BOOL m_bOpenFlag; // whether to open a memory file DWORD m_dwLastError; // Error code private: void _Init (); // initialization parameters public: BOOL Open (DWORD dwAccess = FILE_MAP_READ, char * szMapName = DEFAULT_MAPNAME, DWORD dwSize = 0 ); // Open a memory file LPVOID getBuffer (); // Get the pointer Void Destory () of the current memory file; // Close the current access to the memory file

Bool getcmddataize; // reads command data size BOOL readcmddata (// read command data DWORD DWCommandcode, DWORD DWBUFSIZE, LPVOID Poutbuf);

#ndif // fzd_ffmclass_h / ******************************************************** ********************* File Name: FFMClass.cpp Description: Share memory class ------------- -------------------------------------------------- Author: Fei Zhaodong Create Time: 2004/3/25 10:00:00 Version: 1.0 Modifier: Activities: Update List: 2004/3/29 02:59: 45 ********************************************************** ***************** / // ffmclass.cpp: importation of the cffmclass.////

#include "stdafx.h" #include "ffmclass.h" #include

//// cffmserver //

Cffmserver :: cffmserver () {m_dwlasterror = 0; m_fnpsetentriesinacl = null; _init ();

Cffmserver :: ~ cffmserver () {destory ();

Cffmserver :: Cffmserver (CHAR * SZMAPNAME, DWORD DWSIZE) {// Create a shared memory block with custom settings_init (); Create (SZFileName, Szmapname, dwsize);}

// Initialize each parameter void cffmserver :: _ init () {m_hfile = null; m_hfilemap = null; m_lpfilemapbuffer = null;

m_pfilename = null; m_pmapname = NULL; m_dwsize = 0;

m_bcreateflag = false;

// above the operating system determines whether NT4.0 BOOL CFFMServer :: _ IsWinNTLater () {OSVERSIONINFO Ver; BOOL bAbleVersion = FALSE; Ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (GetVersionEx (& Ver)) {if (Ver.dwPlatformId == Ver_Platform_WIN32_NT && Ver.dwmajorversion> = 4) {BableVersion = true;}} else {m_dwlasterror = getLastError ();}

Return BableVersion;}

// current shared memory is released, and re-initialization parameters void CFFMServer :: Destory () {if (m_lpFileMapBuffer = NULL!) {UnmapViewOfFile (m_lpFileMapBuffer); m_lpFileMapBuffer = NULL;} if (m_hFileMap = NULL!) {CloseHandle (m_hFileMap); m_hfilemap = null;} if (m_hfile && m_hfile! = invalid_handle_value) {closeHandle (M_HFILE); m_hfile = null;} if (m_pfilename! = null) {free (m_pfilename); m_pfilename = null;}

IF (m_pmapname! = null) {free (m_pmapname); m_pmapname = null;}

_Init ();

// Create a shared memory block BOOL CFFMSERVER :: Create (Char * SZFileName, CHAR * SZMAPNAME, DWORD DWSIZE) {// Release Existing shared memory block if (m_bcreateflag) {destory ();

// Copy each parameter IF (SZFileName) {m_pfilename = _STRDUP (SZFileName);

IF (szmapname) {m_pmapname = _STRDUP (szmapname);} else {m_pmapname = _STRDUP (Default_mapname);

IF (DWSIZE> 0) {m_dwsize = dwsize;} else {m_dwsize = default_mapsize;}

// create the shared memory if (m_pFileName) {m_hFile = CreateFile (m_pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);} else {// default, create a shared memory page file M_HFILE = INVALID_HANDLE_VALUE;} if (_ISwinntlater ()) {// set DACL

Const int Num_aces = 2; // Number if Aces Int Dacl // Evryone - Read // CREATOR - FULL Access // Initialization Parameters PSID PeveryOnSid = NULL; // Everyone Group SID PSID PCREATORSID = NULL; // Creator Group Group SID PACL PFILEMAPACL = NULL; // Prepare the DACL PSecurity_Descriptor PSD = NULL; // Memory File Sa Explicit_Access EA [Num_aces]; // External Access Structure

BOOL bHasErr = FALSE; // Return value // create the SID SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; SID_IDENTIFIER_AUTHORITY SIDAuthCreator = SECURITY_CREATOR_SID_AUTHORITY;! // Evryone if (AllocateAndInitializeSid (& SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, & pEveryoneSID)) {bHasErr = TRUE; goto Finish;} // Creator if (AllocateAndInitializeSid (& SIDAuthCreator, 1, SECURITY_CREATOR_OWNER_RID, 0, 0, 0, 0, 0, 0, 0, & pCreatorSID)) {bHasErr = TRUE!; GOTO FINISH;} // Pack Ace ZeromeMory (& EA, NUM_ACES * SIZEOF (EXPLICIT_ACCESS)); // S-1-1-0 evryone, only read permissions EA [0] .grface | generic_write; ea [0]. grfAccessMode = SET_ACCESS; ea [0] .grfInheritance = NO_INHERITANCE; ea [0] .Trustee.TrusteeForm = TRUSTEE_IS_SID; ea [0] .Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea [0] .Trustee.ptstrName = (LPTSTR) pEveryoneSID; / / S-1-3-0 Creator Owner, full permissions EA [1] .grfacessPermissions = standard_rights_all; ea [1] .grfa ccessMode = SET_ACCESS; ea [1] .grfInheritance = NO_INHERITANCE; ea [1] .Trustee.TrusteeForm = TRUSTEE_IS_SID; ea [1] .Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea [1] .Trustee.ptstrName = (LPTSTR) pCreatorSID; / / create and populate the ACL if (NULL == m_fnpSetEntriesInAcl) {HINSTANCE hLib = :: LoadLibrary ( "Advapi32.dll"); if (NULL = hLib!) {m_fnpSetEntriesInAcl = (PSetEntriesInAcl) GetProcAddress (hLib, "SetEntriesInAclA");: : FreeElibrary (HLIB); HLIB = NULL;}

} If (ERROR_SUCCESS = m_fnpSetEntriesInAcl (NUM_ACES, ea, NULL, & pFileMapACL)!) {BHasErr = TRUE; goto Finish;} // Create and initialize the SD pSD = (PSECURITY_DESCRIPTOR) LocalAlloc (LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); if (NULL == pSD ) {bHasErr = TRUE; goto Finish;} if) {bHasErr = TRUE; goto Finish;} (InitializeSecurityDescriptor (pSD, SECURITY_DESCRIPTOR_REVISION!) // added to the ACL to the SD if (SetSecurityDescriptorDacl (pSD, TRUE, // fDaclPresent flag! pFileMapACL, FALSE)) // not a default DACL {bHasErr = TRUE; goto Finish;} // set SA saFileMap.nLength = sizeof (SECURITY_ATTRIBUTES); saFileMap.bInheritHandle = FALSE; saFileMap.lpSecurityDescriptor = pSD; // Create a shared memory file if (m_hFile = NULL!) {m_hFileMap = CreateFileMapping (m_hFile, & saFileMap, PAGE_READWRITE, 0, m_dwSize, m_pMapName); if (NULL == m_hFileMap) {m_hFileMap = OpenFileMapping (FILE_MAP_READ | FILE_MAP_WRITE, TRUE, m_pMapName); if (NULL == M_HFILEMAP) {M_dwlasterror = getLastError (); Destory (); goto finish;}}} finish:} (psd! = Null) {localfree (PSD);} if (pfilemapacl! = Null) {localfree (pfilemapacl);} if (pevery " ! = Null) {freeesid;} if (pcreatorsid! = Null) {freeesid (pcreatorsid);} if (bhaserr) {m_dwlasterror = getLastError (); return false;}} else {// Create a shared memory file IF (m_hFile) {m_hFileMap = CreateFileMapping (m_hFile, NULL, PAGE_READWRITE, 0, m_dwSize, m_pMapName); if (NULL == m_hFileMap) {m_dwLastError = GetLastError (); Destory (); return FALSE;}}}

// map file pointer to the user if (m_hFileMap) {m_lpFileMapBuffer = MapViewOfFile (m_hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, m_dwSize); if (NULL == m_lpFileMapBuffer) {m_dwLastError = GetLastError (); Destory (); return FALSE;}} m_bcreateflag = true; return true;}

// Get memory file pointer LPVOID CFFMSERVER :: getBuffer () {return (m_lpfilemapbuffer) :( null) :( null);}

// Get memory file size DWORD CFFMSERVER :: getSize () {return m_dwsize;}

BOOL CFFMServer :: WriteCmdData (DWORD nCommandCode, DWORD dwDataSize, const LPVOID pBuf) {// if reasonableness test data (NULL == GetBuffer ()) {m_dwLastError = ERROR_NO_MAPFILE; SetLastError (ERROR_NO_MAPFILE); return FALSE;} if (NETRGUSER_CMD_NONE == nCommandCode) {m_dwLastError = ERROR_INVALID_CMDCODE; SetLastError (ERROR_INVALID_CMDCODE); return FALSE;} if (dwDataSize> 0 && pBuf == NULL) {m_dwLastError = ERROR_INVALID_USER_BUFFER; SetLastError (ERROR_INVALID_USER_BUFFER); return FALSE;} if (dwDataSize sizeof (DATA_HEADER > GetSize ()) {m_dwlasterror = error_buffer_overflow; setLastError (Error_Buffer_Overflow); Return False;

// Fill in the data structure

// Header DATA_HEADER dataHeader; dataHeader.nCommandCode = nCommandCode; dataHeader.dwDataSize = dwDataSize; ZeroMemory (GetBuffer (), GetSize ()); memcpy (GetBuffer (), & dataHeader, sizeof (DATA_HEADER));

// Data block LPDATA_HEADER PDATA = (LPDATA_HEADER) getBuffer (); Memcpy (PData-> Binfo, PBUF, DWDATASIZE);

Return True;}

//// cffmclient ///

CffmClient :: cffmclient () {m_dwlasterror = 0; _INIT ();}

Cffmclient :: ~ cffmclient () {destory ();

CffmClient :: CffmClient (DWORD DWACCESS, CHAR * SZMAPNAME, DWORD DWSIZE) {_init ();

IF (!!) {destory ();}}

// Initialization parameter void cffmclient :: _ init () {m_hfilemap = null; m_lpfilemapbuffer = null; m_pmapname = NULL;

m_bopenflag = false;}

// Close the current memory access file void CFFMClient :: Destory () {if (m_lpFileMapBuffer) {UnmapViewOfFile (m_lpFileMapBuffer); m_lpFileMapBuffer = NULL;} if (m_hFileMap) {CloseHandle (m_hFileMap); m_hFileMap = NULL;}

IF (m_pmapname) {free (m_pmapname); m_pmapname = null;}

_Init ();

// Open a file memory BOOL CFFMClient :: Open (DWORD dwAccess, char * szMapName, DWORD dwSize) {if (m_bOpenFlag) {Destory ();} if (! SzMapName = NULL) {m_pMapName = _strdup (szMapName);} else {M_pmapname = _STRDUP (Default_mapname);} m_hfilemap = OpenFileMapping (dwaccess, true, m_pmapname); if (null == m_hfilemap) {m_dwlasterror = getLastError ();

Destory (); return false;} m_lpfilemapbuffer = mappviewoffile (m_hfilemap, dwaccess, 0, 0, dwsize); if (null == m_lpfilemapbuffer) {m_dwlasterror = getLastError ();

Destory (); return false;}

m_bopenflag = true; return true;}

/ / Get the pointer of the current memory file lpvoid cffmclient :: getBuffer () {return (m_lpfilemapbuffer) :( null) :( null)

// Read command data size BOOL CFFMCLIENT :: getCmddataize (DWORD * pDataSize) {assert (pDataSize! = Null); * pDataSize = 0;

LPDATA_HEADER pHeader = (LPDATA_HEADER) GetBuffer (); if (NULL == pHeader) {m_dwLastError = ERROR_NO_MAPFILE; SetLastError (ERROR_NO_MAPFILE); return FALSE;} if (NETRGUSER_CFM_CODE = pHeader-> dwConfirmCode!) {M_dwLastError = ERROR_INVALID_CFMCODE; SetLastError (ERROR_INVALID_CFMCODE) ; return FALSE;} if (NETRGUSER_CMD_NONE == pHeader-> nCommandCode) {m_dwLastError = ERROR_INVALID_CMDCODE; SetLastError (ERROR_INVALID_CMDCODE); return FALSE;} * pDataSize = pHeader-> dwDataSize; return TRUE;}

// Read command data BOOL CFFMClient :: ReadCmddata (DWORD DWCMMANDCODE, DWORD DWBUFSIZE, LPVOID Poutbuf) {Assert (poutbuf! = Null);

LPDATA_HEADER pHeader = (LPDATA_HEADER) GetBuffer (); if (NULL == pHeader) {m_dwLastError = ERROR_NO_MAPFILE; SetLastError (ERROR_NO_MAPFILE); return FALSE;} if (NETRGUSER_CFM_CODE = pHeader-> dwConfirmCode!) {M_dwLastError = ERROR_INVALID_CFMCODE; SetLastError (ERROR_INVALID_CFMCODE) ; return FALSE;} if (NETRGUSER_CMD_NONE == pHeader-> nCommandCode) {m_dwLastError = ERROR_INVALID_CMDCODE; SetLastError (ERROR_INVALID_CMDCODE); return FALSE;} if (pHeader-> dwDataSize> dwBufSize) {m_dwLastError = ERROR_BUFFER_OVERFLOW; SetLastError (ERROR_BUFFER_OVERFLOW); return FALSE } If (phet-> ncommandcode! = Dwcommandcode) {m_dwlasterror = error_INVALID_CMDCODE; setLastError (Error_INVALID_CMDCODE); RETURN FALSE;}

ZeromeMory (poutbuf, dwbufsize);

// Copy data to the buffer Memcpy (poutbuf, phet-> binfo, phet-> dwdatasize); return true;}

转载请注明原文地址:https://www.9cbs.com/read-104095.html

New Post(0)