mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-15 03:59:15 +08:00
687b281b47
* Formatting, coding style, variable & function name * Progress dialog: label position and background
46 lines
1.1 KiB
C++
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;
|
|
}
|