【问题标题】:Can DevCon notify when a driver is finished installing after a rescan?重新扫描后驱动程序安装完成时,DevCon 可以通知吗?
【发布时间】:2012-08-03 19:27:00
【问题描述】:

我正在尝试在 Windows 安装项目期间安装驱动程序。

我要做的第一步是复制 INF 文件并预安装驱动程序。

SetupCopyOEMInf(infFile, null, 1, 0, null, 0, 0, null);

这会正确预安装驱动程序,但在设备管理器中完成硬件重新扫描之前,设备无法使用。我也想自动化这个。我试过使用the setupapi.dll to invoke the hardware rescan,但对我来说并不总是成功的。使用 devcon.exe rescan 总是强制重新扫描硬件,但它是一个同步命令,它在设备完成安装之前返回。硬件扫描完成,驱动安装成功后,有什么办法可以得到返回结果?

谢谢,

米莎

编辑

这是我的工作代码:

    public const UInt32 CR_SUCCESS = 0;
    public const UInt64 CM_REENUMERATE_SYNCHRONOUS = 1;
    public const UInt64 CM_LOCATE_DEVNODE_NORMAL = 0;

    [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("cfgmgr32.dll")]
    public static extern int CM_Locate_DevNode_Ex(ref IntPtr deviceHandle, int deviceId, uint flags, IntPtr machineHandle);

    [DllImport("cfgmgr32.dll")]
    public static extern int CM_Reenumerate_DevNode_Ex(IntPtr devInst, UInt64 flags);

    [DllImport("cfgmgr32.dll")]
    public static extern int CMP_WaitNoPendingInstallEvents(UInt32 timeOut);

    static void Main() {
      bool success = SetupCopyOEMInf(infFile, null, 1, 0, null, 0, 0, null);

      if(!success) {
        throw new Exception("Error installing driver");
      }

      success = RescanAllDevices();

      if (!success) {
        throw new Exception("Error installing driver");
      }
    }

    public static bool RescanAllDevices() {
      int ResultCode = 0;
      IntPtr LocalMachineInstance = IntPtr.Zero;
      IntPtr DeviceInstance = IntPtr.Zero;
      UInt32 PendingTime = 30000;

      ResultCode = CM_Locate_DevNode_Ex(ref DeviceInstance, 0, 0, LocalMachineInstance);
      if (CR_SUCCESS == ResultCode) {
        ResultCode = CM_Reenumerate_DevNode_Ex(DeviceInstance, CM_REENUMERATE_SYNCHRONOUS);
        ResultCode = CMP_WaitNoPendingInstallEvents(PendingTime);
      }
      return ResultCode == CR_SUCCESS;
    }

【问题讨论】:

  • 感谢代码!但是,如果之前驱动程序安装失败并且安装了虚拟驱动程序,则只有重新启动系统才能安装新驱动程序。因此,重新启动似乎是最安全的方法。见stackoverflow.com/questions/14937261/…

标签: installation driver wdk inf device-manager


【解决方案1】:

devcon 的源代码在 WDK 中可用。它位于 src\setup\devcon 目录中。 rescan 命令的逻辑在 cmds.cpp 中的 cmdRescan 函数中。将该逻辑复制到您自己的代码中并确保它不会立即返回是一件简单的事情。

【讨论】:

  • Devcon 没有解决这个问题:它使用异步重新扫描
  • @ColinJensen 除了第一句话之外,您没有阅读我的答案,是吗?我说源是可用的,可以很容易地修改为同步的。
猜你喜欢
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 2019-10-22
  • 2016-04-25
  • 1970-01-01
  • 2023-04-06
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多