rainmeter-studio/Plugins/PluginCoreTemp/SharedMem.cpp

66 lines
1.3 KiB
C++
Raw Normal View History

2011-01-26 13:57:57 +00:00
#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;
2013-05-31 14:28:39 +00:00
hdlMutex = CreateMutex(nullptr,FALSE,CORE_TEMP_MUTEX_OBJECT);
if (hdlMutex == nullptr)
2011-03-29 19:21:57 +00:00
{
2011-01-26 13:57:57 +00:00
return false;
2011-03-29 19:21:57 +00:00
}
WaitForSingleObject(hdlMutex, INFINITE);
2011-01-26 13:57:57 +00:00
hdlMemory = OpenFileMapping(
2011-03-29 19:21:57 +00:00
FILE_MAP_READ, // Read only permission.
TRUE,
2011-01-26 13:57:57 +00:00
CORE_TEMP_MAPPING_OBJECT); // "CoreTempMappingObject"
2011-03-29 19:21:57 +00:00
2013-05-31 14:28:39 +00:00
if (hdlMemory == nullptr)
2011-03-29 19:21:57 +00:00
{
ReleaseMutex(hdlMutex);
2011-02-19 11:12:06 +00:00
CloseHandle(hdlMutex);
2011-01-26 13:57:57 +00:00
return false;
2011-03-29 19:21:57 +00:00
}
2011-01-26 13:57:57 +00:00
pSharedData = (PCORE_TEMP_SHARED_DATA)MapViewOfFile(hdlMemory, FILE_MAP_READ, 0, 0, 0);
2013-05-31 14:28:39 +00:00
if (pSharedData == nullptr)
2011-01-26 13:57:57 +00:00
{
CloseHandle(hdlMemory);
2013-05-31 14:28:39 +00:00
hdlMemory = nullptr;
2011-03-29 19:21:57 +00:00
ReleaseMutex(hdlMutex);
2011-02-19 11:12:06 +00:00
CloseHandle(hdlMutex);
2011-01-26 13:57:57 +00:00
return false;
2011-03-29 19:21:57 +00:00
}
2011-01-26 13:57:57 +00:00
__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);
2011-03-29 19:21:57 +00:00
ReleaseMutex(hdlMutex);
2011-02-19 11:12:06 +00:00
CloseHandle(hdlMutex);
2011-01-26 13:57:57 +00:00
return bRet;
}