A complete shared memory class

xiaoxiao2021-03-06  102

/ ************************************************** ***************** 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 is defined #define DEFAULT_FILENAME NULL // default file name #define DEFAULT_MAPNAME "_FZD_MAP_" // default shared memory name #define DEFAULT_MAPSIZE (0xFFFF 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 // below 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 // Unassigned shared memory file #define error_ Invalid_cfmcode 0xE00003FF // check code mismatch //// memory file format definition #pragma pack (1) // Memory file format for storing command data TypeDef struct _tagdata_header {dword dword ncommandcode; // check code DWORD NCOMMANDE; / / command identification code DWORD dwDataSize; // data size of 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; TYPDEF DWORD (Ulong, PEXPLICIT_ACCESS, PACL, PACL *); // Used to store a shared memory file format for replied data (Not) TYPEDEF STRUCT _TAGANS_HEADER {} ANS_HEADER, * LPANS_HEADER;

#pragma pack () //// class definition, the shared memory server class CFFMServer {public: CFFMServer (); virtual ~ CFFMServer (); CFFMServer (char * szFileName, char * szMapName, DWORD dwSize); protected: PSetEntriesInAcl 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 (); // Judge the current operating system public: BOOL Create (char * szfilename = default_filename, Char * szmapname = default_mapname, dword dwsize = default_mapsize; // New Share Memory LPVOID GetBuffer (); // Get memory file pointer DWORD getSize (); // Get memory file size void destory (); // destroy existing Share memory BOOL WRITECMDDATA (// Write Command Data DWORD NCOMMAMMANDCODE, DWORD DWDATASUE, CONST LPVOID PBUF);}; //// Class Definition, Shared Memory Client CFFFMCLIENT {public: cffmclient (); Virtual ~ Cffmclient (); CffmClient (DWORD DWACCESS, CHAR * SZMAPNAME, DWORD DWSIZE) Protected: handle m_hfilemap; // memory file handle LPVOID M_LPFILEMAPBUFFER; // memory file pointer char * m_pmapname; // memory file name BOOL M_BOPENFLAG; // has opened a memory file DWORD M_DWLASTERROR; // Error code private: void _INIT (); // Initialization Parameter 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 of the current memory file Destory (); // Turn off the current Bool getCmdDataSize (DWORD * PDATASIZE) of the memory file; // read the command data size bool readcmdata (// read command data DWORD DWCommandcode, DWORD DWBUFSIZE, LPVOID Poutbuf);}; Endif // FZD_FFMCLASS_H / ********************************************************* ******************** File Name: FFMClass.cpp

Description: Share memory class ------------------------------------------------------------------------------------------------------ ------------------ Author: fei zhaodong crete 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_fnpsentries = 0; m_fnpsentriesinacl = null; _init (); Cffmserver :: ~ cffmserver () {Destory ();} cffmserver :: cffmserver (char * szfilename, char * szmapname, dWord dwsize) {// Create a shared memory block with custom settings_init (); Create (SzFileName, Szmapname, dwSize);} // initialize various parameters void CFFMServer :: _ Init () {m_hFile = NULL; m_hFileMap = NULL; m_lpFileMapBuffer = NULL; m_pFileName = NULL; m_pMapName = NULL; m_dwSize = 0; m_bCreateFlag = FALSE;} // determines whether NT4.0 or more operating system bool cffmserver :: _ iswinntlater () {OsversionInfo Ver; Bool BableVersion = false; Ver.dwosVersionInfo Size = 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 = null;} if (m_pfilename! =

NULL) {free (m_pfilename); m_pfilename = null;} f (m_pmapname! = Null) {free (m_pmapname); m_pmapname = null;} _init ();} // Create a shared memory block BOOL CFFMSERVERVER :: Create (char * SZFileName, Char * SZMAPNAME, DWORD DWSIZE) {// Release Existing shared memory block if (m_bcreateflag) {Destory ();} // copy the respective 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 m_hFile in the paging file = INVALID_HANDLE_VALUE;} if (_IsWinNTLater ()) {// set DACL const INT NUM_ACES = 2; // Number if Aces Int Dacl // Evryone - Read // Creat - Full Access // Initialization Parameters PSID PeveryOnSid = NULL; // Everyone Group SID PSID P CreatorSID = NULL; // creator group SID PACL pFileMapACL = NULL; // Prepare the new memory files DACL PSECURITY_DESCRIPTOR pSD = NULL; SD SECURITY_ATTRIBUTES saFileMap // memory files; SA EXPLICIT_ACCESS // memory file 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, & prop)) {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 evyone, only read permission EA [0] .grface | generic_write; ea [0] .grface = 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 rights ea [1] .grfAccessPermissions = STANDARD_RIGHTS_ALL; ea [1] .grfAccessMode = 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) GetProcAddres S (HLIB, "STENTRIESINACL"); :: freeelibrary (hlib); hlib = null;}} f (Error_suCcess! = m_fnpsetentriesinacl (num_acs, ea, null, & pfilemapacl) {bhaserr = true; goto finish;} // creation and initialize the SD pSD = (PSECURITY_DESCRIPTOR) LocalAlloc (LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); if (NULL == pSD) {bHasErr = TRUE; goto Finish;} (! InitializeSecurityDescriptor (pSD, SECURITY_DESCRIPTOR_REVISION)) if {bHasErr = TRUE; goto Finish;} // Add ACL to SD to IF (! SetSecurityDescriptOracl (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;}}}}}}} {localfree (psd);} f (pfilemapacl! = Null) {localfree (pfilemapacl);} if (peveryonesid! = Null) {FREESID (PeveryOsID); } if (pCreatorSID = NULL!) {FreeSid (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) (m_lpFileMapBuffer):? (NULL);} // Get memory file size DWORD CFFMSERVER :: getSize () {return m_dwsize;} Bool Cffmser :: Writecmddata (DWord DWDataSize, Const LPVOID PBUF) {// Test data rationality IF (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 of a file 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, C HAR * SZMAPNAME, DWORD DWSIZE) {_init (); if (! Open (dwen (dwen (dwin (dwen (dwin (dwsize)) {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 memory file 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 = MapViewOfFile (m_hFileMap, dwAccess, 0, 0, dwSize); if (NULL == m_lpFileMapBuffer) {m_dwLastError = GetLastError (); Destory (); return FALSE;} m_bOpenFlag = TRUE; return TRUE;} / / get a pointer to the current memory file LPVOID CFFMClient :: GetBuffer () {return (m_lpFileMapBuffer) (m_lpFileMapBuffer) :( NULL);?} // read command data size BOOL CFFMClient :: GetCmdDataSize (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_dwlaster = error; setLastError (Error_INVALID_CFMCODE); RETURN FALSE;} IF (NETRGUSER_CMD_NONE == PHEADER-> NCOMMA ndCode) {m_dwLastError = ERROR_INVALID_CMDCODE; SetLastError (ERROR_INVALID_CMDCODE); return FALSE;} * pDataSize = pHeader-> dwDataSize; return TRUE;} // read command data BOOL CFFMClient :: ReadCmdData (DWORD dwCommandCode, 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_dwlaster = error; 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 (pHeader-> nCommandCode =! dwCommandCode) {m_dwLastError = ERROR_INVALID_CMDCODE; SetLastError (ERROR_INVALID_CMDCODE); return FALSE;} ZeroMemory (pOutBuf, dwBufSize); // copy data to the buffer memcpy (pOutBuf, pHeader-> bInfo, pHeader-> dwDataSize); return TRUE;} OF BLOG:

http://blog.9cbs.net/fzd999/

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

New Post(0)