Click here to Skip to main content
15,900,973 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.6K   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 
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 

Well, buddy.. it's like this - the vast majority of visitors to these pages are not looking for articles, and certainly not looking to learn anything... they simply want cut/paste code... they are pissed if they have to modify the code to make it work for them... they want it plug-n-play.. drop in ready.

Furthermore, most of them have never written an article themselves, or even attempted to give back to the community... so I just don't worry about it.

I'm not nearly as advanced as you or many others here. I'm a web developer who took up programming secondarily. I never took a course... I taught myself from books, reading code, writing code, etc.. So one of the reasons I occasionally post articles is to get feedback from people like yourself -- suggestions, constructive criticisms, better programming ideas, etc... It improves my code, and my programming skills.

Unfortunately, I have to endure some flames and pure bashing occasionally, but I consider the source and just keep writing and keep reading. My revenge is that for a self-taught guy, who just took up C++ a little over a year ago, I am more advanced in my skills than many of my critics.

My advice... don't even look at the rating... and take the comments with a grain of salt. I looked at some of your other articles, and I didn't even know what the hell you were talking about. You are advanced way beyond me... so I wouldn't put much stalk in our little peon votes... but I did give you a 5, for what it's worth.


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

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.