深入DriverStoreExplorer架构:多API驱动引擎设计

深入DriverStoreExplorer架构:多API驱动引擎设计

【免费下载链接】DriverStoreExplorer Driver Store Explorer [RAPR] 【免费下载链接】DriverStoreExplorer 项目地址: https://gitcode.com/gh_mirrors/dr/DriverStoreExplorer

DriverStoreExplorer采用基于IDriverStore接口的多API架构设计,通过统一的抽象层为多种Windows驱动管理API提供标准化契约。该架构包含三个核心实现:NativeDriverStore(Windows Native API)、DismUtil(DISM API)和PnpUtil(PnPUtil命令行),支持在线和离线两种驱动存储操作模式。接口设计遵循面向接口编程原则,定义了11个关键成员方法,包括驱动枚举、删除、添加和导出等功能,同时通过属性暴露各实现的能力特性,形成完整的能力矩阵。工厂模式与智能回退机制确保应用程序能够根据系统能力和用户配置动态选择最合适的底层实现。

IDriverStore接口设计与实现

DriverStoreExplorer的核心架构围绕IDriverStore接口构建,该接口定义了驱动存储操作的标准契约,为多种Windows驱动管理API提供了统一的抽象层。这种设计模式使得应用程序能够无缝切换不同的底层实现,同时保持用户界面和业务逻辑的一致性。

接口定义与设计理念

IDriverStore接口采用面向接口编程(IOP)的设计原则,将驱动存储的核心操作抽象为11个关键成员:

public interface IDriverStore
{
    DriverStoreType Type { get; }
    string OfflineStoreLocation { get; }
    bool SupportAddInstall { get; }
    bool SupportForceDeletion { get; }
    bool SupportDeviceNameColumn { get; }
    bool SupportExportDriver { get; }
    bool SupportExportAllDrivers { get; }
    
    List<DriverStoreEntry> EnumeratePackages();
    bool DeleteDriver(DriverStoreEntry driverStoreEntry, bool forceDelete);
    bool AddDriver(string infFullPath, bool install);
    bool ExportDriver(DriverStoreEntry driverStoreEntry, string destinationPath);
    bool ExportAllDrivers(string destinationPath);
}

接口设计遵循了单一职责原则,每个方法都专注于特定的驱动存储操作。属性设计则体现了接口的元数据特性,允许调用方在运行时查询实现的具体能力。

多实现架构

DriverStoreExplorer通过三个具体类实现IDriverStore接口,每种实现对应不同的Windows驱动管理技术栈:

实现类技术基础适用场景特性优势
NativeDriverStoreWindows Native APIWindows 7+ 原生支持高性能、完整功能支持
DismUtilDISM APIWindows 8+ 系统离线映像支持、企业级特性
PnpUtilPnPUtil 命令行兼容性最佳广泛兼容、稳定可靠

mermaid

核心方法实现细节

枚举驱动包(EnumeratePackages)

EnumeratePackages方法是接口中最复杂的方法之一,它负责检索系统中的所有第三方驱动包。以NativeDriverStore实现为例:

public List<DriverStoreEntry> EnumeratePackages()
{
    var ptr = NativeMethods.DriverStoreOpen(null, null, 0, IntPtr.Zero);
    if (ptr == IntPtr.Zero)
    {
        throw new Win32Exception();
    }

    List<DriverStoreEntry> driverStoreEntries = new List<DriverStoreEntry>();

    try
    {
        GCHandle handle = GCHandle.Alloc(driverStoreEntries);
        try
        {
            NativeMethods.DriverStoreEnum(
                ptr,
                DriverStoreEnumFlags.OemOnly,
                EnumDriverPackages,
                GCHandle.ToIntPtr(handle));
        }
        finally
        {
            handle.Free();
        }
    }
    finally
    {
        NativeMethods.DriverStoreClose(ptr);
    }

    return ConfigManager.FillDeviceInfo(driverStoreEntries);
}

该方法使用Windows Native API进行底层驱动存储枚举,通过回调机制收集驱动包信息,最后通过ConfigManager补充设备关联信息。

驱动删除操作(DeleteDriver)

删除操作实现了强制删除和普通删除两种模式,处理正在使用的驱动时需要特殊权限:

public bool DeleteDriver(DriverStoreEntry driverStoreEntry, bool forceDelete)
{
    if (driverStoreEntry == null)
    {
        throw new ArgumentNullException(nameof(driverStoreEntry));
    }

    switch (this.Type)
    {
        case DriverStoreType.Online:
            try
            {
                SetupAPI.DeleteDriver(driverStoreEntry, forceDelete);
            }
            catch (Win32Exception ex)
            {
                Trace.TraceError(ex.ToString());
                return false;
            }
            return true;

        case DriverStoreType.Offline:
            throw new NotImplementedException();

        default:
            throw new NotSupportedException();
    }
}
驱动导出功能(ExportDriver)

导出功能实现了完整的驱动备份机制,包括文件夹创建和架构感知:

public bool ExportDriver(DriverStoreEntry driverStoreEntry, string destinationPath)
{
    if (driverStoreEntry == null)
    {
        throw new ArgumentNullException(nameof(driverStoreEntry));
    }

    var targetPath = Path.Combine(destinationPath, driverStoreEntry.GetDriversBackupFolderName());
    if (!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }

    switch (this.Type)
    {
        case DriverStoreType.Online:
            var ptr = NativeMethods.DriverStoreOpen(null, null, 0, IntPtr.Zero);
            if (ptr == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            try
            {
                var processorArchitecture = GetProcessorArchitecture(ptr);
                uint status = NativeMethods.DriverStoreCopy(
                    ptr,
                    Path.Combine(driverStoreEntry.DriverFolderLocation, driverStoreEntry.DriverInfName),
                    processorArchitecture,
                    IntPtr.Zero,
                    DriverStoreCopyFlags.None,
                    targetPath);

                return status == 0;
            }
            finally
            {
                NativeMethods.DriverStoreClose(ptr);
            }

        case DriverStoreType.Offline:
            throw new NotImplementedException();

        default:
            throw new NotSupportedException();
    }
}

工厂模式与运行时选择

DriverStoreFactory类负责根据系统能力和用户配置创建适当的IDriverStore实现:

public static IDriverStore CreateOnlineDriverStore()
{
    _ = Enum.TryParse(Settings.Default.DriverStoreOption, out DriverStoreOption driverStoreOption);

    switch (driverStoreOption)
    {
        case DriverStoreOption.Native:
            return new NativeDriverStore();
        case DriverStoreOption.DISM:
            return new DismUtil();
        case DriverStoreOption.PnpUtil:
            return new PnpUtil();
        default:
            throw new ArgumentException($"Unsupported driver store option: {driverStoreOption}");
    }
}

工厂模式还包含了智能回退机制,当首选API不可用时自动选择替代方案:

mermaid

驱动存储类型与能力矩阵

IDriverStore接口通过DriverStoreType枚举区分在线和离线存储操作:

public enum DriverStoreType
{
    Online,    // 操作本地运行系统的驱动存储
    Offline,   // 操作Windows映像文件中的驱动存储
}

每种实现类型支持的能力通过接口属性暴露,形成完整的能力矩阵:

能力特性NativeDriverStoreDismUtilPnpUtil
添加安装驱动✅ 在线模式✅ 在线/离线✅ 在线模式
强制删除驱动✅ 在线模式
设备名称列✅ 在线模式
单个驱动导出
批量驱动导出
离线映像支持

错误处理与日志记录

接口实现采用了统一的错误处理模式,所有操作都返回布尔值表示成功状态,并通过Windows事件日志记录详细错误信息:

try
{
    SetupAPI.AddDriver(infFullPath, install);
}
catch (Win32Exception ex)
{
    Trace.TraceError(ex.ToString());
    return false;
}
return true;

这种设计确保了应用程序的稳定性和可调试性,同时为上层调用者提供了清晰的执行结果反馈。

扩展性与未来演进

IDriverStore接口的设计充分考虑了扩展性,新的驱动存储技术可以通过实现该接口轻松集成到系统中。接口的属性驱动设计允许动态能力发现,使得应用程序能够自适应不同的运行环境和Windows版本。

通过这种精心设计的接口架构,DriverStoreExplorer实现了驱动管理功能的统一抽象,为Windows系统管理员提供了强大而灵活的工具,无论底层使用哪种技术栈,都能获得一致的用户体验和功能完整性。

Native Windows API驱动引擎

DriverStoreExplorer的核心优势之一是其多API架构设计,其中Native Windows API驱动引擎作为最底层的原生实现,提供了对Windows驱动存储最直接和高效的访问能力。该引擎通过直接调用Windows内核级别的API函数,实现了对驱动包的枚举、安装、删除和导出等核心操作。

核心架构设计

Native Windows API引擎基于NativeDriverStore类实现,该类实现了IDriverStore接口,提供了统一的驱动存储操作接口。其架构采用分层设计,将底层原生API调用与上层业务逻辑完全分离:

mermaid

驱动包枚举机制

Native API引擎通过DriverStoreOpenDriverStoreEnumDriverStoreClose三个核心函数实现驱动包的枚举。整个过程采用回调机制,确保高效处理大量驱动包数据:

public List<DriverStoreEntry> EnumeratePackages()
{
    var ptr = NativeMethods.DriverStoreOpen(null, null, 0, IntPtr.Zero);
    if (ptr == IntPtr.Zero) throw new Win32Exception();
    
    List<DriverStoreEntry> driverStoreEntries = new List<DriverStoreEntry>();
    
    try
    {
        GCHandle handle = GCHandle.Alloc(driverStoreEntries);
        try
        {
            NativeMethods.DriverStoreEnum(
                ptr,
                DriverStoreEnumFlags.OemOnly,
                EnumDriverPackages,
                GCHandle.ToIntPtr(handle));
        }
        finally { handle.Free(); }
    }
    finally { NativeMethods.DriverStoreClose(ptr); }
    
    return ConfigManager.FillDeviceInfo(driverStoreEntries);
}

枚举过程中,每个驱动包的详细信息通过属性查询API获取,包括:

属性类型API函数描述
驱动提供商DEVPKEY_DriverPackage_ProviderName驱动程序的提供商名称
签名者DEVPKEY_DriverPackage_SignerName驱动程序的数字签名者
驱动日期DEVPKEY_DriverPackage_DriverDate驱动程序的发布日期
驱动版本DEVPKEY_DriverPackage_DriverVersion驱动程序的版本号
类GUIDDEVPKEY_DriverPackage_ClassGuid设备类的全局唯一标识符

驱动安装与删除

Native API引擎提供了完整的驱动生命周期管理功能,支持在线和离线两种模式的驱动操作:

驱动安装流程

mermaid

驱动删除实现

驱动删除操作支持强制删除模式,通过组合使用DiUninstallDriverSetupUninstallOEMInfAPI实现:

public static void DeleteDriver(DriverStoreEntry driverStoreEntry, bool forceDelete)
{
    if (forceDelete && MethodExists("newdev.dll", "DiUninstallDriverW"))
    {
        NativeMethods.DiUninstallDriver(
            IntPtr.Zero,
            Path.Combine(driverStoreEntry.DriverFolderLocation, driverStoreEntry.DriverInfName),
            DIURFLAG.NO_REMOVE_INF,
            out _);
    }

    NativeMethods.SetupUninstallOEMInf(
        driverStoreEntry.DriverPublishedName,
        forceDelete ? SetupUOInfFlags.SUOI_FORCEDELETE : SetupUOInfFlags.NONE,
        IntPtr.Zero);
}

驱动导出功能

Native API引擎的驱动导出功能通过DriverStoreCopyAPI实现,支持按处理器架构导出驱动包:

public bool ExportDriver(DriverStoreEntry driverStoreEntry, string destinationPath)
{
    var targetPath = Path.Combine(destinationPath, driverStoreEntry.GetDriversBackupFolderName());
    Directory.CreateDirectory(targetPath);
    
    var ptr = NativeMethods.DriverStoreOpen(null, null, 0, IntPtr.Zero);
    try
    {
        var processorArchitecture = GetProcessorArchitecture(ptr);
        
        uint status = NativeMethods.DriverStoreCopy(
            ptr,
            Path.Combine(driverStoreEntry.DriverFolderLocation, driverStoreEntry.DriverInfName),
            processorArchitecture,
            IntPtr.Zero,
            DriverStoreCopyFlags.None,
            targetPath);
            
        return status == 0;
    }
    finally { NativeMethods.DriverStoreClose(ptr); }
}

设备信息集成

为了提供完整的驱动管理体验,Native API引擎与ConfigManager集成,获取设备与驱动的关联信息:

internal static List<DeviceDriverInfo> GetDeviceDriverInfo()
{
    var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, DIGCF.ALLCLASSES);
    
    List<DeviceDriverInfo> deviceDriverInfos = new List<DeviceDriverInfo>();
    SP_DEVINFO_DATA deviceInfo = new SP_DEVINFO_DATA();
    
    for (int i = 0; NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfo); i++)
    {
        deviceDriverInfos.Add(new DeviceDriverInfo(
            TryGetDeviceRegistryProperty(deviceInfoSet, deviceInfo, SPDRP_FRIENDLYNAME),
            GetDriverInf(deviceInfoSet, deviceInfo),
            TryGetDevicePropertyInfo<DateTime>(deviceInfoSet, deviceInfo, DEVPKEY_Device_DriverDate),
            // 其他属性...
        ));
    }
    
    return deviceDriverInfos;
}

错误处理与异常管理

Native Windows API引擎实现了完善的错误处理机制,所有API调用都通过Win32Exception进行异常封装,确保操作失败时能够提供详细的错误信息:

try
{
    SetupAPI.AddDriver(infFullPath, install);
}
catch (Win32Exception ex)
{
   

【免费下载链接】DriverStoreExplorer Driver Store Explorer [RAPR] 【免费下载链接】DriverStoreExplorer 项目地址: https://gitcode.com/gh_mirrors/dr/DriverStoreExplorer

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值