?
Solved

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

Posted on 2004-08-09
5
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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