You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

可用于更新设备驱动的Windows API?C#/C++实现方案咨询

一、实现驱动更新自动化的Windows API

以下是可实现设备管理器驱动更新向导功能的核心Windows API:

  • SetupDi系列API:这是设备驱动安装的核心工具集,关键函数包括:
    • SetupDiGetClassDevs:枚举系统设备,可定位「Other devices」分类下的「802.11ac NIC」设备
    • SetupDiEnumDeviceInfo:遍历设备列表,获取目标设备的详细信息
    • SetupDiGetDeviceRegistryProperty:读取设备硬件ID等属性,用于匹配对应驱动
    • SetupDiInstallDriver:从指定路径为目标设备安装匹配驱动,对应“浏览计算机查找驱动”的核心逻辑
  • UpdateDriverForPlugAndPlayDevices:自动搜索指定路径下的驱动包,为目标设备完成更新,和向导的核心行为完全一致
  • DiInstallDriver:简化驱动安装流程的封装API,可处理驱动包的验证、匹配与安装全步骤
二、用C#/C++实现驱动包添加到驱动商店

C++实现方案

直接调用SetupAPI系列函数,对应pnputil的核心功能:

#include <Windows.h>
#include <SetupAPI.h>
#include <iostream>

#pragma comment(lib, "SetupAPI.lib")

int main() {
    const wchar_t* driverPath = L"C:\\Path\\To\\Driver";
    wchar_t infSearchPattern[MAX_PATH];
    swprintf_s(infSearchPattern, MAX_PATH, L"%s\\*.inf", driverPath);

    WIN32_FIND_DATAW findData;
    HANDLE hFind = FindFirstFileW(infSearchPattern, &findData);
    if (hFind == INVALID_HANDLE_VALUE) {
        std::wcerr << L"未找到任何INF驱动文件" << std::endl;
        return 1;
    }

    do {
        if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
            wchar_t fullInfPath[MAX_PATH];
            swprintf_s(fullInfPath, MAX_PATH, L"%s\\%s", driverPath, findData.cFileName);

            // 将驱动包添加到驱动商店,自动处理子目录
            BOOL result = SetupCopyOEMInfW(
                fullInfPath,
                driverPath,
                SPOST_PATH,
                0,
                nullptr,
                0,
                nullptr,
                nullptr
            );

            if (result) {
                std::wcout << L"驱动添加成功:" << findData.cFileName << std::endl;
            } else {
                std::wcerr << L"驱动添加失败:" << findData.cFileName 
                          << L",错误码:" << GetLastError() << std::endl;
            }
        }
    } while (FindNextFileW(hFind, &findData));

    FindClose(hFind);
    return 0;
}

注:SetupCopyOEMInf函数对应pnputil /add-driver的核心逻辑,SPOST_PATH参数会自动遍历子目录(匹配/subdirs),若需自动安装驱动,可搭配SetupDiInstallDriver进一步处理设备匹配与安装。

C#实现方案

通过P/Invoke调用Windows原生API实现:

using System;
using System.Runtime.InteropServices;
using System.IO;

class DriverStoreManager
{
    [DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool SetupCopyOEMInf(
        string sourceInfPath,
        string sourceMediaPath,
        int sourceMediaType,
        int copyStyle,
        string destInfPath,
        int destInfPathSize,
        out int requiredSize,
        string destInfComponent
    );

    private const int SPOST_PATH = 2;

    static void Main(string[] args)
    {
        string driverDir = @"C:\Path\To\Driver";
        string[] infFiles = Directory.GetFiles(driverDir, "*.inf", SearchOption.AllDirectories);

        foreach (string infFile in infFiles)
        {
            int reqSize = 0;
            bool success = SetupCopyOEMInf(
                infFile,
                driverDir,
                SPOST_PATH,
                0,
                null,
                0,
                out reqSize,
                null
            );

            if (success)
            {
                Console.WriteLine($"驱动添加成功:{Path.GetFileName(infFile)}");
            }
            else
            {
                int errorCode = Marshal.GetLastWin32Error();
                Console.WriteLine($"驱动添加失败:{Path.GetFileName(infFile)},错误码:{errorCode}");
            }
        }
    }
}

若要模拟pnputil /install的自动安装行为,可在驱动添加到商店后,调用UpdateDriverForPlugAndPlayDevicesAPI为目标设备匹配并安装驱动。

内容的提问来源于stack exchange,提问作者volody

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 08:13:22