PowerToys/installer/PowerToysBootstrapper/bootstrapper/RcResource.cpp
Enrico Giordani 687b281b47
[bootstrapper] progress bar UI polishing (#9644)
* Formatting, coding style, variable & function name

* Progress dialog: label position and background
2021-02-11 17:18:19 +01:00

46 lines
1.1 KiB
C++

#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;
}
return RcResource{ res, resSize };
}
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;
}
installerFile.write(reinterpret_cast<const char*>(_memory), _size);
return true;
}