Click here to Skip to main content
15,884,904 members
Articles / Desktop Programming / MFC

Import/Export Registry Sections as XML

Rate me:
Please Sign up or sign in to vote.
4.33/5 (16 votes)
21 Jan 20033 min read 165.9K   6.1K   73  
Export registry sections as XML to simplify registry diffs
This article details a tool aimed to import/export registry sections in XML format, to make registry diff easier in practice.
#include "stdafx.h"
#include "registryxml.h"

#include "registryxmlDoc.h"
#include "registryxmlView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// registryxmlView

IMPLEMENT_DYNCREATE(registryxmlView, CListView)

BEGIN_MESSAGE_MAP(registryxmlView, CListView)
	//{{AFX_MSG_MAP(registryxmlView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// registryxmlView construction/destruction

registryxmlView::registryxmlView()
{
	// TODO: add construction code here

}

registryxmlView::~registryxmlView()
{
}

BOOL registryxmlView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style |= LVS_REPORT; // | LVS_SORTASCENDING;

	return CListView::PreCreateWindow(cs);
}


void registryxmlView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().

	//
	//
	//
	m_imglSmall.Create(16,16,ILC_MASK | ILC_COLOR32,2,1);
	CBitmap aze;
	aze.LoadBitmap(IDB_IMAGELIST);
	m_imglSmall.Add(&aze,RGB(255,0,255));

	// insert basic items in the ListCtrl
	CListCtrl &List = GetListCtrl();
	if (List)
	{
		List.SetImageList(&m_imglSmall,LVSIL_SMALL);

		List.InsertColumn(0,_T("Name"),LVCFMT_LEFT,150);
		List.InsertColumn(1,_T("Data"),LVCFMT_LEFT,350);
	}

}


void registryxmlView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{

	if (lHint==NULL) return; // that's the call from ::OnInitialUpdate() which is useless

	GetListCtrl().DeleteAllItems();

	CString szPath = (LPTSTR) lHint;
	if (szPath.IsEmpty()) return; // called when the user selects the root (useless)

	// open the key now
	HKEY hKey;
	long hr;

	CString szMainKeyname = szPath;

	int nSlash = szPath.Find(_T("\\") );
	if (nSlash>-1)
	{
		szMainKeyname = szPath.Left( nSlash );
		szPath = szPath.Right( szPath.GetLength()-(nSlash+1));
	}
	else
		szPath.Empty();

	hr = Helper_OpenKey(szMainKeyname, szPath, &hKey);

	if (hr==ERROR_SUCCESS) // it's ok
	{
		CPtrArray arrValues;

		SetCursor(LoadCursor(NULL, IDC_WAIT)); // hourglass

		AddRegistryValues(hKey, arrValues);

		SetCursor(LoadCursor(NULL, IDC_ARROW)); // back to normal cursor

		::RegCloseKey(hKey);


		// add the values to the ListView
		//
		int nbItems = arrValues.GetSize();
		for (int i=0; i<nbItems; i++)
		{

			KeyValue *p = (KeyValue*) arrValues.GetAt(i);
			if (!p) continue;

			DWORD dwType = p->GetType();

			int nValueIcon = ILI_STRING;
			if ( (dwType>=REG_BINARY && dwType<=REG_DWORD_BIG_ENDIAN) || dwType==11)
				nValueIcon = ILI_BINARY;

			CStringArray a;
			a.Add( p->GetName() );
			a.Add( p->GetValue() );
			InsertItem( i, nValueIcon, a);

			delete p; // destroy the now useless temp struct
		}


	}

}




BOOL registryxmlView::InsertItem(	int				nIndex,
									int				nImage,
									CStringArray	&szArray)
{
	if (szArray.GetSize()==0) return FALSE;

	CString szName = szArray.GetAt(0);

	LV_ITEM lvi;

	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
	lvi.iItem = nIndex;
	lvi.iSubItem = 0;
	lvi.iImage = nImage;
	lvi.pszText= szName.GetBuffer(0);

	if (GetListCtrl().InsertItem(&lvi)== -1) return FALSE;

	// insert subitem #i
	for (int i=1; i<szArray.GetSize() ; i++)
	{
		CString s = szArray.GetAt(i);

		GetListCtrl().SetItemText(nIndex,i, s.GetBuffer(0) );	
	}

	return TRUE;
}

long registryxmlView::GetNbItems()
{
	return GetListCtrl().GetItemCount();
}


/////////////////////////////////////////////////////////////////////////////
// registryxmlView drawing

void registryxmlView::OnDraw(CDC* pDC)
{
	registryxmlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// registryxmlView printing

BOOL registryxmlView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void registryxmlView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void registryxmlView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// registryxmlView diagnostics

#ifdef _DEBUG
void registryxmlView::AssertValid() const
{
	CView::AssertValid();
}

void registryxmlView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

registryxmlDoc* registryxmlView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(registryxmlDoc)));
	return (registryxmlDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// registryxmlView message handlers


afx_msg void registryxmlView::OnLButtonDblClk( UINT nFlags, CPoint p)
{
	// Select the item the user clicked on.
	UINT uHitFlags;
	int nItem = GetListCtrl().HitTest(p, &uHitFlags);
	if (nItem>-1)
	{
		CString s = GetListCtrl().GetItemText(nItem,1); // 1 = second column
		if (!s.IsEmpty())
		{
			CString sWithCR;
			// add a carriage return each 50 chars
			int nLength = s.GetLength();
			int i =0;
			while ( nLength-i >= 100 )
			{
				sWithCR += s.Mid(i, 100);
				sWithCR += _T("\r\n");
				i += 100; // 
			}
			sWithCR += s.Right(nLength-i);

			AfxMessageBox(sWithCR);
		}
	}

}

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.


Written By
France France
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

Comments and Discussions