寻找正在连接中的网络连接



寻找正在连接中的网络连接,并开启网络连接的网络连接共享功能。

注意:要设置为管理员权限启动工程。否则EnableSharing会失败。

#include <Windows.h>
#include <NetCon.h>
#include <locale>
#include <stdio.h>
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib,"Rpcrt4.lib")//GUID
//启用、禁用网卡
#pragma comment(lib,"ole32.lib")

int main(int argc, char* argv[])
{
    INetConnectionManager *pManager=NULL;
    INetConnection *pConnection=NULL;
    IEnumNetConnection *pEnum=NULL;
    ULONG           celtFetched;
    INetSharingManager *pNetSharingManager=NULL;
    INetConnectionProps *pProps=NULL;
    INetSharingConfiguration *pConfiguration=NULL;
    NETCON_PROPERTIES*   properties=NULL;
    NETCON_MEDIATYPE   mediatype;

    setlocale(LC_CTYPE, "");
    CoInitialize(NULL);
    CoCreateInstance(CLSID_ConnectionManager, NULL, CLSCTX_SERVER, IID_INetConnectionManager, (void**)&pManager);
    if(pManager == NULL)
    {
        printf("获取接受失败,Error:%d\n",GetLastError());
        return 0;
    }
    pManager->EnumConnections(NCME_DEFAULT, &pEnum);

    while(  pEnum->Next(1, &pConnection, &celtFetched) == S_OK   )
    {
        pConnection->GetProperties(&properties);

        if(properties->Status == NCS_CONNECTED)
        {
            if(properties->dwCharacter & NCCF_INCOMING_ONLY == 1)
            {
                wprintf(L"\"%S\"正处于连接状态,但是此连接无法共享!确保至少有2个网络连接!\n",properties->pszwName);
                break;
            }
            CoCreateInstance(CLSID_NetSharingManager, NULL, CLSCTX_SERVER, IID_INetSharingManager, (void**)&pNetSharingManager);
            if(pNetSharingManager == NULL)
            {
                printf("获取接受失败,Error:%d\n",GetLastError());
                break;
            }
            wprintf(L"发现\"%s\"正处于连接状态。尝试开启共享...\n",properties->pszwName);

            if(properties->MediaType >= NCM_DIRECT && properties->MediaType <=NCM_PPPOE)
            {
                pNetSharingManager->get_INetSharingConfigurationForINetConnection(pConnection,
                    &pConfiguration);
                if(pConfiguration && SUCCEEDED(pConfiguration->EnableSharing(ICSSHARINGTYPE_PUBLIC)))
                {
                    wprintf(L"成功设置\"%s\"为共享状态!\n",properties->pszwName);
                    break;
                }
            }
            wprintf(L"设置\"%s\"共享状态失败!Error:%d\n",properties->pszwName,GetLastError());
        }
    }


    if(pManager) pManager->Release();
    if(pConnection) pConnection->Release();
    if(pEnum) pEnum->Release();
    if(pNetSharingManager) pNetSharingManager->Release();
    if(pConfiguration) pConfiguration->Release();
    CoUninitialize();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifndef __REQUIRED_RPCNDR_H_VERSION__ #define __REQUIRED_RPCNDR_H_VERSION__ 440 #endif #include "rpc.h" #include "rpcndr.h" #ifndef __RPCNDR_H_VERSION__ #error this stub requires an updated version of #endif // __RPCNDR_H_VERSION__ #ifndef COM_NO_WINDOWS_H #include "windows.h" #include "ole2.h" #endif /*COM_NO_WINDOWS_H*/ #ifndef __netcon_h__ #define __netcon_h__ /* Forward Declarations */ #ifndef __IEnumNetConnection_FWD_DEFINED__ #define __IEnumNetConnection_FWD_DEFINED__ typedef interface IEnumNetConnection IEnumNetConnection; #endif /* __IEnumNetConnection_FWD_DEFINED__ */ #ifndef __INetConnection_FWD_DEFINED__ #define __INetConnection_FWD_DEFINED__ typedef interface INetConnection INetConnection; #endif /* __INetConnection_FWD_DEFINED__ */ #ifndef __INetConnectionManager_FWD_DEFINED__ #define __INetConnectionManager_FWD_DEFINED__ typedef interface INetConnectionManager INetConnectionManager; #endif /* __INetConnectionManager_FWD_DEFINED__ */ #ifndef __INetConnectionConnectUi_FWD_DEFINED__ #define __INetConnectionConnectUi_FWD_DEFINED__ typedef interface INetConnectionConnectUi INetConnectionConnectUi; #endif /* __INetConnectionConnectUi_FWD_DEFINED__ */ #ifndef __INetConnectionPropertyUi_FWD_DEFINED__ #define __INetConnectionPropertyUi_FWD_DEFINED__ typedef interface INetConnectionPropertyUi INetConnectionPropertyUi; #endif /* __INetConnectionPropertyUi_FWD_DEFINED__ */ #ifndef __INetConnectionCommonUi_FWD_DEFINED__ #define __INetConnectionCommonUi_FWD_DEFINED__ typedef interface INetConnectionCommonUi INetConnectionCommonUi;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值