[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5502
  • Last Modified:

Using a resource file contaiing text (TResourceStream - RT_RCDATA)

Hi,

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

TEST RCDATA TEST.TXT

TEST.TXT looks like this

122019331

(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);
  (etc)

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,

PAG
0
PAG_Promax
Asked:
PAG_Promax
  • 3
1 Solution
 
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

0
 
George TokasCommented:
The memory stream is added by mistake...
From my article on http://bcbjournal.com May 2007 issue...

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

George Tokas.
0
 
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!

PAG.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now