首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎样才能得到和bcdedit /enum一样的结果呢?

我怎样才能得到和bcdedit /enum一样的结果呢?
EN

Stack Overflow用户
提问于 2017-09-01 00:35:06
回答 2查看 727关注 0票数 0

除了使用wmi和C#之外,我正在尝试获取与使用bcdedit命令"bcdedit /enum ALL“获得的相同信息。我知道如何获取bootmgr条目(参见代码),但我无法获取所有条目,尤其是设备选项是我要查找的信息。有谁知道如何做到这一点吗?

这是我用来获取标准和遗留操作系统启动条目的代码。

代码语言:javascript
复制
    public class BCDWMI
{
    public static readonly UInt32 BCDE_STANDARD_OS_ENTRY = 0x10200003;
    public static readonly UInt32 BCDE_LEGACY_OS_ENTRY = 0x10300006;
    public static readonly UInt32 BcdLibraryElementTypeString_Description = 0x12000004;

    public static Dictionary<string, string> EnumerateObjectsByType(uint bcdType, string storePath)
    {

        Dictionary<string, string> dictEntries = null;

        ConnectionOptions options = new ConnectionOptions();
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.EnablePrivileges = true;
        ManagementScope MgmtScope = new ManagementScope("root\\WMI", options);
        ManagementPath MgmtPath = new ManagementPath("root\\WMI:BcdStore.FilePath='" + storePath + "'");
        ManagementObject bcdStore = new System.Management.ManagementObject(MgmtScope, MgmtPath, null);
        ManagementBaseObject[] mboArray;

        bool success = EnumerateObjects(bcdStore, bcdType, out mboArray);
        if (success)
        {
            dictEntries = new Dictionary<string, string>();

            foreach (ManagementBaseObject mbo in mboArray)
            {
                ManagementPath BcdObjectPath = new ManagementPath("root\\WMI:BcdObject.Id=\"" + mbo.GetPropertyValue("Id") + "\",StoreFilePath='" + storePath + "'");

                ManagementObject BcdObject = new ManagementObject(MgmtScope, BcdObjectPath, null);
                ManagementBaseObject Element;
                String Description = String.Empty;
                try
                {
                    bool getDescripStatus = GetElement(BcdObject, BcdLibraryElementTypeString_Description, out Element);
                    if (getDescripStatus)
                        Description = Element.GetPropertyValue("String").ToString();
                }
                catch (Exception)
                {
                }
                dictEntries.Add((string)mbo.GetPropertyValue("Id"), String.Format("Type: {0:X8} {1}", mbo.GetPropertyValue("Type"), Description));
            }
        }
        return dictEntries;
    }

    public static bool EnumerateObjects(ManagementObject bcdStore, uint Type, out System.Management.ManagementBaseObject[] Objects)
    {
        System.Management.ManagementBaseObject inParams = null;
        inParams = bcdStore.GetMethodParameters("EnumerateObjects");
        inParams["Type"] = ((uint)(Type));
        System.Management.ManagementBaseObject outParams = bcdStore.InvokeMethod("EnumerateObjects", inParams, null);
        Objects = ((System.Management.ManagementBaseObject[])(outParams.Properties["Objects"].Value));
        return System.Convert.ToBoolean(outParams.Properties["ReturnValue"].Value);
    }

    public static bool GetElement(ManagementObject bdcObject, uint Type, out System.Management.ManagementBaseObject Element)
    {
        System.Management.ManagementBaseObject inParams = null;
        inParams = bdcObject.GetMethodParameters("GetElement");
        inParams["Type"] = ((uint)(Type));
        System.Management.ManagementBaseObject outParams = bdcObject.InvokeMethod("GetElement", inParams, null);
        Element = ((System.Management.ManagementBaseObject)(outParams.Properties["Element"].Value));
        return System.Convert.ToBoolean(outParams.Properties["ReturnValue"].Value);
    }
}

为了查询系统存储,我像这样调用函数。

代码语言:javascript
复制
            Dictionary<string, string> StdOSEntries = BCDWMI.EnumerateObjectsByType(BCDWMI.BCDE_STANDARD_OS_ENTRY, String.Empty);
        foreach (String guid in StdOSEntries.Keys)
            Debug.WriteLine(String.Format("Id={0} {1}", guid,  StdOSEntries[guid]));
EN

回答 2

Stack Overflow用户

发布于 2017-09-14 22:17:24

如果您需要了解感兴趣的引导条目的类型值,可以使用bcdedit.exe查找该条目的GUID,然后将该条目加载到程序中,将GUID放入ManagementPath中,就像您在中所做的那样:

代码语言:javascript
复制
ManagementPath BcdObjectPath = new ManagementPath("root\\WMI:BcdObject.Id=\"" + "{Guid goes here}" + "\",StoreFilePath='" + storePath + "'");

然后获取Type属性的值,如下所示:

代码语言:javascript
复制
uint Type = (uint)BcdObject["Type"];
Console.WriteLine("{0:X8}", Type);

如果bcdedit.exe显示{current}或{bootmgr}之类的“众所周知”标识符,请使用this document将该标识符映射到GUID。

您还可以通过编程方式枚举类型,如下所示:

代码语言:javascript
复制
for (uint a = 1; a <= 10; a++) {
    uint FirmwareType = 0x10100000 | a;
    uint BootType = 0x10200000 | a;
    uint LegacyType = 0x10300000 | a;
    uint RealModeType = 0x10400000 | a;
}

它会给出像这里这样的标识符

代码语言:javascript
复制
public static readonly UInt32 BCDE_STANDARD_OS_ENTRY = 0x10200003;
public static readonly UInt32 BCDE_LEGACY_OS_ENTRY = 0x10300006;

有关这些标识符是如何构成的说明,请参阅WMI documentation on BcdObject class

票数 1
EN

Stack Overflow用户

发布于 2017-09-18 19:22:18

我找到了解决方案。就像往常一样,这是非常明显和简单的。我刚将0x0作为Type传递给调用。

代码语言:javascript
复制
Dictionary<string, string> StdOSEntries = BCDWMI.EnumerateObjectsByType(0, String.Empty);

这就是我所得到的。

代码语言:javascript
复制
    Id: {0ce4991b-e6b3-4b16-b23c-5e0d9250e5d9} Type: 20100000 
Id: {4636856e-540f-4170-a130-a84776f4c654} Type: 20100000 
Id: {6efb52bf-1766-41db-a6b3-0ee5eff72bd7} Type: 20200003 
Id: {7619dcc8-fafe-11d9-b411-000476eba25f} Type: 30000000 
Id: {7619dcc9-fafe-11d9-b411-000476eba25f} Type: 10200003 Windows Setup 
Id: {7ea2e1ac-2e61-4728-aaa3-896d9d0a9f0e} Type: 20100000 
Id: {7ff607e0-4395-11db-b0de-0800200c9a66} Type: 20200003 Hypervisor Settings
Id: {9dea862c-5cdd-4e70-acc1-f32b344d4795} Type: 10100002 Windows Boot Manager
Id: {a19f4228-8f0e-11e7-ac85-005056c00008} Type: 30000000 
Id: {a1c5dee9-8f0e-11e7-ac85-005056c00008} Type: 10200003 WinPE x86
Id: {a20de869-8f0e-11e7-ac85-005056c00008} Type: 10200003 WinPE amd64
Id: {b2721d73-1db4-4c62-bf78-c548a880142d} Type: 10200005 Windows Memory Diagnostic

我的设备选项现在以类型0x30000000列出。因此,如果我传递0x30000000,该函数将返回两个设备。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45986313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档