Click here to Skip to main content
Click here to Skip to main content

How to Extract Text from an HTML Resource

, 10 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Nitron
Software Developer (Senior) Barbnet Investment Co.
United States United States
Walter Storm is currently a principal software engineer doing quantitative research for a private hedge fund. 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 PinmemberMember 143041122-Sep-10 20:31 
Generalworking under vs2008 with unicode enabled [modified] PinmemberRudi66620-Aug-09 22:54 
QuestionVS2005 compatible code PinmemberDouglas R. Keesler22-Dec-06 15:28 
AnswerRe: VS2005 compatible code PinmemberNitron23-Dec-06 4:10 
GeneralRe: VS2005 compatible code [modified] Pinmemberspoida25-Jun-07 20:52 
GeneralLoading resource with Unicode enabled Pinmemberbunton30-Jan-06 11:16 
Generalre: using with Plain Text, etc.... PinmemberDouglas R. Keesler13-Jun-05 13:24 
GeneralRe: re: using with Plain Text, etc.... PinmemberGarth J Lancaster16-Aug-05 14:13 
GeneralRe: re: using with Plain Text, etc.... PinmemberDouglas R. Keesler17-Aug-05 13:21 

There may be a more programmatically "pure" way of handling this -- this is a bit of a hack, I know... but it's a simple fix, and it works for me.

Good luck with your project.
 


In business, if two people always agree, one of them is unnecessary.

 


GeneralRe: re: using with Plain Text, etc.... PinmemberGarth J Lancaster17-Aug-05 13:25 
GeneralAwesome - 5 out of 5 PinmemberDouglas R. Keesler12-Jun-05 16:46 
GeneralRe: Awesome - 5 out of 5 PinmemberNitron13-Jun-05 3:01 
GeneralRe: Awesome - 5 out of 5 PinmemberDouglas R. Keesler13-Jun-05 13:14 
GeneralRe: Awesome - 5 out of 5 PinmemberAvis p13-Jun-05 14:39 
GeneralRe: Awesome - 5 out of 5 PinmemberDouglas R. Keesler13-Jun-05 15:04 
GeneralRe: Awesome - 5 out of 5 PinmemberAvis p13-Jun-05 16:44 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Jun 2005
Article Copyright 2005 by Nitron
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid