Надеюсь, я еще не опоздал на вечеринку - это возможно без devcon / vbs (хотя для этого требуется много родных вызовов pinvoke):
Чтобы установить адаптер обратной связи, вызовите следующий методс параметрами "C:\Windows\Inf\netloop.inf"
, *MSLOOP
:
class Devcon
{
//https://msdn.microsoft.com/en-us/magazine/dd419661.aspx?f=255&MSPPError=-2147217396#id0070035
[HandleProcessCorruptedStateExceptions]
static bool InstallDriver(string inf, string hwid)
{
StringBuilder className = new StringBuilder(MAX_CLASS_NAME_LEN);
Guid ClassGUID = Guid.Empty;
if (!SetupDiGetINFClass(inf, ref ClassGUID, className, MAX_CLASS_NAME_LEN, 0))
return false;
IntPtr DeviceInfoSet = SetupDiCreateDeviceInfoList(ref ClassGUID, IntPtr.Zero);
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
if (!SetupDiCreateDeviceInfo(DeviceInfoSet, className.ToString(), ref ClassGUID, null, IntPtr.Zero, DICD_GENERATE_ID, DeviceInfoData))
return false;
if (!SetupDiSetDeviceRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_HARDWAREID, hwid, hwid.Length))
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return false;
}
if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, DeviceInfoSet, DeviceInfoData))
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return false;
}
// http://stackoverflow.com/questions/11474317/updatedriverforplugandplaydevices-error-is-telling-me-im-not-doing-something
try
{
bool reboot = false;
if (!UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, hwid, inf, 0, reboot))
{
SetupDiCallClassInstaller(DIF_REMOVE, DeviceInfoSet, DeviceInfoData);
return false;
}
}
catch (AccessViolationException) { }
return true;
}
// Consts
const int MAX_CLASS_NAME_LEN = 32;
const int SPDRP_HARDWAREID = 0x00000001;
const int DICD_GENERATE_ID = 0x00000001;
const int DIF_REGISTERDEVICE = 0x00000019;
const int DIF_REMOVE = 0x00000005;
// Pinvokes
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiGetINFClass(string infName, ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPStr)] StringBuilder ClassName, int ClassNameSize, int RequiredSize);
[DllImport("setupapi.dll", SetLastError = true)]
static extern IntPtr SetupDiCreateDeviceInfoList(ref Guid ClassGuid, IntPtr hwndParent);
[DllImport("Setupapi.dll", SetLastError = true)]
static extern bool SetupDiCreateDeviceInfo(IntPtr DeviceInfoSet, String DeviceName, ref Guid ClassGuid, string DeviceDescription, IntPtr hwndParent, Int32 CreationFlags, SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiSetDeviceRegistryProperty(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, uint Property, string PropertyBuffer, int PropertyBufferSize);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData);
[DllImport("newdev.dll", SetLastError = true)]
static extern bool UpdateDriverForPlugAndPlayDevices(IntPtr hwndParent, string HardwareId, string FullInfPath, int InstallFlags, bool bRebootRequired);
// Structs
[StructLayout(LayoutKind.Sequential, Pack = 8)]
class SP_DEVINFO_DATA
{
internal int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
[MarshalAs(UnmanagedType.Struct)]
internal Guid classGuid = Guid.Empty; // temp
internal int devInst = 0; // dumy
internal long reserved = 0;
}
}
Вышеописанное будет работать на 64-разрядных ОС.чтобы он работал на x86 OS, измените Pack = 8
на Pack = 1
в SP_DEVINFO_DATA
struct