Click here to Skip to main content
15,881,607 members
Articles / Operating Systems / Windows
Article

Windows Vista BCD

Rate me:
Please Sign up or sign in to vote.
4.37/5 (7 votes)
16 Jun 2007CPOL3 min read 74.7K   1.6K   26   8
An article on the new Windows Vista Boot Environment

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:

C#
//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:

C#
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)


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

Comments and Discussions

 
QuestionCannot find the "Microsoft BCD Constants Library" Pin
Member 1097649813-Aug-14 10:32
Member 1097649813-Aug-14 10:32 
GeneralInvalid Class (XP) Pin
starbase23110-Apr-10 9:54
starbase23110-Apr-10 9:54 
GeneralHehehe Pin
Acgiuna2-Jul-09 4:39
Acgiuna2-Jul-09 4:39 
GeneralHelp on BCD Pin
xero92006-Sep-08 8:21
xero92006-Sep-08 8:21 
GeneralRe: Help on BCD Pin
Tran Dinh Hop7-Sep-08 1:13
Tran Dinh Hop7-Sep-08 1:13 
QuestionRead Vista BCD file from Windows XP Pin
paragcoder3-Oct-07 23:38
paragcoder3-Oct-07 23:38 
AnswerRe: Read Vista BCD file from Windows XP Pin
theMadCoder7-Mar-12 8:22
theMadCoder7-Mar-12 8:22 
GeneralConfirms what I though about the Vista designers! Pin
Bob100018-Jun-07 12:57
professionalBob100018-Jun-07 12:57 

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

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