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


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

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

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);
The links below will be very helpful for you to understand this article:
| You must Sign In to use this message board. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||