106 lines
1.6 KiB
C++
106 lines
1.6 KiB
C++
#include "Resource.h"
|
|
|
|
#include <streambuf>
|
|
#include <istream>
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
static HMODULE GetCurrentModule()
|
|
{
|
|
HMODULE hModule = NULL;
|
|
GetModuleHandleEx(
|
|
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
|
(LPCTSTR)GetCurrentModule,
|
|
&hModule
|
|
);
|
|
|
|
return hModule;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
class ResBuf : public std::streambuf
|
|
{
|
|
public:
|
|
ResBuf(int id, int type)
|
|
{
|
|
HMODULE hMod = GetCurrentModule();
|
|
|
|
HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(id), MAKEINTRESOURCE(type));
|
|
if (!hRes)
|
|
{
|
|
throw std::exception("Could not find resource");
|
|
}
|
|
|
|
HGLOBAL hResLoad = LoadResource(hMod, hRes);
|
|
if (!hResLoad)
|
|
{
|
|
throw std::exception("Could not load resource");
|
|
}
|
|
|
|
LPVOID lpResLock = LockResource(hResLoad);
|
|
if (!lpResLock)
|
|
{
|
|
throw std::exception("Could not lock resource");
|
|
}
|
|
|
|
DWORD dwResSize = SizeofResource(hMod, hRes);
|
|
if (dwResSize == 0)
|
|
{
|
|
throw std::exception("Could not aquire resource size");
|
|
}
|
|
|
|
_hRes = hRes;
|
|
_buff = (char*)lpResLock;
|
|
_size = dwResSize;
|
|
|
|
setg(_buff, _buff, _buff + _size);
|
|
}
|
|
|
|
virtual ~ResBuf()
|
|
{
|
|
FreeResource(_hRes);
|
|
}
|
|
|
|
virtual int underflow()
|
|
{
|
|
return EOF;
|
|
}
|
|
|
|
char* buff()
|
|
{
|
|
return _buff;
|
|
}
|
|
|
|
size_t size()
|
|
{
|
|
return _size;
|
|
}
|
|
|
|
private:
|
|
HRSRC _hRes;
|
|
char* _buff;
|
|
size_t _size;
|
|
};
|
|
}
|
|
|
|
|
|
Resource::Resource(int id, int type) : std::istream(new ResBuf(id, type))
|
|
{
|
|
}
|
|
|
|
Resource::~Resource()
|
|
{
|
|
delete rdbuf();
|
|
}
|
|
|
|
const char* Resource::data()
|
|
{
|
|
return (const char*)(((ResBuf*)rdbuf())->buff());
|
|
}
|
|
|
|
size_t Resource::size()
|
|
{
|
|
return ((ResBuf*)rdbuf())->size();
|
|
} |