?
Solved

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

Posted on 2004-08-09
5
Medium Priority
?
190 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Integration Management Part 2
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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