zoukankan      html  css  js  c++  java
  • 寻找正在连接中的网络连接

    

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

    注意:要设置为管理员权限启动project。

    否则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
    ",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个网络连接。
    ",properties->pszwName);
                    break;
                }
                CoCreateInstance(CLSID_NetSharingManager, NULL, CLSCTX_SERVER, IID_INetSharingManager, (void**)&pNetSharingManager);
                if(pNetSharingManager == NULL)
                {
                    printf("获取接受失败,Error:%d
    ",GetLastError());
                    break;
                }
                wprintf(L"发现"%s"正处于连接状态。尝试开启共享...
    ",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"为共享状态!
    ",properties->pszwName);
                        break;
                    }
                }
                wprintf(L"设置"%s"共享状态失败!Error:%d
    ",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;
    }
    

  • 相关阅读:
    Android中实现定时器的三种方法 分类: Android 2015-07-14 18:04 11人阅读 评论(0) 收藏
    java构造器内部多态方法
    java继承方法覆盖
    java对象实例化 静态块,对象块,构造函数执行顺序
    Linux 的系统运行级别
    Jmeter启动jmeter-server.bat 报java.io.FileNotFoundException:rmi_keystore.jks 解决方法
    jmeter中JSON Extractors使用
    CentOS6.5下安装jenkins
    day12接口自动化测试框架
    day10 python接口开发、mock接口、网络编程
  • 原文地址:https://www.cnblogs.com/llguanli/p/7236053.html
Copyright © 2011-2022 走看看