当前位置:网站首页>The Internet share option of network card is set automatically by program

The Internet share option of network card is set automatically by program

2021-01-23 20:07:07 Mike_ Zhang

operating system : Windows 10_x64 [ edition 10.0.19042.685]

Windows You can access the Internet through network card sharing , But it needs to be set in the properties of the network card , Need to operate in the windows interface , It can't be automated .

 

 

Check the following information , Can pass INetSharingManager To achieve , The principle is simple : Simulation of manual action, automatic operation .

The sample code is as follows :

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;
}

Add relevant code and use VS2015 compile :

 

  Requires administrator rights to run ,Release The directory has executable programs

 

Usage method :

ConsoleApplication1.exe  The name of the network card that can access the Internet   The name of the network card that needs to access the Internet 

such as :

ConsoleApplication1.exe WLAN " Ethernet  4"

Of course, you can also use batch files , But we need to pay attention to the use of file coding GBK code .

 

  This article deals with the code and precompiler download address :https://pan.baidu.com/s/1sRKwYDP_gFMegeK5rTP3rg

WeChat official account ( Talk about blog posts ) Post reply 2021012301 Get extraction code .

版权声明
本文为[Mike_ Zhang]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123200601082t.html

随机推荐