rainmeter-studio/Plugins/PluginCoreTemp/SharedMem.cpp
2011-02-19 11:12:06 +00:00

66 lines
1.3 KiB
C++

#include <windows.h>
#include "SharedMem.h"
CSharedMemClient::CSharedMemClient(void)
{
}
CSharedMemClient::~CSharedMemClient(void)
{
}
bool CSharedMemClient::ReadSharedMem(PCORE_TEMP_SHARED_DATA i_SharedData)
{
bool bRet = false;
PCORE_TEMP_SHARED_DATA pSharedData;
HANDLE hdlMemory;
HANDLE hdlMutex;
hdlMutex = CreateMutex(NULL,FALSE,CORE_TEMP_MUTEX_OBJECT);
if (hdlMutex == NULL)
{
return false;
}
WaitForSingleObject(hdlMutex, INFINITE);
hdlMemory = OpenFileMapping(
FILE_MAP_READ, // Read only permission.
TRUE,
CORE_TEMP_MAPPING_OBJECT); // "CoreTempMappingObject"
if (hdlMemory == NULL)
{
ReleaseMutex(hdlMutex);
CloseHandle(hdlMutex);
return false;
}
pSharedData = (PCORE_TEMP_SHARED_DATA)MapViewOfFile(hdlMemory, FILE_MAP_READ, 0, 0, 0);
if (pSharedData == NULL)
{
CloseHandle(hdlMemory);
hdlMemory = NULL;
ReleaseMutex(hdlMutex);
CloseHandle(hdlMutex);
return false;
}
__try
{
memcpy_s(i_SharedData, sizeof(core_temp_shared_data), pSharedData, sizeof(core_temp_shared_data));
bRet = true;
}
__except(1)
{
bRet = false;
SetLastError(0x20000000); //Unknown error
}
UnmapViewOfFile(pSharedData);
CloseHandle(hdlMemory);
ReleaseMutex(hdlMutex);
CloseHandle(hdlMutex);
return bRet;
}