1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <Windows.h>
#ifndef _waxie_binary_resource_h_
#define _waxie_binary_resource_h_
#include <string>
class BinRes
{
public:
BinRes();
virtual ~BinRes();
public:
static void ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName);
private:
static std::string getAppLocation();
};
#endif
#include <string>
#include <fstream>
#include <iostream>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
BinRes::BinRes()
{
}
BinRes::~BinRes()
{
}
void BinRes::ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName )
{
HGLOBAL hResourceLoaded; // handle to loaded resource
HRSRC hRes; // handle/ptr. to res. info.
char *lpResLock; // pointer to resource data
DWORD dwSizeRes;
std::string strOutputLocation;
std::string strAppLocation;
// lets get the app location
strAppLocation = getAppLocation();
strOutputLocation = strAppLocation += "\\";
strOutputLocation += strOutputName;
// find location of the resource and get handle to it
hRes = FindResource( NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str() );
// loads the specified resource into global memory.
hResourceLoaded = LoadResource( NULL, hRes );
// get a pointer to the loaded resource!
lpResLock = (char*)LockResource( hResourceLoaded );
// determine the size of the resource, so we know how much to write out to file!
dwSizeRes = SizeofResource( NULL, hRes );
std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.close();
}
// retrieves the full path and file name for our executable file
std::string BinRes::getAppLocation()
{
TCHAR szPathName[128];
std::string strPath;
GetModuleFileName(NULL, szPathName, 128);
strPath = szPathName;
int slashPos = strPath.rfind('\\');
if(slashPos == strPath.npos)
throw "Unable to get exe location";
strPath = strPath.substr(0, slashPos);
return strPath;
}
|