Solved

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
Unix / Linux grid computing 5 126
sum13 challenge 24 77
Annoying "thing" blocks my view 4 50
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now