Install Microsoft loopback software switch from VBS (or C # / VB)

I would like to programmatically install an MS loopback adapter to automate SMB tunneling through SSH .

All the code I found on the network uses the MS devcon utility, which is not redistributable (see http://support.microsoft.com/kb/311272/en-us ). Usage example (additional examples):

devcon -r install %WINDIR%\Inf\Netloop.inf *MSLOOP

Besides the distribution problem, ideally I would like to have some control over the resulting device name, although this could be fixed by listing the network adapters before and after and searching for a new loopback device. It's a bit cool, although I think I can handle it. My idea is to adapt some of this code .

I am currently studying the devcon source code from the WDK to add a loopback adapter through SetupAPI / CfgMgr32, as stated in the MS KB article above. Is there an easier way / script?

If not, does anyone have a relatively simple code example for this SetupAPI / CfgMgr32 route?

+3
source share
3 answers

, exe , , cscript devcon netsh. , , , , WMI . , netsh , , create-loopback.vbs, XP 2008.

  Dim strLastLoopbackAdapterName, loopbackAdapterName 

  If wscript.arguments.count < 3 then
    WScript.Echo "usage: create-loopback.vbs loopbackAdapterName loopbackIpAddress loopbackSubNetMask "
    WScript.Quit
  end If
  loopbackAdapterName = wscript.arguments(0)
  loopbackIpAddress = wscript.arguments(1)
  loopbackSubNetMask = wscript.arguments(2)

  Wscript.Echo "Creating loopback called " &loopbackAdapterName &" on " &loopbackIpAddress &" with mask " &loopbackSubNetMask

  Set objShell = CreateObject("WScript.Shell") 
  Wscript.Echo "Installing loopback adapter..."

  objShell.Run "cmd /c devcon install %windir%\inf\netloop.inf *MSLOOP", 0, True 

  Wscript.Echo "Waiting for drivers to update..."
  Wscript.sleep 10000 'Allow 10s for install' 

  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  Set colItems = objWMIService.ExecQuery("SELECT NetConnectionID FROM Win32_NetworkAdapter WHERE Name='Microsoft Loopback Adapter'", "WQL", 48)
  For Each objItem In colItems
     strLastLoopbackAdapterName = objItem.NetConnectionID
  Next

  Wscript.Echo "Last Loopback Connection is " & strLastLoopbackAdapterName  

  Wscript.Echo "Renaming new loopback..."
  objShell.Run "netsh interface set interface name = " &Chr(34) &strLastLoopbackAdapterName &Chr(34) &" newname = " &Chr(34) &loopbackAdapterName &Chr(34), 0, True 
  Wscript.Echo "Configuring loopback..."
  objShell.run "netsh interface ip set address name=" &Chr(34) &loopbackAdapterName &Chr(34) &" source=static " &loopbackIpAddress &" " &loopbackSubNetMask, 0, True 
  Wscript.Echo "Done"
  WScript.Quit(0)
+4
+2

, - devcon/vbs ( pinvoke):

loopback, "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;
    }
}

The above will work with x64. in order for it to work on x86 OS, change Pack = 8to Pack = 1in SP_DEVINFO_DATAstruct

+1
source

Source: https://habr.com/ru/post/1785861/


All Articles