Discussion:
text file rersource
(too old to reply)
Sudesh
2007-10-02 07:03:00 UTC
Permalink
hello
how to inlcude a plain text file as resource and then read it in program?
thanks,
sudesh
Remy Lebeau (TeamB)
2007-10-02 17:01:37 UTC
Permalink
Post by Sudesh
how to inlcude a plain text file as resource
You have to declare the filename in a .rc file that you then add to the
project. The .rc file defines an ID number for the resource so your code
can find it later.
Post by Sudesh
and then read it in program?
Use the Win32 API FindResource(), LoadResource(), and LockResource()
functions, or the VCL's TResuorceStream class.

--- myfile.rh ---

#ifndef MyFileH
#define MyFileH

#define ID_MYFILE 12345

#endif


--- myfile.rc ---

#include "myfile.rh"

ID_MYFILE RCDATA "myfile.txt"


--- mysrc.cpp ---

#include "myfile.rh"

{
HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(ID_MYFILE),
RT_RCDATA);
if( hSrc )
{
HGLOBAL hData = LoadResource(NULL, hSrc);
if( hData )
{
LPVOID pData = LockResource(hData);
DWORD dwSize = SizeofResource(NULL, hSrc);

// use pData as needed, up to dwSize bytes...
}
}
}

or:

{
TResourceStream *strm = new TResourceStream(0, ID_MYFILE,
RT_RCDATA);
// use strm as needed...
delete strm;
}


Gambit
Sudesh
2007-10-03 03:04:42 UTC
Permalink
Hello Remy

Thanks very much. It works fine.

Why did the following gave me an access violation?

TResourceStream *RS = new TResourceStream((int)HInstance,
MAKEINTRESOURCE("ID_MYFILE"), RT_RCDATA );

Thanks
Sudesh
Remy Lebeau (TeamB)
2007-10-03 16:37:38 UTC
Permalink
Post by Sudesh
Why did the following gave me an access violation?
TResourceStream *RS = new TResourceStream((int)HInstance,
MAKEINTRESOURCE("ID_MYFILE"), RT_RCDATA );
ID_MYFILE is a numeric value, not a string. You are causing
MAKEINTRESOURCE() pass the wrong value. Look at the earlier example I gave
you. I was not using quotes when calling MAKEINTRESOURCE(). I wasn't even
using MAKEINTRESOURCE() with TResourceStream, since it has an overloaded
constructors that takes an integer ID as input.


Gambit
Sudesh
2007-10-03 19:04:18 UTC
Permalink
Thanks Remy for pointing out the mistake.
Sudesh

Loading...