We help IT Professionals succeed at work.

mfc load HTML resource text into a Cstring

rileo8
rileo8 asked
on
Hi,

i have an html file into a HTML resource.

How can i load its content into a CString?

i tried this:

 
static bool GetHTML(const int& idrHTML, CString& rString)
{
	bool retVal = false;
	try
	{		
		HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(idrHTML), MAKEINTRESOURCE(idrHTML));
		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;
}

Open in new window

static bool GetHTML(const int& idrHTML, CString& rString)
{
	bool retVal = false;
	try
	{		
		HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(idrHTML), MAKEINTRESOURCE(idrHTML));
		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;
}

Open in new window


but it returns nothing...
Comment
Watch Question

jkr
Top Expert 2012

Commented:
Well, there's basically just one mistake:
HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(idrHTML), MAKEINTRESOURCE(idrHTML));

Open in new window


should be
HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(idrHTML),RT_HTML);

Open in new window



See also http://www.codeproject.com/KB/cpp/GetHTML.aspx ("How to Extract Text from an HTML Resource") which has the same code, but without the error

Author

Commented:
RT_HTML is not found...
jkr
Top Expert 2012

Commented:
At compile time or runtime?

Author

Commented:
At compile time...
jkr
Top Expert 2012

Commented:
Be sure to

#include <winuser.>

That's where it is defined. To test if it works at all, try

#define RT_HTML         MAKEINTRESOURCE(23)

Author

Commented:
Now is like this:

#include <winuser.h>

#define RT_HTML         MAKEINTRESOURCE(23)


static bool GetHTML(const int& idrHTML, CString& rString)
{
      bool retVal = false;
      try
      {            
            //HRSRC hSrc =  FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(idrHTML), _T("TEXTFILE"));
           HRSRC hSrc =  FindResource(NULL, MAKEINTRESOURCE(idrHTML), RT_HTML);
            if (hSrc != NULL)

But hSrc is still empty...
Top Expert 2012
Commented:
Then try
static bool GetHTML(const int& idrHTML, CString& rString)
{
	bool retVal = false;
	try
	{		
		HRSRC hSrc = FindResource(NULL,RT_HTML, MAKEINTRESOURCE(idrHTML));
		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);
		}
                else // hSrc == NULL 
                {
                    DWORD dwErr = GetLastError(); // please check what this value is
                }             
	}
	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;
}

Open in new window

Author

Commented:
Nothing change...
jkr
Top Expert 2012

Commented:
And the error code is... ? You see, I added

// please check what this value is

on purpose in the above code snippet. I don't have a crystal ball, unfortunately.