title: Installing a specific driver for a specific device programmatically (and when pre-install fails)
body: <p>I have a signed driver and I need to install it programmatically for a specific USB device using C# (targeting Vista and 7 64-bit). </p>
  
  <p>I found very easy the use of the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff544817%28v=vs.85%29.aspx" rel="nofollow">DriverPackagePreinstall</a> function within the DIFx API (<a href="http://pinvoke.net/default.aspx/difxapi/DriverPackagePreinstall.html" rel="nofollow">here</a> is how to P/Invoke it): by calling it before the device is plugged into the USB, Windows is able to associate the driver to the device once this is detected.</p>
  
  <p>The problems arise when the device is plugged into the USB without having pre-installed the driver. It can happen that another driver is installed for that device (in one test PC I found that a generic driver has been installed and that the device is listed under "Other devices" in Device Manager).</p>
  
  <p>Trying to solve this problem, up to now I've been able to detect that the device is there by using the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff551069%28v=vs.85%29.aspx" rel="nofollow">SetupDiGetClassDevs</a>, the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff551010%28v=vs.85%29.aspx" rel="nofollow">SetupDiEnumDeviceInfo</a> and the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff551967%28v=vs.85%29.aspx" rel="nofollow">SetupDiGetDeviceRegistryProperty</a> functions in order to identify the device by its VID and PID (vendor and product identifier). Now that I have the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff552344%28v=vs.85%29.aspx" rel="nofollow">SP_DEVINFO_DATA</a> for my device I guess I've to call the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff547654%28v=vs.85%29.aspx" rel="nofollow">InstallSelectedDriver</a> function, isn't it? If yes, could you please tell me how to P/Invoke that function (I cannot find it on pinvoke.net) and how to use it by specifying the driver I want to install for my device (supposing I've the INF path)?</p>