 |
|
|
 |
|
 |
hello all!
first i've tried the funtion to load my html resource but it only returned garbage. so i followed the instructions from bunton and spoida and now ith works like a charm
for all interessted in the complete funtion for vs2008 and unicode enabled, here it is:
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)
{
LPCSTR lpcHtml = static_cast<LPCSTR>(LockResource(hHeader));
if (lpcHtml != NULL)
{
rString = CString(lpcHtml, ::SizeofResource(NULL, hSrc));
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;
}
regards,
rudi
modified on Friday, August 21, 2009 6:02 AM
|
|
|
|
 |
|
 |
Hey Nitron... have you tried using this code in VS2005? It runs fine in debug, but crashes in release mode.
It compiles without warning, but crashes when you call the function. I can't figure out why. It's a problem with the data pointer. If you comment out the line that converts the char* to CString it will run without crashing (but obviously no data returned).
Any ideas?
In business, if two people always agree, one of them is unnecessary.
|
|
|
|
 |
|
 |
Douglas R. Keesler wrote: have you tried using this code in VS2005? It runs fine in debug, but crashes in release mode.
hmm... I only have C++ Express for 2005, so I can't check it. If I were to guess, I'd take a look at your character set and see if the debug mode is different than the release mode... that's really all I can think of.
Things like this are the very reasons I hate casting handles, it makes it difficult to debug... I guess another thing to try is to add a double NULL to the end of the html resource. Open it in binary mode and add 0x00 0x00 to the end of the file. Heck, go ahead and add four of them just in case.
|
|
|
|
 |
|
 |
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 -- modified at 2:58 Tuesday 26th June, 2007
|
|
|
|
 |
|
 |
If attempting to run this code in a Unicode enabled application (i.e. you've defined the _UNICODE symbol) and you're attempting to load an HTML resource that is saved in an external file in standard Windows-1252 or UTF-8 encoding, you will run into problems when trying to load the resource. Most notably, your CString value will always be empty. In order to fix the problem, change the following line of code: LPCTSTR lpcHtml = static_cast<LPCTSTR>(LockResource(hHeader)); to LPCSTR lpcHtml = static_cast<LPCSTR>(LockResource(hHeader)); I.e. you must cast it specfically to a LPCSTR (const char*) instead of LPCTSTR (const TCHAR*). p.s. Thanks for the great little code snippet!
|
|
|
|
 |
|
 |
I mentioned in my first post that you can use this trick with plain text as well, such as license agreements, mail-merge forms, or any text based documents that you want to display in a dialog or edit control.
This is true, but one small caveat... You must place an <html> at the beginning and an </html> tag at the end, and then strip them out after it is loaded into the string with Replace().
You may not have to use the tag at the beginning (I do for insurance), but you definitely need the one at the end, otherwise it will read into the next resource's memory block and you will get the text of more than one resource in your string.
Just thought I'd pass that tid-bit on, for what it's worth.
In business, if two people always agree, one of them is unnecessary.
|
|
|
|
 |
|
 |
thats interesting - thanks Douglas... Im beginning to experiment with embedding a text resource initially from a file into my exe, so a seperate program can use updateresource() on it later ... I'll keep your tip in mind when I start getting the sh*ts !! - I suspect this will be because the resource doesnt have anything marking it's end, so treating it as 'html' might be the trick
'G'
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
I ended up importing my file as data (RT_RCDATA) and using SizeOfResource to tell me how many bytes to read once I had a handle to the resource - it strikes me that you could also use SizeOfResource with RT_HTML, that way you wouldnt have to handle the (dummy) tags yourself
I can post some code if you need it
'G'
|
|
|
|
 |
|
 |
I've been looking for this for a long time... I've found how to do everything under the sun with an HTML document, except to load it into a string from resource... Sure beats hard coding documents into a string variable one line at a time. This will also work with plain text files (just load as HTML resource) -- could use for resume's, mail merge apps, EULA's or other text based documents to display in edit controls. This is an awesome little trick.. You're my new hero.:-> I give you 5 out of 5.
In business, if two people always agree, one of them is unnecessary.
|
|
|
|
 |
|
 |
Thanks! At least someone found it useful. I dunno why I have a 3.46, I think I wrote it well.
~Nitron.
ññòòïðïðB A start
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
Hmm… not very accurate, true, some of us does but the vast majority read, learns and benefit from the knowledge of many experts on this site (one of which is Nitron of course )
I for one got here following Nitron's articles after being deeply impressed and well educated by his excellent "An In-Depth Study of the STL Deque Container" article.
I think that the mostly silent crowd (me included) usually has nothing to contribute, still we highly regard the effort, brilliance and skill invested in the articles.
So Nitron - Thank You!
|
|
|
|
 |
|
 |
Agreed. I mean (by "vast majority") to say, majority of those who make rude remarks, or downrate articles for no apparent reason and with little or no explanation.
In business, if two people always agree, one of them is unnecessary.
|
|
|
|
 |
|
 |
Douglas R. Keesler wrote:
In business, if two people always agree, one of them is unnecessary
I guess you have just rendered me unnecessary (again )
|
|
|
|
 |