Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / WTL

Generator for Include File Guards

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
18 Jul 2001 55.4K   578   19  
A little utility that generates header file guards against multiple inclusion.
#include "stdinc.h"
#include "resource.h"
#include "maindialog.h"
#include "aboutbox.h"

const TCHAR fmtGuid[] = _TEXT(
	"%08lx_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x");

const TCHAR fmtGuard[] = _TEXT(
	"#if !defined(%s_INCLUDED_%s)\r\n"
	"#define %s_INCLUDED_%s\r\n"
	"\r\n"
	"#endif // !defined(%s_INCLUDED_%s)\r\n");

BOOL CMainDialog::PreTranslateMessage(MSG* pMsg)
{
	return IsDialogMessage(pMsg);
}

BOOL CMainDialog::OnIdle()
{
	return FALSE;
}


void CMainDialog::CloseDialog(int val)
{
	m_header.Detach();
	m_guard.Detach();

	DestroyWindow();
	::PostQuitMessage(val);
}

CString CMainDialog::GetWindowText(const CWindow& wnd)
{
	int length = wnd.GetWindowTextLength() + 1;

	CString str;
	wnd.GetWindowText(str.GetBuffer(length), length);
	str.ReleaseBuffer();

	return str;
}

// Create guid and transform into text format
CString CMainDialog::CreateGuid()
{
	GUID guid;
	CoCreateGuid(&guid);

	CString str;
	str.Format(
		fmtGuid,
		guid.Data1,
		guid.Data2,
		guid.Data3,
		guid.Data4[0],	guid.Data4[1], guid.Data4[2], guid.Data4[3],
		guid.Data4[4],	guid.Data4[5], guid.Data4[6], guid.Data4[7]);

	return str;
}

CString CMainDialog::CreateGuard(CString strHeader, CString strGuid)
{
	if (strHeader.Find(_TEXT('.')) == -1)
	{
		strHeader = strHeader + _TEXT(".h");
	}

	strHeader.Replace(_TEXT('.'), _TEXT('_'));

	strHeader.MakeUpper();
	strGuid.MakeUpper();

	CString str;
	str.Format(
		fmtGuard, 
		strHeader, strGuid,
		strHeader, strGuid,
		strHeader, strGuid);

	return str;
}

LRESULT CMainDialog::OnInitDialog(HWND, LPARAM)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	// attach header file edit control
	HWND hHeader = GetDlgItem(IDC_HEADER);
	ATLASSERT(hHeader != 0);
	m_header.Attach(hHeader);

	// attach guard edit control
	HWND hGuard = GetDlgItem(IDC_GUARD);
	ATLASSERT(hGuard != 0);
	m_guard.Attach(hGuard);

	return true;
}

LRESULT CMainDialog::OnAppAbout(uint /*notifyCode*/, int /*id*/, HWND /*hCtrl*/)
{
	CAboutBox about;
	about.DoModal();
	return 0;
}

LRESULT CMainDialog::OnOK(uint /*notifyCode*/, int /*id*/, HWND /*hCtrl*/)
{
	CString strHeader = GetWindowText(m_header);

	CString strGuid = CreateGuid();

	CString strGuard = CreateGuard(strHeader, strGuid);
	m_guard.SetWindowText(strGuard);

	GotoDlgCtrl(m_guard);

	return 0;
}

LRESULT CMainDialog::OnCancel(uint /*notifyCode*/, int id, HWND /*hCtrl*/)
{
	CloseDialog(id);
	return 0;
}

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 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
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions