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);
}
}
}
Best Answer
Try something like this:
Then to call it:
I generated most of this signature using the P/Invoke Signature Generator.
The full details of this method and its parameters are on MSDN. According to MSDN the first parameter can be null, the second one must be null, and the last parameter must be 0. You only have to pass in the string parameter.