Click here to Skip to main content
15,886,689 members
Articles / Desktop Programming / MFC

XHTMLTipOfTheDay - HTML Tip of the Day Dialog

Rate me:
Please Sign up or sign in to vote.
4.19/5 (17 votes)
14 Jun 2004CPOL4 min read 72.2K   1.3K   45  
XHTMLTipOfTheDay is a Tip of the Day dialog that displays HTML and has forward and back buttons.
// CreateFileFromResource.cpp  Version 1.1
//
// Author:  Hans Dietrich
//          hdietrich2@hotmail.com
//
// License:
//     This software is released into the public domain.  You are free to use
//     it in any way you like, except that you may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CreateFileFromResource.h"
#include <io.h>

DWORD CreateFileFromResource(UINT nID,							// resource id
							 LPCTSTR lpszResourceType,			// resource type
							 LPCTSTR lpszFile,					// output file name
							 LPCTSTR lpszDirectory /*= NULL*/,	// directory path
							 BOOL bOverWrite /*= FALSE*/)		// TRUE = overwrite existing file
{
	DWORD dwSize = 0;
	
	ASSERT(nID != 0);
	ASSERT((lpszFile != NULL) && (lpszFile[0] != _T('\0')));
	ASSERT((lpszResourceType != NULL) && (lpszResourceType[0] != _T('\0')));

	if ((nID == 0) ||
		(lpszFile == NULL) || 
		(lpszFile[0] == _T('\0')) ||
		(lpszResourceType == NULL) ||
		(lpszResourceType[0] == _T('\0')))
	{
		TRACE(_T("ERROR - CreateFileFromResource bad args\n"));
		return dwSize;
	}

	CString strFile = _T("");
	if ((lpszDirectory != NULL) && (lpszDirectory[0] != _T('\0')))
	{
		strFile = lpszDirectory;
		strFile.TrimRight();
		if (strFile.Right(1) != _T("\\"))
			strFile += _T("\\");
	}
	strFile += lpszFile;

	TRACE(_T("CreateFileFromResource: strFile=<%s>\n"), strFile);
	
	// check if we should overwrite existing file
	if (!bOverWrite)
	{
		if (_taccess(strFile, 00) == 0)
		{
			TRACE(_T("%s exists, will not overwrite\n"), strFile);
			return dwSize;
		}
	}
	
	// create the output file
	HANDLE hFile = ::CreateFile(strFile,
								GENERIC_READ | GENERIC_WRITE,
								FILE_SHARE_READ | FILE_SHARE_WRITE,
								NULL,
								CREATE_ALWAYS,
								FILE_ATTRIBUTE_NORMAL,
								NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		TRACE(_T("ERROR - failed to create %s\n"), strFile);
		return dwSize;
	}

	// The resource specified by nID is read in as a
	// lpszResourceType resource, and written to the file.
	//
	// In the .rc file, insert a line like the following:
	//
	//     IDU_FOO  TEXT  MOVEABLE PURE  "BAR.TXT"
	//
	// where BAR.TXT is the name of the file that contains the 
	// TEXT resource type.  The resource type specified by
	// lpszResourceType can be any string, including your own 
	// custom types, e.g.:
	//
	//     IDU_FOO  MYTYPE  MOVEABLE PURE  "BAR.DAT"

	TRACE(_T("reading  resource %d\n"), nID);

	HINSTANCE hinst = AfxGetResourceHandle();
	ASSERT(hinst);

	if (hinst)
	{
		HRSRC hrsrc = ::FindResource(hinst, MAKEINTRESOURCE(nID), lpszResourceType);
		ASSERT(hrsrc);

		if (hrsrc)
		{
			// get size of the resource
			dwSize = ::SizeofResource(hinst, hrsrc);
			TRACE(_T("resource dwSize=%u\n"), dwSize);
			ASSERT(dwSize);

			if (dwSize)
			{
				HGLOBAL hglbl = ::LoadResource(hinst, hrsrc);
				ASSERT(hglbl);

				if (hglbl)
				{
					LPTSTR lpBuf = (LPTSTR) ::LockResource(hglbl);
					ASSERT(lpBuf);

					// lpBuf points to first byte of file resource

					if (lpBuf)
					{
						DWORD dwBytesWritten = 0;
						BOOL bRet = ::WriteFile(hFile, lpBuf, dwSize, 
											&dwBytesWritten, NULL);
						if (!bRet)
						{
							dwSize = 0;
							TRACE(_T("ERROR - write failed\n"));
						}
						else
						{
							TRACE(_T("%u bytes written\n"), dwBytesWritten);
						}
	
						::UnlockResource(hglbl);
					}
					else
					{
						TRACE(_T("ERROR - LockResource failed\n"));
						dwSize = 0;
					}

					::FreeResource(hglbl);
				}
				else
				{
					TRACE(_T("ERROR - LoadResource failed\n"));
					dwSize = 0;
				}
			}
		}
	}
	
	::CloseHandle(hFile);

	return dwSize;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions