Manipulate managed and native objects in C++ to show the registry in a WPF TreeView

Last time, we looked at how easy it is to add managed code to your existing C++ application. (Call managed code from your C++ code)

The sample below shows a more substantial C++ program which

  • Liberally intermixes both native and managed objects in C++ code for demo purposes.
  • Reads the registry recursively using native C++ code to call Windows API
  • Stores strings in both a native STL vector and a managed List<String> to demonstrate using these techniques
  • Also stores strings in a TreeViewItem
  • Uses Windows Presentation Foundation (WPF) to display the registry data in a TreeView
  • Subscribes to a WPF event Window->OnLoaded

We’ll start by creating a new C++ project.

File->New->Project->C++ General->Empty Project CppClr

Source->Files->Right-Click->Add New C++ File “CppClr”

Paste in the code below

Now change the project type to be Windows (not Console) and set the new entry point:

// Project->Properties

// set Linker->System->SubSystem Windows (/SUBSYSTEM:WINDOWS)

// Set Linker->Advance->entry point = mymain

Because we’re using WPF, we must set our main thread to be Single Threaded Apartment (STA). To do this for a C++/CLI project, we need to define our own entry point that gets called before the default entry point. One way to do this is to define a custom entry point “MyMain” that

  1. is marked with the STA attribute
  2. initializes COM as STA
  3. then calls mainCRTStartup to initialize the C Runtime library and calls our “main” program

If we comment out the STA attribute line, we get exceptions like:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in PresentationCore.dll Additional information: The calling thread must be STA, because many UI components require this.



File->New->Project->C++ General->Empty Project CppClr
Source->Files->Right-Click->Add New C++ File 

// Project->Properties
//		set Linker->System->SubSystem  Windows (/SUBSYSTEM:WINDOWS)
//		Set Linker->Advance->entry point = mymain


// CppClr.cpp : main project file.

//#include "stdafx.h"
#include "objbase.h"
#include "atlbase.h"
#include "string"
#include "vector"

using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;
using namespace std;

extern "C" void mainCRTStartup();

public ref class MyWindow : Window
	String ^_strReg;
	int _nTotal;
		_strReg = gcnew String(
		Title = _strReg;
		Height = 500;
		Width = 500;
		// subscribe to managed event
		this->Loaded += 
			gcnew RoutedEventHandler(

	void OnLoaded(Object ^Sender, RoutedEventArgs ^e)
		// create a new treeview
		auto tv = gcnew TreeView();
		// get the content
		List<String^>^ lst = GetRegKeys(_strReg, tv);
		// add content to the form
		this->Content = tv;
		// set the title
		this->Title = 
			"# reg keys {0} {1}", 

	//GetRegKeys is a native method that 
	// gets the reg keys, adds them to the passed in
	//  ItemsControl
	// both TreeView and TreeViewItem are ItemsControls
	// The parameters are both managed 
	// Even though not used, return a generic List<String> 
	// to demonstrate usage.
	List<String^>^ GetRegKeys(
		// a managed string 
		String ^ strReg, 
		ItemsControl ^itemsControl 
		// create a vector. Can't mix native/managed 
		//		(can't have a vector of a System.String)
		vector<wstring> vecKeys;  
		// create a generic list<string>
		auto lst = gcnew List<String ^>(); 

		// convert the Managed string to an IntPtr
		IntPtr wstrReg = 

		// CRegKey is an ATL class, 
		//	with a dtor to close the key
		CRegKey regkey; 

		DWORD dwResult = regkey.Open(
			(WCHAR *) wstrReg.ToPointer(), //IntPtr to WCHAR

		// don't leak this guy

		if (ERROR_SUCCESS == dwResult)
			for (int nIndex = 0; ; nIndex++)
				wstring strKey(200, L'\0'); // native string
				DWORD nLen = strKey.length();
				// enumerate the key
				if (ERROR_SUCCESS != 
					regkey.EnumKey(nIndex, &strKey[0], &nLen))
				// add the native string to the native vector
				// create a managed string and add it to the list
				String ^ str = gcnew String(;
				//create & store item in WPF TreeViewItem
				auto tvItem = gcnew TreeViewItem();
				auto childLst = GetRegKeys(
					strReg + "\\" + str,
					); // recur
				// set the text
				tvItem->Header = strReg+"\\"+ str;
				tvItem->IsExpanded = true;
		_nTotal += lst->Count;
		return lst;

// we need a new entry point 
// so we can set the STAThread attribute on 
// the main thread
int mymain()  //the new entry point so we can set STAThread
	//If we need COM, Init COM as single model apartment

	//Initialize the CRT,
	// which also calls our "main" program
	return 0;

// this main is called from CRuntime mainCRTStartup
int main()
	Console::WriteLine(L"Hello World");
	// create a new WPF Window
	auto win = gcnew MyWindow();
	// show it modally

	return 0;


Comments (0)