3

我想以编程方式安装 MS 环回适配器以自动通过 SSH 建立 SMB 隧道

我在网上找到的所有代码都使用了 MS devcon 实用程序,该实用程序不可再分发(参见http://support.microsoft.com/kb/311272/en-us)。示例用法(更多示例):

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

除了可分配性问题之外,理想情况下,我希望对生成的设备名称进行一些控制,尽管这可以通过枚举之前和之后的网络适配器并寻找新的 MS 环回设备来解决。这有点过分,虽然我想我可以忍受它。我的想法是调整其中的一些代码

我目前正在研究来自 WDK 的 devcon 源代码,以通过 SetupAPI/CfgMgr32 添加环回适配器,正如上面链接的 MS KB 文章所建议的那样。有没有更简单/可编写脚本的方法?

如果没有,是否有人为该 SetupAPI/CfgMgr32 路由提供了一些相对简单的示例代码?

4

3 回答 3

4

我想在不编写任何新 exe 的情况下实现相同的目标,并发现它可以使用 cscript 以及 devcon 和 netsh 工具来完成。创建适配器似乎无法控制它的调用方式,因此您必须在创建它之后使用 WMI 接口进行枚举。不幸的是,netsh 行为取决于您使用的 Windows 版本,但将以下内容放入名为 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)
于 2012-10-18T15:15:46.797 回答
2

请检查以下线程:

如何在 Win32 上使用 C 安装硬件驱动程序

于 2011-02-04T02:36:38.177 回答
2

希望我参加聚会不会太晚 - 可以在没有 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;
    }
}

以上将适用于 x64 操作系统。为了让它在 x86 操作系统上工作,在结构中更改Pack = 8Pack = 1SP_DEVINFO_DATA

于 2016-06-26T07:52:53.073 回答