One memory file mapping user class

zhaozj2021-02-08  515

// uc_filemapping.h:! Interface for the UC_FILEMAPPING class.////#if defined (AFX_UC_FILEMAPPING_H__72FE31B2_7B02_442F_A754_66427E1C5946__INCLUDED _) # define AFX_UC_FILEMAPPING_H__72FE31B2_7B02_442F_A754_66427E1C5946__INCLUDED_ # if _MSC_VER> 1000 # pragma once # endif // _MSC_VER> 1000 //// module: memory mapped files user class // author: Zhang Min // create: 2002.01.16 // Description: // use memory-mapped file sharing memory between processes, // the current 32-bit applications addressing the range of 4GB, does not require the high / / # include "uc_log.h" // Custom return value #define ret_fileopenned ret_user 1 // file has been opened #define ret_filenotopenned RET_USER 2 // File Open #define RET_BUFFERTOBIG RET_USER 3 // Buffer Excessive #define RET_BUFFEROVERFLOW RET_USER 4 // buffer overflow #define RET_FILEPROCESSING RET_USER 5 // file being operated #define RET_OFFSETOVERFLOW RET_USER 6 // // offset overflow shared memory reserved for the predefined length information structure typedef struct tagMapInfo {DWORD dwSizeHigh ; // High file size DWORD DWSIZELOW; // low file size char SZMAppingName [_MAX_PATH]; // Mapping Name tagmapinfo () { dwSizeHigh = dwSizeLow = 0; memset (szMappingName, 0, _MAX_PATH);}} US_MAPINFO, * PUS_MAPINFO; // tab threshold #define HIGH_MAX 0xFFFFFFFE // high maximum #define LOW_MAX 0xFFFFFFFF - sizeof (US_MAPINFO) // low maximum # Define info_len sizeof (US_MapInfo) // header information length #define nophysicalfile 0xfffffffff // No physical file class uc_filemapping: public uc_log {public: dword getsize ();

// // Author: Zhang Min // Create: 2002.01.16 // Function: // mapping file written argument: // [in] nSize buffer size [in] lpBuf buffer // // [in] dwOffsetLow Offset address status // Return: // RET_BADARG parameter illegal illegal // RET_BUFFERTOBIG Cache area excess // RET_BUFFEROVERFLOW buffer overflow // RET_FILENOTOpenned file is not open // RET_FILEPROCESSING file is operating // RET_FILEERR file operation failed // RET_OK operation success / / DWORD Write (LPVOID lpBuf, UINT & nSize, DWORD dwOffsetLow = 0, BOOL isAppend = TRUE); // // author: Zhang Min // create: 2002.01.16 // function: reads mapping file // parameters: // [IN] LPBUF buffer // [in] nsize buffer size // [in] dwoffsetlow offset address status // return: // RET_BADARG parameter illegal // RET_BUFFERTOBIG Cache area excess // RET_BUF FEROVERFLOW buffer overflow // RET_FILENOTOPENNED file does not open // RET_FILEPROCESSING file being operated on file operation failed // // RET_FILEERR success RET_OK operation // DWORD Read (LPVOID lpszBuf, UINT & nSize, DWORD dwOffsetLow = 0); // // Author: Zhang Wei // Created: 2002.01.16 // Function: Close Mapping file // Parameter: // [in] Wantdump Requires Export // Return: // Ret_FileNotopenned file Und opened // RET_FILEERR file operation failed // RET_OK operation Success / / Dword close (bool wantdump = true);

// // Author: Zhang Min // Create: 2002.01.16 // Function: // Open the map file parameters: // [in] lpszMappingName named mapped memory // Return: // RET_BADARG parameters illegal // RET_FILEOPENNED file is already open // // RET_FILEERR file operation failed RET_OK successful operation // DWORD open (LPCTSTR lpszMappingName); // // author: Zhang Min // create: 2002.01.16 // function: // open the map file parameters: // [in ] LPSZFILEPATH Physical File Path // [In] LPSZMAppingName Mapping Memory Name // [In] DWSELOW Low Space Size // Return: // Ret_Badarg Parameter Illegal // Ret_FileOpenned file has opened // RET_FILEERR file operation failed // RET_OK operation success / / DWORD Open (LPCTSTR lpszFilePath, LPCTSTR lpszMappingName, DWORD dwSizeLow); // // author: Zhang Min // create: 2002.01.16 // function: constructor // UC_FILEMAPPING (); // // author: Zhang Min / / Create: 2002.01.16 // Function: Destructor // Virtual ~ uc_filemapping (); protected: handle m_hphysicsfile; // physical file handle; // mapping file handle handle m_hfileop; // file operation mutual exclusion LPVOID M_LPCURSOR; // Mapping Cursor Address pointer LPVOID M_LPADDRESS; // Mapping file address pointer PUS_MAPINFO M_PUSMAPINFO; // Mapping Inside Information BOOL M_ISFILOADED; // Working Status Sign Bool M_IsMyHandle; // Is it a file mapping dword m_dwsysalloc; // system allocation memory PRIVATE: DWORD FLUSHVIEW (); DWORD getErrorMessage ();

// // Author: Zhang Min // Create: 2002.01.16 // Function: // format size and offset parameters: // [in] dwOffsetLow low offset // Return: // RET_BADARG parameters illegal / / RET_FILENOTOPENNED file does not open the file // RET_FILEPROCESSING being operated // RET_OK operation was successful // // // author: Zhang Min // create: 2002.01.16 // function: // move the file pointer mapping parameters: // [in] dwOffsetLow low offset // return: // RET_BADARG // RET_FILENOTOPENNED illegal parameter file is not open // RET_FILEPROCESSING // RET_OK file operation is successful operation //}; # endif // defined (AFX_UC_FILEMAPPING_H__72FE31B2_7B02_442F_A754_66427E1C5946__INCLUDED _) // uc_filemapping.cpp!: implementation of the UC_FILEMAPPING class.////#include "stdafx.h" #include "uc_filemapping.h" #ifdef _DEBUG # undef THIS_FILEstatic char THIS_FILE [] = __ FILE __; # define new DEBUG_NEW # endif //// Construction / Destruction ////Constructor UC_FILEMAPPING :: UC_FILEMAPPING () {WaitForSingleObject (m_hMutex, INFINITE); // initialize a handle m_hPhysicsFile = NULL; m_hMappingFile = NULL; m_hFileOP = NULL; // initialization flag m_isFileLoaded = FALSE; m_isMyHandle = FALSE; // member variable initialization file m_pusMapInfo = NULL; m_lpAddress = NULL; m_lpCursor = NULL; // get the system minimum memory unit SYSTEM_INFO SysInfo; GetSystemInfo (& SysInfo); m_dwSysAlloc = SysInfo.dwAllocationGranularity; ReleaseMutex (m_hMutex);} // destructor UC_FILEMAPPING :: ~ UC_FILEMAPPING () {WaitForsingleObject (m_hmutex, infinite); // File Processing Mutual exclusion WaitForsingleObject (m_hfileop, infinite);

// close mapped memory header pointer if (m_pusMapInfo) delete m_pusMapInfo; // close the handle ReleaseMutex (m_hFileOP); // Only the creator have the right to close the handle if (m_isMyHandle) {CloseHandle (m_hPhysicsFile); CloseHandle (m_hMappingFile); CloseHandle (m_hFileOP);} ReleaseMutex (m_hMutex);} // New file DWORD UC_FILEMAPPING :: Open (LPCTSTR lpszFilePath, LPCTSTR lpszMappingName, DWORD dwSizeLow) {// condition monitoring if (m_isFileLoaded) return RET_FILEOPENNED; // parameter monitoring if (lpszMappingName = = NULL) return RET_BADARG; DWORD dwRet = RET_OK; // Create a file operation mutex handle char szMutex [_MAX_PATH]; memset (szMutex, 0, _MAX_PATH); sprintf (szMutex, "% s_MUTEX", lpszMappingName); m_hFileOP = CreateMutex ( NULL, FALSE, szMutex); WaitForSingleObject (m_hFileOP, INFINITE);! // Create a physical file if (lpszFilePath = NULL) {// new file m_hPhysicsFile = CreateFile (lpszFilePath, GENERIC_READ corresponding | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL NULL); // If the file exists, open an existing file if (m_hPhysicsFile == INVALID_HANDLE_VALUE) {GetErrorMessage (); m_hPhysicsFile = CreateFile (lpszFilePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);}} if (m_hPhysicsFile == INVALID_HANDLE_VALUE) {getErrorMessage (); m_hphysicsfile = null; dwret = RET_FILEERR;} else {// Create a mapping file (actual length than the length of the application length) IF (dwret == RET_OK) {if (m_hphysicsfile == null) { M_HmappingFile = CreateFilemapping ((Handle) NoPhysicalFile, Null, Page_Readwrite, 0, DWSiZELOW, LPSZMAPPINGNAME

} Else {m_hMappingFile = CreateFileMapping (m_hPhysicsFile, NULL, PAGE_READWRITE, 0, dwSizeLow INFO_LEN, lpszMappingName);} if (m_hMappingFile == NULL) {GetErrorMessage (); CloseHandle (m_hPhysicsFile); m_hPhysicsFile = NULL; dwRet = RET_FILEERR;} else {// Get the corresponding mapping address m_lpaddress = mappViewoffile (m_hmappingfile, file_map_all_access, 0, 0, 0); if (m_lpaddress == null) {dwret = RET_FILEERR;} else {// Save Mapping Interior Head Information IF (M_PusMapInfo == NULL) m_pusMapInfo = new US_MAPINFO; m_pusMapInfo-> dwSizeLow = dwSizeLow iNFO_LEN; memset (m_pusMapInfo-> szMappingName, 0, _MAX_PATH); memcpy (m_pusMapInfo-> szMappingName, l pszmappingname, strlen (lpszmappingname)); Memcpy (m_lpaddress, m_pusmapinfo, info_len); // Save the start address and initialization cursor information of the application // This actually actually address, because the header is included, a blank portion is moved to m_lpAddress = m_lpCursor = (LPVOID) ((LPBYTE) m_lpAddress iNFO_LEN); // open setting flag information m_isFileLoaded = TRUE; m_isMyHandle = TRUE;}}}} ReleaseMutex (m_hFileOP); if (! dwRet = RET_OK) CloseHandle (m_hfileop); return dwret;

} // Open an existing memory mapped file name DWORD UC_FILEMAPPING :: Open (LPCTSTR lpszMappingName) {// condition monitoring if (m_isFileLoaded || m_hFileOP) return RET_FILEOPENNED; // parameter monitoring if (lpszMappingName == NULL) return RET_BADARG; DWORD dwRet = RET_OK; // Create a file operation mutex handle char szMutex [_MAX_PATH]; memset (szMutex, 0, _MAX_PATH); sprintf (szMutex, "% s_MUTEX", lpszMappingName); m_hFileOP = OpenMutex (MUTEX_ALL_ACCESS, FALSE, szMutex); if (m_hFileOP == NULL) {return RET_FILENOTOPENNED;} WaitForSingleObject (m_hFileOP, INFINITE); // open mapping file m_hMappingFile = OpenFileMapping (FILE_MAP_ALL_ACCESS, FALSE, lpszMappingName); if (m_hMappingFile == NULL) {dwRet = RET_FILEERR;} else { // Get the corresponding mapping address m_lpaddress = mappViewoffile (m_hmappingfile, file_map_all_access, 0, 0, 0); if (m_lpaddress == null) {dwret = RET_FILEERR;} else {// Get mapping interior head information IF (m_pusmapinfo == null ) m_pusMapInfo = new US_MAPINFO; memcpy (m_pusMapInfo, m_lpAddress, INFO_LEN); // Save the principle address and cursor // create the same time m_lpAddress = m_lpCursor = (LPVOID) ((LPBYTE) m_lpAddress INFO_LEN); // set the opening flag information m_isFileLoaded = TRUE; m_isMyHandle = TRUE;}} ReleaseMutex (m_hFileOP); return dwRet;} // close file DWORD UC_FILEMAPPING :: Close (BOOL wantDump) {// condition monitoring if (m_isFileLoaded == FALSE) return RET_FILENOTOPENNED; DWORD dwRet = RET_OK WaitforsingleObject (m_hfileop, infinite); if (wantdump) Flushview (); delete m_pusmapinfo;

m_pusMapInfo = NULL; m_isFileLoaded = FALSE; ReleaseMutex (m_hFileOP); // only have the right to close the handle creator if (m_isMyHandle == FALSE) {CloseHandle (m_hPhysicsFile); CloseHandle (m_hMappingFile); CloseHandle (m_hFileOP);} return dwRet; } // Read Mapping Document DWORD UC_FILEMAPPING: READ (LPVOID LPBUF, UINT & NSIZE, DWORD DWOFFSETLOW) {// Status Monitor IF (m_isFileLoaded == false) Return Ret_FileNotopenned; // Parameter Monitor IF (LPBUF == Null || nsize == 0) return RET_BADARG; DWORD dwRet = RET_OK; WaitForSingleObject (m_hFileOP, INFINITE); // calculate whether the content overflows UINT nSizeUsed = (UINT) (LPBYTE (m_lpCursor) - LPBYTE (m_lpAddress)); if (nSize dwSizeLow - (UINT) (LPBYTE (m_lpCursor) - LPBYTE (m_lpAddress)) - iNFO_LEN; if (nLeftSize

// Move the cursor if (isAppend) m_lpCursor = (LPVOID) ((LPBYTE) m_lpCursor nSize);} ReleaseMutex (m_hFileOP); return dwRet;} // get an error message DWORD UC_FILEMAPPING :: GetErrorMessage () {DWORD dwRet = GetLastError ( ); LPVOID lpMsgBuf; FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRet, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) & lpMsgBuf, 0, NULL); // Process any inserts in lpMsgBuf // ... . // display the string. // MessageBox (NULL, (LPCTSTSTR) LPMSGBUF, "Error", MB_OK | MB_ICONIORMATION; Trace ("0x% 08x:% S / N", DWRET, LPMSGBUF; // Free the Buffer . LocalFree (lpMsgBuf); return dwRet;} // get mapped file size DWORD UC_FILEMAPPING :: GetSize () {if (m_isFileLoaded == FALSE) return RET_FILENOTOPENNED; if (m_pusMapInfo) return m_pusMapInfo-> dwSizeLow - iNFO_LEN; else return -1;} // outputting content DWORD UC_FILEMAPPING :: FlushView () {// condition monitoring if (m_isFileLoaded) {if (m_lpCursor> m_lpAddress && m_hPhysicsFile) {SIZE_T nSize = (SIZE_T) ((LPBYTE) m_lpCursor - (LPBYTE) m_lpaddress; if (FlushViewoffile (m_lpaddress, nsize)) Return RET_OK; Else Return Ret_Fileerr;} Return Ret_ok;} else {return Ret_FileNotopenned;}} I hope you colleagues correct.

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

New Post(0)