Click here to Skip to main content
Click here to Skip to main content

Windows Vista BCD

By , 16 Jun 2007
Rate this:
Please Sign up or sign in to vote.

Windows Vista BCD Editor

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?)

Old Boot.ini

BCD is a new boot environment for Windows Vista.
BCD = Boot Configuration Data

2. BCD Hierarchy

BCD hierachy

  • 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

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.

Our application

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

Classes generate

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).

BCD Constants COM

IV. Samples code

Example code to load all OS to listbox:

//USE IMPERSONATE LEVEL
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;

ManagementScope managementScope = new ManagementScope(@"root\WMI", 
                            connectionOptions);

// SYSTEM STORE GUID = {9dea862c-5cdd-4e70-acc1-f32b344d4795}
BcdObject systemObject = new BcdObject(managementScope, 
            "{9dea862c-5cdd-4e70-acc1-f32b344d4795}", "");

// GET LIST OF OS
ManagementBaseObject mboOut;
bool success = systemObject.GetElement((uint)
    BCDConstants.BcdBootMgrElementTypes.BcdBootMgrObjectList_DisplayOrder,
     out mboOut);

if (success)
{
    string[] osIdList = (string[])mboOut.GetPropertyValue("Ids");
         // LOOP FOR ALL OS Ids (GUID)
         foreach (string osGuid in osIdList)
         {
              BcdObject osObj = new BcdObject(managementScope, osGuid, "");
              // GET DESCRIPTION STRING
              success = osObj.GetElement((uint)
         BCDConstants.BcdLibraryElementTypes.BcdLibraryString_Description, 
         out mboOut);

              if (success)
              {
                  // USE A CLASS TO KEEP OS OBJECT AND NAME FOR LATER USE
                  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!

License

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

About the Author

Tran Dinh Hop

Vietnam Vietnam
Started my first .NET application 5 years ago.
2 years worked with VC++
Currently : Hanoi Aptech Computer Education Trainer

Comments and Discussions

 
GeneralInvalid Class (XP) Pinmemberstarbase23110-Apr-10 9:54 
GeneralHehehe PinmemberAcgiuna2-Jul-09 4:39 
GeneralHelp on BCD Pinmemberxero92006-Sep-08 8:21 
GeneralRe: Help on BCD PinmemberTran Dinh Hop7-Sep-08 1:13 
QuestionRead Vista BCD file from Windows XP Pinmemberparagcoder3-Oct-07 23:38 
AnswerRe: Read Vista BCD file from Windows XP PinmembertheMadCoder7-Mar-12 8:22 
GeneralConfirms what I though about the Vista designers! PinmemberBob100018-Jun-07 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 16 Jun 2007
Article Copyright 2007 by Tran Dinh Hop
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid