【发布时间】: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