本文发表在 rolia.net 枫下论坛#pragma once
class SharedMem
{
private:
HANDLE memMappedFileHandle_;
char* dataPointer_;
std::string name_;
int size_;
public:
SharedMem(const char* name, bool createOrAttach, int size)
{
size_ = size;
name_ = name;
// TODO: parameter checking
// Create unique named mutex
//guard = new Mutex(false, name + "Mutex");
// Create or attach to shared memory segment
if(createOrAttach)
{
memMappedFileHandle_ = CreateFileMappingA(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
size,
name);
}
else
{
memMappedFileHandle_ = OpenFileMappingA(FILE_MAP_ALL_ACCESS, true, name);
}
if( memMappedFileHandle_ == INVALID_HANDLE_VALUE)
{
DWORD errorCode = GetLastError();
if(errorCode == ERROR_INVALID_HANDLE)
{
//"Shared memory segment already in use"
}
else
{
//"Unable to access shared memory segment. GetLastError = " + errorCode;
}
}
dataPointer_ = reinterpret_cast<char*>(MapViewOfFile(memMappedFileHandle_, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if(dataPointer_ == NULL)
{
DWORD errorCode = GetLastError();
CloseHandle(memMappedFileHandle_);
memMappedFileHandle_ = NULL;
//"Unable to map shared memory segment. GetLastError = " + i
}
}
~SharedMem()
{
// always clean up unmanaged resources
if(dataPointer_ != NULL)
{
UnmapViewOfFile(dataPointer_);
}
if(memMappedFileHandle_ != NULL)
{
CloseHandle(memMappedFileHandle_);
}
}
const char* Name()
{
return name_.c_str();
}
int Size()
{
return size_;
}
char* GetBuffer()
{
return dataPointer_;
}
void Lock()
{
//guard.WaitOne();
}
void Unlock()
{
//guard.ReleaseMutex();
}
void SaveToTemp()
{
std::string tempPath("c:\\temp\\");
std::string saveToPath = tempPath + name_;
std::ofstream tempFile(saveToPath.c_str(), std::ofstream::binary);
tempFile.write(dataPointer_, size_);
tempFile.close();
}
};更多精彩文章及讨论,请光临枫下论坛 rolia.net
class SharedMem
{
private:
HANDLE memMappedFileHandle_;
char* dataPointer_;
std::string name_;
int size_;
public:
SharedMem(const char* name, bool createOrAttach, int size)
{
size_ = size;
name_ = name;
// TODO: parameter checking
// Create unique named mutex
//guard = new Mutex(false, name + "Mutex");
// Create or attach to shared memory segment
if(createOrAttach)
{
memMappedFileHandle_ = CreateFileMappingA(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
size,
name);
}
else
{
memMappedFileHandle_ = OpenFileMappingA(FILE_MAP_ALL_ACCESS, true, name);
}
if( memMappedFileHandle_ == INVALID_HANDLE_VALUE)
{
DWORD errorCode = GetLastError();
if(errorCode == ERROR_INVALID_HANDLE)
{
//"Shared memory segment already in use"
}
else
{
//"Unable to access shared memory segment. GetLastError = " + errorCode;
}
}
dataPointer_ = reinterpret_cast<char*>(MapViewOfFile(memMappedFileHandle_, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if(dataPointer_ == NULL)
{
DWORD errorCode = GetLastError();
CloseHandle(memMappedFileHandle_);
memMappedFileHandle_ = NULL;
//"Unable to map shared memory segment. GetLastError = " + i
}
}
~SharedMem()
{
// always clean up unmanaged resources
if(dataPointer_ != NULL)
{
UnmapViewOfFile(dataPointer_);
}
if(memMappedFileHandle_ != NULL)
{
CloseHandle(memMappedFileHandle_);
}
}
const char* Name()
{
return name_.c_str();
}
int Size()
{
return size_;
}
char* GetBuffer()
{
return dataPointer_;
}
void Lock()
{
//guard.WaitOne();
}
void Unlock()
{
//guard.ReleaseMutex();
}
void SaveToTemp()
{
std::string tempPath("c:\\temp\\");
std::string saveToPath = tempPath + name_;
std::ofstream tempFile(saveToPath.c_str(), std::ofstream::binary);
tempFile.write(dataPointer_, size_);
tempFile.close();
}
};更多精彩文章及讨论,请光临枫下论坛 rolia.net