Click here to Skip to main content
15,885,182 members
Articles / Desktop Programming / MFC
Article

How to Extract Text from an HTML Resource

Rate me:
Please Sign up or sign in to vote.
4.69/5 (27 votes)
10 Jun 20052 min read 77.3K   1.6K   28   16
This article illustrates how to get the text out of an HTML resource.

Introduction

This article will make the beginner walk through the process of adding an HTML resource to their project and accessing that resource through code. What I present here is a simple function that will fill a CString with the contents of an HTML resource.

Background

A few days ago, I was coding up this application that generates an HTML report and wanted to add the template for the report to my code. I wanted to avoid hard-coding any HTML and I didn't want to refer an external file for the template. So I decided to solve this dilemma by adding the HTML template to my project as a resource. Well, as it turns out, I was able to load the resource and get a HANDLE to it, but couldn't figure out how to get the contents into a CString. My fundamental problem was that I did not realize the HANDLE was actually a bald pointer to the HTML I had loaded.

This seemingly simple task was racking my brain for nearly an hour before help arrived via the VC++ forum. I then decided to throw this stuff together in order to serve as a reference for anyone else who may run into this.

Adding an HTML Resource to your Project

Once you have created a project, you can insert an HTML resource by right-clicking the project item found in the resource tab and selecting "Add Resource" as illustrated below:

Add Resource

You will then be prompted to select the resource type. Select HTML and you may choose to create a New one or Import an existing file.

Select Resource Type

Once you have your HTML in your project, you're ready to access it through your program.

The GetHTML() Function

static bool GetHTML(const int& idrHTML, CString& rString)
{
   bool retVal = false;
   try
   {      
      HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(idrHTML), RT_HTML);
      if (hSrc != NULL)
      {
         HGLOBAL hHeader = LoadResource(NULL, hSrc);
         if (hHeader != NULL)
         {
            LPCTSTR lpcHtml = static_cast<LPCTSTR>(LockResource(hHeader));
            if (lpcHtml != NULL)
            {
               rString = CString(lpcHtml);
               retVal = true;
            }
            UnlockResource(hHeader);
         }
         FreeResource(hHeader);
      }
   }
   catch (CMemoryException* e)
   {
      SetLastError(ERROR_FUNCTION_FAILED);
      e->ReportError();
      e->Delete();
      retVal = false;
   }
   catch (CResourceException* e)
   {
      SetLastError(ERROR_FUNCTION_FAILED);
      e->ReportError();
      e->Delete();
      retVal = false;
   }
   catch (CException* e)
   {
      SetLastError(ERROR_FUNCTION_FAILED);
      e->ReportError();
      e->Delete();
      retVal = false;
   }
   catch (...)
   {
      SetLastError(ERROR_FUNCTION_FAILED);
      retVal = false;
   }
   return retVal;
}

Well, that's really all there is to it.

Conclusion

I understand that, all of this may seem second-nature to many seasoned developers, but sometimes the obvious is not-so-obvious. Hopefully this little snippet helps you out in your project.

History

  • 10.July.2005

    Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Aspen Insights
United States United States
Walter Storm is currently doing quantitative research and data science. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^], and an M.S. in Systems Engineering from SMU[^]. He has been professionally developing software in some form or another since January of 2001.

View Walter Storm's profile on LinkedIn.[^]

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 143041122-Sep-10 20:31
Member 143041122-Sep-10 20:31 
Generalworking under vs2008 with unicode enabled [modified] Pin
Rudi66620-Aug-09 22:54
Rudi66620-Aug-09 22:54 
QuestionVS2005 compatible code Pin
Douglas R. Keesler22-Dec-06 15:28
Douglas R. Keesler22-Dec-06 15:28 
AnswerRe: VS2005 compatible code Pin
Nitron23-Dec-06 4:10
Nitron23-Dec-06 4:10 
GeneralRe: VS2005 compatible code [modified] Pin
Tim Callaghan25-Jun-07 20:52
Tim Callaghan25-Jun-07 20:52 
I found that to get it working in Release under VS2005 you need to tell the CString its length during construction.
You can get the size of the resource (in bytes) by using the ::SizeofResource(...) function and then compute the resulting CString length depending on the format of the html resource (ANSI, Unicode etc.).

So, all you need to do (for the project supplied on this page) is change

if (lpcHtml != NULL)
{
rString = CString(lpcHtml);
retVal = true;
}

to

if (lpcHtml != NULL)
{
rString = CString(lpcHtml, ::SizeofResource(NULL, hSrc));
retVal = true;
}

and it should work (it did for me).

I'm not entirely sure why this is, but I noticed that in Release lpcHtml was pointing to the entire resource block (not just the html) and it was failing when trying to cast this to something suitable to pass to the CString constructor. If anybody knows more on this I'd be happy to learn something!

Cheers,

Spoida

PS: Thanks for the great article. Easy to follow and easy to use. Perfect Smile | :)

-- modified at 2:58 Tuesday 26th June, 2007
GeneralLoading resource with Unicode enabled Pin
bunton30-Jan-06 11:16
bunton30-Jan-06 11:16 
Generalre: using with Plain Text, etc.... Pin
Douglas R. Keesler13-Jun-05 13:24
Douglas R. Keesler13-Jun-05 13:24 
GeneralRe: re: using with Plain Text, etc.... Pin
Garth J Lancaster16-Aug-05 14:13
professionalGarth J Lancaster16-Aug-05 14:13 
GeneralRe: re: using with Plain Text, etc.... Pin
Douglas R. Keesler17-Aug-05 13:21
Douglas R. Keesler17-Aug-05 13:21 
GeneralRe: re: using with Plain Text, etc.... Pin
Garth J Lancaster17-Aug-05 13:25
professionalGarth J Lancaster17-Aug-05 13:25 
GeneralAwesome - 5 out of 5 Pin
Douglas R. Keesler12-Jun-05 16:46
Douglas R. Keesler12-Jun-05 16:46 
GeneralRe: Awesome - 5 out of 5 Pin
Nitron13-Jun-05 3:01
Nitron13-Jun-05 3:01 
GeneralRe: Awesome - 5 out of 5 Pin
Douglas R. Keesler13-Jun-05 13:14
Douglas R. Keesler13-Jun-05 13:14 
GeneralRe: Awesome - 5 out of 5 Pin
Avis p13-Jun-05 14:39
Avis p13-Jun-05 14:39 
GeneralRe: Awesome - 5 out of 5 Pin
Douglas R. Keesler13-Jun-05 15:04
Douglas R. Keesler13-Jun-05 15:04 
GeneralRe: Awesome - 5 out of 5 Pin
Avis p13-Jun-05 16:44
Avis p13-Jun-05 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.