Using a resource file contaiing text (TResourceStream - RT_RCDATA)


I have a resource file called TEST.RC.. This file contains the following like:


TEST.TXT looks like this


(Single line of numbers).

When I build (BRCC32.EXE) the RC file, I get a RES file.  If I open this in a resource explorer, I can see I have a RT_RCDATA type resource called TEST and can see the text in it.

I want to now be able to use that in my C++ project.  I've linked the RES file in using #pragma resource.  I have the following code:

  AnsiString sBuffer;

  TResourceStream* resFile = new TResourceStream((int)HInstance, AnsiString("TEST"), "RT_RCDATA");
  resFile->Read(&sBuffer,  resFile->Size);

When it tries to create the resourcestream, an exception is raised.  I've verified the resource file has been compiled into the EXE.

I am using BCB V6.

Does anyone have any suggestions at all as to why I would be getting this error?  I'm sure its a conceptual error.  I have this code working fine within Delphi 7.

Many thanks,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

George TokasCommented:
Try the following:

AnsiString      Details;
        TMemoryStream   *Stream;
        bool    NextLine;
        HRSRC   ResHandle;
        HGLOBAL MemHandle;
        void *ResPtr;
        int   ResSize;
        char  *lpName;
        ResHandle = FindResource(HInstance, "TEST", "RC_DATA");
        if(ResHandle == NULL){return false;}//not found
        MemHandle = LoadResource(HInstance, ResHandle);
        if(MemHandle == NULL){return false;}//error
        ResPtr = LockResource(MemHandle);
        if(ResPtr == NULL){return false;}
        ResSize = SizeofResource(HInstance, ResHandle);
        char *pszBuffer = new char[ResSize+1];
        memcpy(pszBuffer, ResPtr, ResSize);
        Details = pszBuffer;//loaded to an ansistring
        delete []pszBuffer;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
George TokasCommented:
The memory stream is added by mistake...
From my article on May 2007 issue...

George Tokas.
George TokasCommented:
Inside your RC file you can use also:
In this case instead of:
>>ResHandle = FindResource(HInstance, "TEST", "RC_DATA");
ResHandle = FindResource(HInstance, "TEST", "TXT");

George Tokas.
PAG_PromaxAuthor Commented:
Thank you very much for this fantastic answer.  It worked brilliantly.

Its funny, I spent ages last night looking through Google, and found nothing but examples of how to load cursors and wave files.

Thanks again!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.