Filemapping User Class Source Code (Original, Welcome Criticism)

zhaozj2021-02-08  395

Welcome to

// 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 # prgma overce # endif //_MSC_VER> 1000

//// module: memory mapped files the user class // Author: Zhang Min // Create: 2002.01.16 // Description: // use memory-mapped file sharing memory between processes, // the current 32-bit applications Addressing range 4GB, no high level // # include "uc_log.h"

// Custom return value #define ret_fileopenned ret_user 1 // file already opened #define return 未 et et 2 2 2 2 未 # # 2 3 2 2 2 2 2 # # 2 2 Spilling #define ret_fileprocessing ret_user 5 // File is operating #define return ket_offsetoverflow return RET_USER 6 // Offset overflow

// Reserved length information structure for shared memory predefined typef struct tagmapinfo {

DWORD dwsizehigh; // high file size DWORD dwsizerow; // low file size

DWORD DWSIUSEDHIGH; / / High position uses size DWORD DWSIZEUSEDLOW; // low use size

Char szmappingname [_max_path]; // Mapping Name

Tagmapinfo () {dwsizehigh = dwsizelow = dwsizeusedhigh = dwsizeusedlow = 0; MEMSET (SzmappingName, 0, _MAX_PATH);

} US_MAPINFO, * PUS_MAPINFO;

// paged threshold #define HIGH_MAX 0xFFFFFFFE // high maximum #define LOW_MAX 0xFFFFFFFF - sizeof (US_MAPINFO) // low maximum #define INFO_LEN sizeof (US_MAPINFO) // header length #define NOPHYSICALFILE 0xFFFFFFFF // no physical file

class UC_FILEMAPPING: public UC_LOG {public: // // Author: Zhang Min // Create: 2002.01.16 // Function: // get the file size using the parameters: // Returns: // files size // DWORD GetSizeUsed () ; // // author: Zhang Min // create: 2002.01.16 // function: // get file size parameters: // returns: // file size // DWORD GetSize (); // // author: Zhang Min // Create: 2002.01.16 // Function: Write map file // Parameter: // [in] LPBUF buffer // [in] NSIZE buffer size // [in] dwoffsetlow offset address status // [in] ] Isappend Add write // Return: // RET_BADARG parameter 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 buffer area excess // RET_BUFFEROVERFLOW buffer overflow //// RET_FILENOTOpenned file is not open // RET_FILEPROCESSING file is operationing // RET_FILEERR file operation failed / / RET_OK operation was successful // DWORD Read (LPVOID lpszBuf, UINT & nSize, DWORD dwOffsetLow = 0); // // Author: Zhang Min // Create: 2002.01.16 // Function: // close the file mapping parameters: // [ in] wantDump need to export // return: // RET_FILENOTOPENNED file does not open // RET_FILEERR file operation failed // RET_OK operation was successful // DWORD Close (BOOL wantDump = TRUE); // // author: Zhang Min // create: 2002.01 .16 // Function: Open mapping file // parameter: // [in] lpszmappingName mapping memory name // Return: // RET_BADARG parameter illegal // RET_FILEOPENED file has been opened // RET_FILEERR file operation failed // RET_OK operation Success // DWORD OPEN (LPCTSTR LPSZMAppingName);

// // Author: Zhang Min // Create: 2002.01.16 // Function: // Open the map file parameters: // [in] lpszFilePath physical file path // [in] lpszMappingName mapped memory named // [in] dwSizeLow low space // return: // RET_BADARG parameters illegal // RET_FILEOPENNED file is already open // RET_FILEERR file operation failed // RET_OK operation was successful // DWORD open (LPCTSTR lpszFilePath, LPCTSTR lpszMappingName, DWORD dwSizeLow); // // author: // Create Zhang Min: 2002.01.16 // function: constructors // UC_FILEMAPPING (); // // OF: Create Single Min @: @ 2002.01.16 function: destructor // virtual ~ UC_FILEMAPPING () protected; // Physical File Handle Handle M_HMAppingFile; // Mapping File Handle Handle M_HFileop; // File Operation Mutual Quantity LPVOID M_LPCURSOR; // Mapping Cursor Address Pointer LPVOID M_LPADDRESS; / / Map File Address Pointer

PUS_MAPINFO M_PUSMAPINFO; / / Mapping Internal Sort Bool M_isFileLoaded; // Working Status Sign Bool M_IsMyHandle; / / Is it a file mapping you created?

DWORD m_dwSysAlloc; // allocate memory system the smallest unit private: DWORD FlushView (); DWORD GetErrorMessage (); // // Author: Zhang Min // Create: 2002.01.16 // Function: Formatted sizes and offsets / / 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: Mobile mapping file pointer // parameter: // [in] dwoffsetlow low offset // Return: // RET_BADARG parameter illegal // RET_FILENOTOPENNED file UndThat // RET_FILEPROCESSING file is operationing / / RET_OK operation success //};

#ENDIF /! Defined (AFX_UC_FILEMAPPING_H__72FE31B2_7B02_442F_A754_66427E1C5946__included_)

// uc_filemapping.cpp: importation 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);

// Initialization handle m_hphysicsfile = null; m_hmappingfile = null; m_hfileop = null; // initialization flag m_isfileloaded = false; m_ismyhandle = false;

// Initialization file member variable m_pusmapinfo = null; m_lpaddress = null; m_lpcursor = NULL;

// Get the system's 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 the mapping memory head information pointer IF (m_pusmapinfo) delete m_pusmapinfo;

// Close the handle ReleaseMutex (M_HFileop);

// Only the creator has the right to close the handle if (m_ismyhandle) {closeHandle (m_hphysicsfile); closehandle (m_hmappingfile); closehandle (m_hfileop);

ReleaseMutex (m_hmutex);

// New Document DWORD UC_FILEMAPPING :: Open (LPCTSTR LPSZFILEPATH, LPCTSTSTR LPSZMAPINGNAME, DWORD DWSILOW) {// Status Monitor 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 corresponding physical file if (lpszfilepath! = Null) {// New file m_hphysicsfile = createfile (lpszFilepath, generic_read | 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 the mapping memory 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, lpszMappingName, strlen (lpszMappingName)); memcpy (m_lpAddress, m_pusMapInfo, INFO_LEN);

/ / Save the start address and initialization cursor information of the application // This actually actually address this is included in the blank portion m_lpaddress = m_lpcursor = (lpbyte) m_lpaddress (lpbyte) M_LPADDRESS INFO_LEN);

// Set the open flag information m_isFileLoaded = true; m_ismyhandle = true;

}}}

}

ReleaseMutex (M_HFileop); if (dwret! = RET_OK) CloseHandle (M_HFileop);

Return dwret;}

/ / Open an existing named memory mapping file DWORD uc_filemapping :: Open (LPCTSTR LPSZMAPPINGNAME) {// Status Monitor 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_FILENOTOPENED;} WaitForsingleObject (M_HFileop, Infinite);

// Open the 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 the mapping interior head information IF (m_pusmapinfo == null) m_pusmapinfo = new US_MapInfo;

Memcpy (m_pusmapinfo, m_lpaddress, info_len);

// Save address and cursor // The principle of creating M_LPAddress = m_lpcursor = (lpvoid) ((lpbyte) m_lpaddress info_len);

// Set the open flag information m_isFileLoaded = true; m_ismyhandle = true;

}

ReleaseMutex (M_HFileop); return dwret;}

// Close Document DWORD UC_FILEMAPPING: CLOSE (BOOL WANTDUMP) {// Status Monitor 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 the creator has the right to close the handle 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 monitoring IF (lpbuf == null || nsize == 0) Return Ret_badarg;

DWORD dwret = RET_OK; WaitForsingleObject (m_hfileop, infinite); // Calculate whether content overflow uint nsizeused = (uint) (LPBYTE (M_LpCursor);

IF (nsize

// Write information nsize = nsizeused; memcpy (lpbuf, (lpbyte) m_lpaddress dwoffsetlow), nsize;

}

ReleaseMutex (M_HFileop); return dwret;}

// Write mapping file DWORD uc_filemapping :: Write (LPVOID LPBUF, UINT & NSIZE, DWORD DWOFFSETLOW, BOOL ISAPPEND) {// State Monitor IF (m_isfileloaded == false) return retun ret_filenotopenned;

// Parameter monitoring IF (lpbuf == null || nsize == 0) Return Ret_badarg;

DWORD DWRET = RET_OK; WaitforsingleObject (M_HFileop, Infinite);

/ / Calculate whether the content overflow uint nleftsize = (uint) m_pusmapinfo-> dwsizelow - (LPBYTE (M_LPCURSOR) - LPBYTE (M_LPAddress)) - Info_len;

IF (NLEFTSIZE

/ / According to the mode mobile cursor IF (isappend == false) {m_lpcursor = (lpvoid) (lpbyte) m_lpaddress dwoffsetlow);

// Write information memcpy (m_lpcursor, lpbuf, nsize);

// Mobile Cursor IF (Isappend) m_lpcursor = (lpbyte) m_lpcursor nsize);

// Record file usage m_pusmapinfo-> dwsizeusedlow = (dword) (lpbyte) m_lpcursor - (lpbyte) m_lpaddress;}

ReleaseMutex (M_HFileop); return dwret;}

// get the 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. // ... // mssagebox (null, (lpctstr) lpmsgbuf, "error", MB_OK | MB_ICONIORMATION; trace ("0x% 08x:% s / n ", dwret, lpmsgbuf); // free the buffer. Localfree (lpmsgbuf); return dwret;}

// Get the mapping file size DWORD UC_FILEMAPPING:: getSize () {if (m_isfileloaded == false) return retun retun ret_filenotopenned;

IF (m_pusmapinfo) Return M_PusmapInfo-> DWSiZELOW - INFO_LEN; Else Return -1;}

// Get the mapping file Using the size DWORD UC_FILEMAPPING:: getSizeused () {if (m_isfileloaded == false) return retun ret_filenotopenned;

IF (m_pusmapinfo) Return M_PusmapInfo-> DWSIZEUSEDLOW; Else Return -1;}

/ / Output DWORD UC_FILEMAPPING :: FlushView () {// Status Monitor IF (m_isfileloaded) {

IF (m_lpcursor> m_lpaddress && m_hphysicsfile) {size_t nsize = ((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;}}

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

New Post(0)