Solved

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

Posted on 2004-08-09
5
186 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 52
Get filename and folder into excel 7 84
if loop in java 3 163
Excel file not created as expected 7 93
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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