7

通过程序自动设置网卡的“internet共享”选项

 3 years ago
source link: http://www.cnblogs.com/MikeZhang/p/windowsICS20210123.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

操作系统 : Windows 10_x64 [版本 10.0.19042.685]

Windows下可以通过网卡共享进行上网,但是需要在网卡的属性里面进行设置,需要在视窗界面进行操作,不能实现自动化。

AR7nQfY.png!mobile

查了下资料,可以通过INetSharingManager来实现,原理很简单: 模拟手工的动作自动化操作。

示例代码如下:

HRESULT shareNet(INetSharingManager* pNSM, const char* srcName, const char* dstName)
{
    INetConnection * pNC = NULL;
    INetSharingConfiguration * pNSC = NULL;
    IEnumVARIANT * pEV = NULL;
    IUnknown * pUnk = NULL;
    INetSharingEveryConnectionCollection * pNSECC = NULL;

    HRESULT hr = pNSM->get_EnumEveryConnection(&pNSECC);
    VARIANT v;
    VariantInit(&v);

    if (!pNSECC)
    {
        wprintf(L"failed to get EveryConnectionCollection!\r\n");
        return NULL;
    }

    hr = pNSECC->get__NewEnum(&pUnk);
    if (pUnk)
    {
        hr = pUnk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&pEV);
        pUnk->Release();
    }


    wprintf(L"----------------------------------------------------------\n");
    while (S_OK == pEV->Next(1, &v, NULL))
    {
        if (V_VT(&v) == VT_UNKNOWN)
        {
            V_UNKNOWN(&v)->QueryInterface(__uuidof(INetConnection), (void**)&pNC);
            if (pNC)
            {
                NETCON_PROPERTIES* pNP = NULL;
                pNC->GetProperties(&pNP);
                setlocale(LC_ALL, "chs");

                wprintf(L"pszwName--%s\n", pNP->pszwName);
                wprintf(L"pszwDeviceName--%s\n", pNP->pszwDeviceName);
                wprintf(L"Status--%d\n", pNP->Status);
                wprintf(L"\n");

                //continue;
                if (pNP->Status != NCS_CONNECTED) continue;
                std::string tmpName = CW2A(pNP->pszwName);
                //printf("###### |%s| : |%s|\r\n", tmpName.c_str(),(char*)nicName);

                hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
                if (!strcmp(tmpName.c_str(), (char*)srcName))
                {
                    printf("**************find nic srcName : %s\r\n", (char*)srcName);
                    hr = pNSC->DisableSharing();
                    Sleep(500);
                    //getchar();
                    hr = pNSC->EnableSharing(ICSSHARINGTYPE_PUBLIC);
                }
                pNSC->Release();

                hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
                if (!strcmp(tmpName.c_str(), (char*)dstName))
                {
                    printf("**************find nic dstName : %s\r\n", (char*)dstName);
                    hr = pNSC->DisableSharing();
                    Sleep(500);
                    hr = pNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE);
                }
                pNSC->Release();
            }
        }
    }
    return hr;
}

添加相关代码并使用VS2015编译:

BvyIJbQ.png!mobile

需要管理员权限运行,Release目录有可执行程序

BrQ36fQ.png!mobile

使用方法:

ConsoleApplication1.exe 能上网的网卡名称 需要上网的网卡名称

比如:

ConsoleApplication1.exe WLAN "以太网 4"

当然也可以使用批处理文件,但需要注意下文件编码使用GBK 编码。

FfUfQze.png!mobile

本文涉代码及预编译程序下载地址: https://pan.baidu.com/s/1sRKwYDP_gFMegeK5rTP3rg

可关注微信公众号(聊聊博文)后回复 2021012301 获取提取码。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK