Introduction
This article will guide you on how to edit Windows Vista Boot Configuration programmatically just like we did with our old friends XP or Windows 2000 by editing the boot.ini file.
1. What is Windows Vista BCD
Old Windows uses boot.ini to configure boot sequences, this boot.ini file will be loaded by ntldr.exe
An old boot.ini looks like this (very familiar, huh?)
BCD is a new boot environment for Windows Vista.
BCD = Boot Configuration Data
2. BCD Hierarchy
- BCD Store: Namespace container for BCD Objects and other elements that make up the contents of a store.
- BCD Object: Container for BCD Elements. The most common type of BCD Object describes a boot environment.
- BCD Element: An item of data, i.e. boot application name, OS device …
3. BCD System Store
So, take a look at the figure.
If you want to manipulate some boot information, you have to access the Windows Boot Manager BCD Object.
Want to show the old friend boot.ini file? Access the Legacy Boot Loader. Pretty simple, huh ?
4. How do we access BCD in .NET
In .NET, there's a namespace called System.Management
, which provides classes for managing information and events about the system, devices, and applications instrumental to the Windows Management Instrumentation (WMI) infrastructure.
We can manually use System.Management
's classes to manipulate BCD data, by using ManagementClass
, ManagementObject
… classes … … but it's not a good idea.
We will use some tricks here. We will create all the classes representing BCDStore
, BCDObject
, BCDElement
… (all these classes make used of ManagementClass
, ManagementObject
… of course) but we'll create it automatically by using a tool.
5. Generate classes by using mgmtclassgen.exe
There's a file called bcd.mof in the path system32\wbem. This file contains all the information we need to generate BCD classes.
And we'll use the mgmtclassgen.exe tool to generate these classes (this tool comes with Visual Studio .NET).
OK, enough talking. Let's have some fun with the new Windows Vista BCD.
6. Step by Step demo
We'll create an application that allows users to change the name of the OS display in the load screen and set the timeout value (these tasks were very easy with the old friend boot.ini)
I. Start Visual Studio 2005 and create a new Windows Application. Design the GUI.
II. Create BCD classes
Go to the Visual Studio 2005 Command Prompt, execute the command: mgmtclassgen BcdStore /N root\WMI /L CS
to generate BcdStore
class in Csharp language, namespace root\WMI (BcdStore
class resides in this namespace)
Similar for BcdObject
, BcdElement
…
After this step, you'll have all the classes you need in the .cs files.
III. Reference System.Management.dll and add all the .cs files you have created in step II to your project.
Add a reference to the COM components named Microsoft BCD constants library to get all the constants pre-defined for BCD (that you don't need to define manually).
IV. Samples code
Example code to load all OS to listbox:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
ManagementScope managementScope = new ManagementScope(@"root\WMI",
connectionOptions);
BcdObject systemObject = new BcdObject(managementScope,
"{9dea862c-5cdd-4e70-acc1-f32b344d4795}", "");
ManagementBaseObject mboOut;
bool success = systemObject.GetElement((uint)
BCDConstants.BcdBootMgrElementTypes.BcdBootMgrObjectList_DisplayOrder,
out mboOut);
if (success)
{
string[] osIdList = (string[])mboOut.GetPropertyValue("Ids");
foreach (string osGuid in osIdList)
{
BcdObject osObj = new BcdObject(managementScope, osGuid, "");
success = osObj.GetElement((uint)
BCDConstants.BcdLibraryElementTypes.BcdLibraryString_Description,
out mboOut);
if (success)
{
OS myOS = new OS();
myOS.Name = mboOut.GetPropertyValue("String").ToString();
myOS.GUID = osGuid;
lstOS.Items.Add(myOS);
}
}
}
Example code to set a new description for OS:
BcdObject osObj = ((OS)lstOS.SelectedItem).osObj;
osObj.SetStringElement(txtDisplayName.Text,
(uint)BCDConstants.BcdLibraryElementTypes.BcdLibraryString_Description);
Simple, huh?
References
The links below will be very helpful for you to understand this article:
History
- First version, no updates!
Started my first .NET application 5 years ago.
2 years worked with VC++
Currently : Hanoi Aptech Computer Education Trainer