Example #1
0
// 
// The ICS manager initialization takes 3 steps --
// 1. Make sure that ICS is supported in the current version of Windows
// 2. Retrieve all the ICS connections
// 3. Initialize each connection
//
HRESULT
CIcsManager::InitIcsManager
(
)
{
    HRESULT                                 hr                  =   S_OK;
    IUnknown*                               pUnkEnum            =   nullptr;
    IEnumNetSharingEveryConnection*         pNSEConn            =   nullptr;
    INetSharingEveryConnectionCollection*   pConnectionsList    =   nullptr;
    INetConnection*                         pNetConnection      =   nullptr;
    LONG                                    lIndex              =   0;
    VARIANT_BOOL                            bSharingEnabled     =   VARIANT_FALSE;
    VARIANT                                 varItem;
    ULONG                                   pceltFetched;

    VariantInit(&varItem);

    if (m_pNSMgr)
    {
        hr = E_UNEXPECTED;
        BAIL_ON_HRESULT_ERROR(hr);
    }

    hr =
    CoCreateInstance
    (
        __uuidof(NetSharingManager),
        nullptr,
        CLSCTX_ALL,
        __uuidof(INetSharingManager),
        reinterpret_cast<void**>(&m_pNSMgr)
    );
    BAIL_ON_HRESULT_ERROR(hr);
    ASSERT(m_pNSMgr);


    hr =
    RefreshInstalled
    (
    );
    BAIL_ON_HRESULT_ERROR(hr);

    if (!m_bInstalled)
    {
        hr = E_UNEXPECTED;
        BAIL_ON_HRESULT_ERROR(hr);
    }

    hr =
    m_pNSMgr->get_EnumEveryConnection
    (
        &pConnectionsList
    );
    BAIL_ON_HRESULT_ERROR(hr);
    ASSERT(pConnectionsList);

    hr =
    pConnectionsList->get__NewEnum
    (
        &pUnkEnum
    );
    BAIL_ON_HRESULT_ERROR(hr);
    ASSERT(pUnkEnum);

    hr =
    pUnkEnum->QueryInterface
    (
        __uuidof(IEnumNetSharingEveryConnection),
        reinterpret_cast<void**>(&pNSEConn)
    );
    BAIL_ON_HRESULT_ERROR(hr);
    ASSERT(pNSEConn);

    m_lNumConns = 0;

    while (1)
    {
        VariantClear(&varItem);

        hr =
        pNSEConn->Next
        (
            1,
            &varItem,
            &pceltFetched    
        );
        BAIL_ON_HRESULT_ERROR(hr);

        if (S_FALSE == hr)
        {
            hr = S_OK;
            break;
        }

        m_lNumConns++;

        ASSERT( (V_VT(&varItem) == VT_UNKNOWN) && V_UNKNOWN(&varItem) );
    }

    hr = pNSEConn->Reset( );
    BAIL_ON_HRESULT_ERROR(hr);


    if (0 == m_lNumConns)
    {
        BAIL( );
    }

    m_pList = new(std::nothrow) CIcsConnection [m_lNumConns];
    if (!m_pList)
    {
        hr = E_OUTOFMEMORY;
        BAIL_ON_HRESULT_ERROR(hr);
    }


    lIndex = 0;
    while ( 1 )
    {
        SAFE_RELEASE(pNetConnection);
        VariantClear(&varItem);

        hr =
        pNSEConn->Next
        (
            1,
            &varItem,
            &pceltFetched
        );
        BAIL_ON_HRESULT_ERROR(hr);

        if (S_FALSE == hr)
        {
            hr = S_OK;
            break;
        }

        lIndex++;
        if (lIndex > m_lNumConns)
        {
            hr = E_UNEXPECTED;
            BAIL_ON_HRESULT_ERROR(hr);
        }


        ASSERT( (V_VT(&varItem) == VT_UNKNOWN) && V_UNKNOWN(&varItem) );

        hr =
        V_UNKNOWN(&varItem)->QueryInterface
        (
            __uuidof(INetConnection),
            reinterpret_cast<void**>(&pNetConnection)
        );
        BAIL_ON_HRESULT_ERROR(hr);
        ASSERT(pNetConnection);

        hr =
        m_pList[lIndex-1].InitIcsConnection
        (
            this,
            pNetConnection,
            lIndex-1
        );
        BAIL_ON_HRESULT_ERROR(hr);

    }

    ASSERT(lIndex == m_lNumConns);

error:

    SAFE_RELEASE(pNetConnection);
    VariantClear(&varItem);
    SAFE_RELEASE(pNSEConn);
    SAFE_RELEASE(pUnkEnum);
    SAFE_RELEASE(pConnectionsList);

    return hr;
}
Example #2
0
HRESULT DoTheWork (INetSharingManager * pNSM)
{   // add a port mapping to every firewalled or shared connection

    INetSharingEveryConnectionCollection * pNSECC = NULL;
    HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
    int LastErrorCode = 0;
    if (!pNSECC)
        return ICS_Error_FailGetEvery;
    else {

        // enumerate connections
        IEnumVARIANT * pEV = NULL;
        IUnknown * pUnk = NULL;
        hr = pNSECC->get__NewEnum (&pUnk);
        if (pUnk) {
            hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
                                       (void**)&pEV);
            pUnk->Release();
        }else{
            return ICS_Error_FailGetNewEnum;
        }
        if (pEV) {
            VARIANT v;
            VariantInit (&v);
            while (S_OK == pEV->Next (1, &v, NULL)) {
                if (V_VT (&v) == VT_UNKNOWN) {
                    INetConnection * pNC = NULL;
                    V_UNKNOWN (&v)->QueryInterface
                            (__uuidof(INetConnection),
                             (void**)&pNC);
                    if (pNC) {
                        INetConnectionProps * pNCP = NULL;
                        pNSM->get_NetConnectionProps (pNC, &pNCP);
                        if (!pNCP)
                            wprintf (L"failed to get NetConnectionProps!\r\n");
                                     else {
                                         // check properties for firewalled or shared connection
                                         NETCON_MEDIATYPE MediaType;
                                         pNCP->get_MediaType(&MediaType);
                                         NETCON_STATUS Status;
                                         pNCP->get_Status(&Status);
                                         BSTR DevName;
                                         pNCP->get_DeviceName(&DevName);

                                         if (MediaType & (NCM_LAN | NCM_SHAREDACCESSHOST_LAN | NCM_PHONE)
                                                 && Status == NCS_CONNECTED
                                                 && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("hosted network", 0, Qt::CaseInsensitive)==-1
                                                 && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("virtual", 0, Qt::CaseInsensitive)==-1
                                                 && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("teamviewer", 0, Qt::CaseInsensitive)==-1) {
                                                 // got a shared/firewalled connection
                                                 INetSharingConfiguration * pNSC = NULL;
                                                 hr = pNSM->get_INetSharingConfigurationForINetConnection (pNC, &pNSC);
                                                 if (!pNSC)
                                                 wprintf (L"can't make INetSharingConfiguration object!\r\n");
                                                 else {
                                                     hr = pNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE);
                                                     if(hr!=S_OK){
                                                         LastErrorCode = ICS_Error_EnableSharing;
                                                     }else{
                                                         BSTR Name;
                                                         pNCP->get_Name(&Name);
                                                         QMessageBox msg;
                                                         msg.setText(QString("Network: %1 %2 %3").arg(_com_util::ConvertBSTRToString(Name)).arg(_com_util::ConvertBSTRToString(DevName)).arg(Status));
                                                         msg.exec();
                                                         return 0;
                                                     }
                                                     pNSC->Release();
                                                 }
                                         }
                                         pNCP->Release();
                                     }
                                     pNC->Release();
                    }
                }
                VariantClear (&v);
            }
            pEV->Release();
        }else{
            return ICS_Error_FailGetEnumVariant;
        }
        pNSECC->Release();
    }
    if(LastErrorCode!=0) return LastErrorCode;
    return hr;
}