📄 vmmemmappedfile.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description:
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: 1 $ : $Date: 9/12/97 3:30p $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include <assert.h>
#include <string.h>
#include "VMMemMappedFile.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMMemMappedFile
DESCRIPTION: ctor. Sets up all variables.
INPUT: pchName - The name of the MMF to attach to this class
bCreate - Create the MMF during construction
dwSizeLow - the size of the mmf to create
RETURNS: void
*/
VMMemMappedFile::VMMemMappedFile( char* pchName, bool bCreate, DWORD dwSizeLow )
{
assert ( NULL != pchName );
strcpy( m_achName, pchName );
char achTemp[ 32 ];
strcpy( achTemp, m_achName );
strcat( achTemp, "SemForWrite" );
strcpy( m_achSemNameWrite, achTemp );
strcpy( achTemp, m_achName );
strcat( achTemp, "SemForRead" );
strcpy( m_achSemNameRead, achTemp );
assert( 0 < dwSizeLow );
m_dwSizeHigh = 0;
m_dwSizeLow = dwSizeLow;
m_hFileMap = NULL;
m_hFileMapReader = NULL;
m_bICreatedMapping = false;
m_dwSysReturnCode = 0L;
m_pvView = NULL;
m_hWriteSemaphore = NULL;
m_hReadSemaphore = NULL;
m_iFlags = 0;
if ( true == bCreate )
{
Create();
}
else
{
Open();
}
}
/* end of function "VMMemMappedFile" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: ~VMMemMappedFile
DESCRIPTION: dtor. closes all object handles
INPUT: void
RETURNS: void
*/
VMMemMappedFile::~VMMemMappedFile()
{
if ( NULL != m_hFileMap )
{
::CloseHandle( m_hFileMap );
}
if ( NULL != m_hFileMapReader )
{
::CloseHandle( m_hFileMapReader );
}
if ( NULL != m_hReadSemaphore )
{
::CloseHandle( m_hReadSemaphore );
}
if ( NULL != m_hWriteSemaphore )
{
::CloseHandle( m_hWriteSemaphore );
}
}
/* end of function "~VMMemMappedFile" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetLastErrorText
DESCRIPTION: support function that allows the caller to get a text
message based on the last error encountered in the class
INPUT: pchOutBuffer - a buffer to put the message into
iBufferSize - the size of the output buffer
RETURNS:
*/
void VMMemMappedFile::GetLastErrorText( char* pchOutBuffer, int iBufferSize )
{
void* pvBuffer = NULL;
if ( 0 != m_dwSysReturnCode )
{
::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
m_dwSysReturnCode,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
(LPTSTR) &pvBuffer,
0,
NULL );
int iMsgLen;
if ( 0 != ( iMsgLen = strlen( (const char*)pvBuffer ) ) )
{
if ( iMsgLen < iBufferSize )
{
strcpy( pchOutBuffer, (char*)pvBuffer );
}
else
{
strncpy( pchOutBuffer, (char*)pvBuffer, iBufferSize );
}
}
::LocalFree( pvBuffer );
}
else
{
strcpy( pchOutBuffer, "No Error Text" );
}
}
/* end of function "GetLastErrorText" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Create
DESCRIPTION: creates the mmf and all of the access control objects (single
owner semaphores) for the MMF.
INPUT: void
RETURNS: true if no errors, false otherwise
*/
bool VMMemMappedFile::Create( void )
{
m_dwSysReturnCode = 0L;
SECURITY_ATTRIBUTES xsa;
SECURITY_DESCRIPTOR xsd;
::InitializeSecurityDescriptor( &xsd, SECURITY_DESCRIPTOR_REVISION );
::SetSecurityDescriptorDacl( &xsd,
TRUE,
NULL,
FALSE );
xsa.nLength = sizeof( SECURITY_ATTRIBUTES );
xsa.bInheritHandle = true;
xsa.lpSecurityDescriptor = &xsd;
m_hFileMap = ::CreateFileMapping( (HANDLE) 0xFFFFFFFF,
&xsa,
PAGE_READWRITE,
m_dwSizeHigh,
m_dwSizeLow,
m_achName );
if ( m_hFileMap != NULL )
{
if ( ERROR_ALREADY_EXISTS == ( m_dwSysReturnCode = ::GetLastError() ) )
{
// this is a non critical error
//
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
m_bICreatedMapping = false;
}
else
{
m_bICreatedMapping = true;
}
m_hReadSemaphore = ::CreateSemaphore ( &xsa,
(LONG) 1,
(LONG) 2,
m_achSemNameRead );
DWORD dwError = ::GetLastError();
if ( ERROR_ALREADY_EXISTS == dwError )
{
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
}
else
{
m_dwSysReturnCode = dwError;
}
m_hWriteSemaphore = ::CreateSemaphore ( &xsa,
(LONG) 1,
(LONG) 2,
m_achSemNameWrite );
dwError = ::GetLastError();
if ( ERROR_ALREADY_EXISTS == dwError )
{
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
}
else
{
m_dwSysReturnCode = dwError;
}
}
else
{
m_dwSysReturnCode = ::GetLastError();
m_bICreatedMapping = false;
}
if ( 0 == m_dwSysReturnCode )
{
return( true );
}
else
{
return( false );
}
}
/* end of function "Create" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Open
DESCRIPTION: Opens the file mapping belonging to this class
INPUT: void
RETURNS: true if the operation succeeded, false otherwise
*/
bool VMMemMappedFile::Open()
{
m_dwSysReturnCode = 0L;
m_hFileMapReader = ::OpenFileMapping( FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
m_achName );
if ( NULL == m_hFileMapReader )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -