visual studio - How do I embed and use resource files in a C program using VS11? -


this first attempt @ using resource file. have seen lot of answers apply c# not c. suggestions?

assuming mean method used sysinternals , others carry drivers or needed dlls (or x64 version of program itself) in resource section of program (e.g. sysinternals' process explorer), using microsoft visual c can use code:

bool extractresto(hinstance instance, lpctstr binresname, lpctstr newpath, lpctstr restype) {     bool bresult = false;     hrsrc hrsrc;     if(hrsrc = findresource(hmodule(instance), binresname, restype))     {         hglobal hglob         if(hglobal hglob = loadresource(instance, hrsrc))         {             dword dwressize = sizeofresource(instance, hrsrc);             handle hfilewrite = createfile(newpath, generic_read | generic_write, file_share_read | file_share_write, null, create_always, file_attribute_archive, 0);             if(hfilewrite != invalid_handle_value)             __try             {                 dword dwsizewritten = 0;                 bresult = (writefile(hfilewrite, lockresource(hglob), dwressize, &dwsizewritten, null) && (dwsizewritten == dwressize));             }             __finally             {                 closehandle(hfilewrite);             }         }     }     return bresult; } 

this saves given resource (binresname) of resource type (restype) module (e.g. dll) instance file newpath. if c doesn't understand __try , __finally you'll have adjust code accordingly.

taken here (in sidt.rar) , adjusted c. code under liberal bsd license according website.

now if wanted pointer data (ppres) , size (pwdressize):

bool getresourcepointer(hinstance instance, lpctstr resname, lpctstr restype, lpvoid* ppres, dword* pdwressize) {     // check pointers want write     if(ppres && pdwressize)     {         hrsrc hrsrc;         // find resource resname of type restype in dll/exe described instance         if(hrsrc = findresource((hmodule)instance, resname, restype))         {             hglobal hglob;             // make sure it's in memory ...             if(hglob = loadresource(instance, hrsrc))             {                 // lock pointer                 *ppres = lockresource(hglob);                 // retrieve size of resource                 *pdwressize = sizeofresource(instance, hrsrc);                 // return true if both succeeded                 return (*ppres && *pdwressize);             }         }     }     // failure means don't use values in *ppres , *pdwressize     return false; } 

call this:

lpvoid presource; dword presourcesize; if(getresourcepointer(hinstance, _t("somename"), _t("sometype"), &presource, &presourcesize)) {     // use presource , presourcesize     // e.g. store string buffer or whatever want ... } 

note, works resources integer ids. can detect these using macro is_intresource.

the resource script, e.g. myresources.rc, trivial:

#include <winnt.rh> "somename" "sometype" "path\to\file\to\embed" 

rcdata instead of sometype reasonable choice

#include <winnt.rh> // rcdata known rc.exe "somename" rcdata "path\to\file\to\embed" 

... in case adjust above call be:

getresourcepointer(hinstance, _t("somename"), rt_rcdata, &presource, &presourcesize) 

complete example making use of getresourcepointer above. let's have pointer variable char* buf , know resource actual text, need make sure zero-terminated when used string, that's all.

resource script:

#include <winnt.rh>  "test" rcdata "test.txt" 

the code accessing it

char* buf = null; lpvoid presource; dword presourcesize; if(getresourcepointer(hinstance, _t("test"), rt_rcdata, &presource, &presourcesize)) {     if(buf = calloc(presourcesize+1, sizeof(char)))     {         memcpy(buf, presource, presourcesize);         // use buf         free(buf);     } } 

unless, of course meant link .res file works passing linker command line.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -