2020-07-28 00:53:29 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "RcResource.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
std::optional<RcResource> RcResource::create(int resource_id, const std::wstring_view resource_class)
|
|
|
|
{
|
|
|
|
const HRSRC resHandle = FindResourceW(nullptr, MAKEINTRESOURCEW(resource_id), resource_class.data());
|
|
|
|
if (!resHandle)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
const HGLOBAL memHandle = LoadResource(nullptr, resHandle);
|
|
|
|
if (!memHandle)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
const size_t resSize = SizeofResource(nullptr, resHandle);
|
|
|
|
if (!resSize)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
auto res = static_cast<const std::byte*>(LockResource(memHandle));
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-07-28 19:26:30 +08:00
|
|
|
return RcResource{ res, resSize };
|
2020-07-28 00:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RcResource::saveAsFile(const std::filesystem::path destination)
|
|
|
|
{
|
|
|
|
std::fstream installerFile{ destination, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc };
|
|
|
|
if (!installerFile.is_open())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-28 19:26:30 +08:00
|
|
|
installerFile.write(reinterpret_cast<const char*>(_memory), _size);
|
2020-07-28 00:53:29 +08:00
|
|
|
return true;
|
|
|
|
}
|