问题
I want to install a third-party libusb driver during a Windows Setup installation. I created this installation with Visual Studio 2010.
I have tried installing this driver through the command line using SetupAPI and DifXAPI, but nothing happens. I expect a window to popup stating that it is an unsigned driver, and you have to click OK to continue. The only way I have got this window to popup is if I used a C# console application and P/Invoke to call the driver install code from DifXApi (pointing to the INF file which looks like it was generated by inf-wizard), and the project needed to be built for x64 (I need this to also work for 32-bit installers). After clicking OK the driver never installed.
The only way this driver correctly installs is if I plug in the hardware via USB, right click the unknown device, and browse to the folder containing the driver DLL file's, sys files, and INF file. How does Windows figure out how to install the driver?
The INF file has driver sections for 32 bit/64 bit/Itanium, but how does Windows know which section to install, and what is Windows doing differently that I am in command line?
回答1:
I am able to install the drivers on 32-bit and 64-bit Windows with the following code, where infPath
is the path to the INF file, and devices
is a list of all the device id's associated with the USB device:
[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);
}
}
}
来源:https://stackoverflow.com/questions/11659223/install-the-libusb-driver-using-the-inf-file-with-c-sharp