📄 sharedmemory.h
字号:
// SharedMemory.h: interface for the CSharedMemory class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SHAREDMEMORY_H__86467BA6_5AFA_11D3_863D_00A0244A9CA7__INCLUDED_)
#define AFX_SHAREDMEMORY_H__86467BA6_5AFA_11D3_863D_00A0244A9CA7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <process.h>
class CSharedMemory
{
private:
class CWriteQueue
{
// This class is the queue, it contains a pointer to
// a data block and a pointer to the next queue item.
friend class CSharedMemory;
private:
CWriteQueue(int nDataSize)
{
pData = new BYTE[nDataSize];
pNext = NULL;
};
~CWriteQueue()
{
delete [] pData;
};
void *pData;
CWriteQueue *pNext;
};
public:
enum
{
// Return values of the class-functions.
MEM_ERROR_UNKNOWN = -1,
MEM_SUCCESS = 0,
MEM_ERROR_CLOSED = 1,
MEM_ERROR_TIMEOUT = 2,
MEM_ERROR_OTHERPARTY = 3,
MEM_ERROR_DATASIZE = 4
};
CSharedMemory()
{
m_nOtherInstanceID = 0;
m_nInstanceID = 0;
// Create an event that indicates wether the connection
// is open or not.
m_hClosed = CreateEvent(NULL, TRUE, TRUE, NULL);
m_hDataWrit[0] = NULL;
m_hDataWrit[1] = NULL;
m_hDataRead[0] = NULL;
m_hDataRead[1] = NULL;
m_hDataInQueue = NULL;
m_hQueueMutex = NULL;
};
virtual ~CSharedMemory()
{
Close();
CloseHandle(m_hClosed);
};
bool Open(char* sName, int nDataSize, int nTimeOut = INFINITE)
{
m_pFirst = NULL;
// The connection must be closed before it can be opened.
if (WaitForSingleObject(m_hClosed, 0) == WAIT_OBJECT_0)
{
// The name may not exceed MAX_PATH, we substract 10 because we
// add some strings to the name in some code.
if (strlen(sName) != 0 && strlen(sName) < MAX_PATH - 10)
{
// The datasize must be larger than 0.
if (nDataSize > 0)
{
// The following mutexes can indicate 4 things:
// - No instance of this shared memory class was created.
// - The first instance of this class was created.
// - The second instance of this shared memory class was created.
// - Both instances were created.
char sMutex0 [MAX_PATH];
char sMutex1 [MAX_PATH];
strcpy(sMutex0 , sName);
strcpy(sMutex1 , sName);
strcat(sMutex0 , "Mutex0");
strcat(sMutex1 , "Mutex1");
m_hSharedMemoryMutex[0] = CreateMutex(NULL, FALSE, sMutex0);
m_hSharedMemoryMutex[1] = CreateMutex(NULL, FALSE, sMutex1);
if (m_hSharedMemoryMutex[0] && m_hSharedMemoryMutex[1])
{
// Only two instances of this class (with this name) may reside on
// one system. These will be referred to as 'm_nInstanceID and m_nOtherInstanceID'
HANDLE hWait[2] = {m_hSharedMemoryMutex[0], m_hSharedMemoryMutex[1]};
DWORD dwResult = WaitForMultipleObjects(2, hWait, FALSE, 0);
if (dwResult == WAIT_OBJECT_0 || dwResult == (WAIT_OBJECT_0 + 1))
{
if ((m_nInstanceID = dwResult - WAIT_OBJECT_0) == 0)
m_nOtherInstanceID = 1;
else
m_nOtherInstanceID = 0;
char sName0 [MAX_PATH];
char sName1 [MAX_PATH];
strcpy(sName0 , sName);
strcpy(sName1 , sName);
strcat(sName0 , "0");
strcat(sName1 , "1");
// We will use two shared memory pools to provide duplex
// communication.
if ((m_hSharedMemory[0] = CreateFileMapping( (HANDLE)0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof(int) + nDataSize,
sName0)) != NULL
&&
(m_hSharedMemory[1] = CreateFileMapping( (HANDLE)0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
sizeof(int) + nDataSize,
sName1)) != NULL)
{
bool bFileMappingAlreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
// Now map a pointer to the size tag in the shared memory.
m_pSize = (int*)MapViewOfFile( m_hSharedMemory[0],
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof(int));
if (m_pSize)
{
bool bSharedMemorySizeOk = false;
if (bFileMappingAlreadyExists)
{
// We will check if the size of the memory block is of the
// same size as the block that was already allocated by another
// instance of the shared memory class.
// The size of the memory block is saved in the first integer
// at the specified shared memory address.
if (*m_pSize == nDataSize)
bSharedMemorySizeOk = true;
}
else
{
// The memory was not allocated by another instance so we
// have the honors to allocate it. This means also that we should
// set the size of the memory that we have allocated in the first
// integer of the shared memory space.
*m_pSize = nDataSize;
bSharedMemorySizeOk = true;
}
if (bSharedMemorySizeOk)
{
m_pSharedMemory[0] = (BYTE*)MapViewOfFile( m_hSharedMemory[0],
FILE_MAP_ALL_ACCESS,
0,
0,
nDataSize);
m_pSharedMemory[1] = (BYTE*)MapViewOfFile( m_hSharedMemory[1],
FILE_MAP_ALL_ACCESS,
0,
0,
nDataSize);
if (m_pSharedMemory[0] && m_pSharedMemory[1])
{
// Move the pointer a little further so that it does not point to
// the size tag, but to the address of the data that we want to share.
m_pSharedMemory[0] += sizeof(int);
m_pSharedMemory[1] += sizeof(int);
// The following events make sure that data can only
// be read when data was written and vise versa.
char sDataWrit0 [MAX_PATH];
char sDataWrit1 [MAX_PATH];
char sDataRead0 [MAX_PATH];
char sDataRead1 [MAX_PATH];
strcpy(sDataWrit0 , sName);
strcpy(sDataWrit1 , sName);
strcpy(sDataRead0 , sName);
strcpy(sDataRead1 , sName);
strcat(sDataWrit0 , "DataWrit0");
strcat(sDataWrit1 , "DataWrit1");
strcat(sDataRead0 , "DataRead0");
strcat(sDataRead1 , "DataRead1");
m_hDataWrit[0] = CreateEvent(NULL, FALSE, FALSE, sDataWrit0);
m_hDataWrit[1] = CreateEvent(NULL, FALSE, FALSE, sDataWrit1);
m_hDataRead[0] = CreateEvent(NULL, FALSE, TRUE, sDataRead0);
m_hDataRead[1] = CreateEvent(NULL, FALSE, TRUE, sDataRead1);
if (m_hDataWrit[0] && m_hDataWrit[1] && m_hDataRead[0] && m_hDataRead[1])
{
m_hSecondInstanceAvailable = CreateEvent(NULL, FALSE, FALSE, sName);
if (m_hSecondInstanceAvailable)
{
if (m_nInstanceID == 0)
{
// We are the first instance, wait for the second instance
// to come this far, then we can assume that the connection
// is fully open.
if (WaitForSingleObject(m_hSecondInstanceAvailable, nTimeOut) == WAIT_OBJECT_0)
{
CloseHandle(m_hSecondInstanceAvailable);
ResetEvent(m_hClosed);
m_hQueueMutex = CreateMutex(NULL, FALSE, NULL);
m_hDataInQueue = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hQueueThread = (HANDLE)_beginthread(QueueThread, 0, this);
return true;
}
}
else if (m_nInstanceID == 1)
{
// We are the second instance, signal the other instance that
// we have come this far.
// Immediately wait 0 seconds for the event, if it is still signaled
// we know that the other instance was not waiting, the connection
// has failed.
SetEvent(m_hSecondInstanceAvailable);
if (WaitForSingleObject(m_hSecondInstanceAvailable, 0) == WAIT_TIMEOUT)
{
CloseHandle(m_hSecondInstanceAvailable);
ResetEvent(m_hClosed);
m_hQueueMutex = CreateMutex(NULL, FALSE, NULL);
m_hDataInQueue = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hQueueThread = (HANDLE)_beginthread(QueueThread, 0, this);
return true;
}
}
CloseHandle(m_hSecondInstanceAvailable);
}
else
{
// We could not create the required event.
}
}
else
{
// We could not create any event handles.
}
UnmapViewOfFile(m_pSharedMemory[0]);
UnmapViewOfFile(m_pSharedMemory[1]);
}
else
{
// We could not get a pointer to the actual data.
}
}
else
{
// The datasize of the already allocated memory, and the size of this
// instance do not match.
}
UnmapViewOfFile(m_pSize);
}
else
{
// We could not map to the integer that contains the size of the memory block.
}
CloseHandle(m_hSharedMemory[0]);
CloseHandle(m_hSharedMemory[1]);
}
else
{
// The memory handles could not be created.
}
}
else
{
// There was no mutex available, this can mean that there are
// already two instances of this object with the same name
// in use on this system.
}
CloseHandle(m_hSharedMemoryMutex[0]);
CloseHandle(m_hSharedMemoryMutex[1]);
}
else
{
// The mutexes could not be created.
}
}
else
{
// The datasize is not > 0.
}
}
else
{
// The name of the shared memory is not valid, or the datasize is not larger than 0.
}
}
else
{
// This instance is already open.
}
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -