Solved

How to package an external file and use this external file in my application

Posted on 2004-08-09
5
184 Views
Last Modified: 2013-11-20
Hi,

I am writing a simple dialog application using MFC on VS.NET 2003. I want to use some external data files in my application. One is XML file, the other one is a text file. In the resource editor window, I add two cusomized resource types: XML and TXT. Then I add XML file and text file as resources. Each file has an associtated ID.

So My question is how to use these two files in my program? For example, I can use LoadImage or LoadIcon to use image and icon resource. My intention is to load these two files as string, so I can parse XML file or read text file.

Thanks for your help.

-David
0
Comment
Question by:htang_us
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11762793
You have to use FindResource() to obtain a handle to resource, and then LoadResource() to obtain a pointer to data.
0
 
LVL 2

Author Comment

by:htang_us
ID: 11765043
Hi, Jaime_olivares,

Thanks for your opinions. Could you give me a simple code snippet to show how to get the file content.

For example, I have following resource

XML
 |---IDR_INDEX

Thanks

-David
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 50 total points
ID: 11765193
First, obtain the resource size
int size = GetResourceSize("RESOURCE_NAME", "XML");

reserve memory in a buffer, by example:
BYTE *buffer = new BYTE[size];

Finally, get resource data:
int size = ExtractResource("RESOURCE_NAME", "XML", size, buffer);

Don't forget to delete the buffer, when you don't need it more.
A tip: Use ALWAYS uppercase in resource name and type (This is not documented in MSDN, as far I know)

-----------------------------------------------------------------------------------------------

int CYourApp::GetResourceSize(LPCTSTR name, LPCTSTR type)
{
      HRSRC hRes = ::FindResource(m_hInstance, name, type);

      return hRes ? ::SizeofResource(m_hInstance, hRes) : -1;
}

bool CYourApp::ExtractResource(LPCTSTR name, LPCTSTR type, int size, BYTE *buffer)
{
      HRSRC hRes = ::FindResource(m_hInstance, name, type);
      if (!hRes)
            return false;

      HGLOBAL hResLoad = ::LoadResource(m_hInstance, hRes);
      if (!hResLoad)
            return false;

      BYTE *data = (BYTE *)::LockResource(hResLoad);
      if (!data)
            return false;
      
      memcpy(buffer, data, size);

      return true;
}
0
 
LVL 2

Author Comment

by:htang_us
ID: 11773077
Hi, Jaime_olivares,

Thanks a lot for excellent answer.

-David
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11773156
Thanks for the points. Notice that when working with text objects like XML, you have to put a null character at the end of the text (and reserve 1 extra byte for it).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
centeredAverage challenge 8 164
fizzArray2 challenge 1 79
twoTwo  challenge 35 103
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question