驱动程序在重新扫描完成后安装时可以通知DevCon吗?
问题描述:
我想在Windows安装项目中安装驱动程序。驱动程序在重新扫描完成后安装时可以通知DevCon吗?
我做的第一步是复制INF文件并预安装驱动程序。
SetupCopyOEMInf(infFile, null, 1, 0, null, 0, 0, null);
这正确地预装了驱动程序,但设备未准备使用,直到重新扫描硬件在设备管理器中完成的。我也想自动化这个。我曾尝试使用the setupapi.dll to invoke the hardware rescan,但它并不总是对我成功。使用devcon.exe重新扫描始终会强制重新扫描硬件,但它是一个同步命令,并且在设备完成安装之前它会返回。硬件扫描完成并且驱动程序安装成功后,有什么方法可以获得返回结果吗?
感谢,
米莎
编辑
这是我工作的代码:
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;
}
答
用于DEVCON的源代码可以在WDK。它位于src \ setup \ devcon目录中。重新扫描命令的逻辑位于cmds.cpp中的cmdRescan函数中。将该逻辑复制到自己的代码并确保它不会立即返回将是一件简单的事情。
+0
谢谢凯里! – Misha 2012-08-13 16:08:17
感谢您的代码!但是,如果驱动程序安装先前失败并且安装了虚拟驱动程序,则只有重新启动系统才会安装新的驱动程序。所以,重启似乎是最安全的方法。见http://stackoverflow.com/questions/14937261/equivalent-of-scan-for-hardware-changes-in-device-manager-doesnt-work-after-f – OneWorld 2013-02-19 07:30:00