首页 文章

使用带有C#的INF文件安装libusb驱动程序

提问于
浏览
3

我想在Windows安装过程中安装第三方libusb驱动程序 . 我使用Visual Studio 2010创建了此安装 .

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

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

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

1 回答

  • 1

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

    [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);
        }
      }
    }
    

相关问题