从 VBS(或 C#/VB)以编程方式安装 Microsoft 环回适配器

发布于 2024-10-12 12:10:38 字数 786 浏览 6 评论 0原文

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

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

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

除了可分发性问题之外,理想情况下我希望对生成的设备名称进行一些控制,尽管这可以通过枚举之前和之后的网络适配器并查找新的 MS 环回设备来解决。这有点过分了,不过我想我可以忍受。我的想法是修改一些此代码< /a>.

我目前正在研究 WDK 中的 devcon 源代码,以便按照上面链接的 MS 知识库文章的建议通过 SetupAPI/CfgMgr32 添加环回适配器。有没有更简单/可编写脚本的方法?

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

I'd like to programmatically install the MS loopback adapter to automate tunneling of SMB over SSH.

All the code I found on the net uses the MS devcon utility, which is not redistributable (cf. http://support.microsoft.com/kb/311272/en-us). Example usage (more examples):

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

Apart from the distributability issue, ideally i'd like to have some control over the resulting device name, though that could be fixed by enumerating the network adapters before and after and looking for the new MS loopback device. That's a bit racy, though I think I could live with it. My idea is to adapt some of this code.

I'm currently looking into the devcon source code from the WDK to add the loopback adapter via SetupAPI/CfgMgr32 as the MS KB article linked above suggests. Is there any easier/scriptable way?

If there is none, does anyone have some relatively simple sample code for that SetupAPI/CfgMgr32 route?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不服输 2024-10-19 12:10:38

我想在不编写任何新的 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)

I wanted to achieve the same thing without writing any new exe to do it and found it can be done with cscript and the devcon and netsh tools. creating the adapter seems to give you no control over what it will be called, so you have to enumerate using the WMI interface after you have created it. Unfortunately netsh behaviour depends on which version of windows you're on, but bung the following in a file called create-loopback.vbs and it will work on XP and 2008 server.

  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)
她比我温柔 2024-10-19 12:10:38

希望我参加聚会还不算太晚 - 无需 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 操作系统上工作,请将 SP_DEVINFO_DATA 结构中的 Pack = 8 更改为 Pack = 1

Hope i'm not too late for the party - doing it without devcon/vbs is possible (though many native pinvoke calls are required):

In order to install the loopback adapter, call the following method with the parameters "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 on x64 OSs. in order for it to work on x86 OSs, change Pack = 8 to Pack = 1 in the SP_DEVINFO_DATA struct

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文