【问题标题】:Install the libusb driver using the INF file with C#使用 C# 的 INF 文件安装 libusb 驱动程序
【发布时间】:2012-07-25 21:53:47
【问题描述】:

我想在 Windows 安装程序安装期间安装第三方 libusb 驱动程序。我使用 Visual Studio 2010 创建了这个安装。

我尝试使用 SetupAPI 和 DifXAPI 通过命令行安装此驱动程序,但没有任何反应。我希望会弹出一个窗口,说明它是未签名的驱动程序,您必须单击“确定”才能继续。弹出此窗口的唯一方法是,如果我使用 C# 控制台应用程序和 P/Invoke 从 DifXApi 调用驱动程序安装代码(指向看起来像是由 inf-wizard 生成的 INF 文件),并且需要为 x64 构建项目(我需要它也适用于 32 位安装程序)。单击确定后,驱动程序从未安装。

正确安装此驱动程序的唯一方法是,如果我通过 USB 插入硬件,右键单击未知设备,然后浏览到包含驱动程序 DLL 文件、sys 文件和 INF 文件的文件夹。 Windows 如何知道如何安装驱动程序?

INF 文件包含 32 位/64 位/Itanium 的驱动程序部分,但 Windows 如何知道要安装哪个部分,以及 Windows 与我在命令行中的操作有何不同?

【问题讨论】:

    标签: c# installation driver libusb inf


    【解决方案1】:

    我可以使用以下代码在 32 位和 64 位 Windows 上安装驱动程序,其中 infPath 是 INF 文件的路径,devices 是所有设备 ID 的列表USB 设备:

    [DllImport("setupapi.dll")]
    public static extern bool SetupCopyOEMInf(
        string SourceInfFileName,
        string OEMSourceMediaLocation,
        int OEMSourceMediaType,
        int CopyStyle,
        string DestinationInfFileName,
        int DestinationInfFileNameSize,
        int RequiredSize,
        string DestinationInfFileNameComponent
        );
    
    [DllImport("newdev.dll")]
    public static extern bool UpdateDriverForPlugAndPlayDevices(
        IntPtr hwndParent,
        string HardwareId,
        string FullInfPath,
        uint InstallFlags,
        bool bRebootRequired
        );
    
    [STAThread]
    static void Main() {
      if (SetupCopyOEMInf(infPath, null, 0, 0, null, 0, 0, null)) {
        foreach (string device in devices) {
          UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, device, infPath, 0, false);
        }
      }
    }
    

    【讨论】:

    • 你把这段代码放在你安装包的什么地方?它是 MSI 吗?
    • 嘿,很抱歉,我已经有一段时间没有查看此代码或使用它了……但我是的,这是一个 Windows 安装项目,我在其中创建了一个 MSI。我相信此代码位于 Installer 类之一中,并且通过在自定义操作中调用此安装程序来调用代码。
    猜你喜欢
    • 2023-03-16
    • 2014-04-25
    • 1970-01-01
    • 2018-02-17
    • 2011-01-03
    • 2014-03-10
    • 2017-09-06
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多