Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
2
Indexable
Never
#include <Windows.h>
#include <iostream>
#include <NetCon.h>

int main() {
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr)) {
        std::cerr << "CoInitialize failed" << std::endl;
        return 1;
    }

    INetConnectionManager* pNetConnectionManager = NULL;
    hr = CoCreateInstance(CLSID_NetConnectionManager, NULL, CLSCTX_ALL, IID_INetConnectionManager, (LPVOID*)&pNetConnectionManager);
    if (FAILED(hr)) {
        std::cerr << "CoCreateInstance failed" << std::endl;
        CoUninitialize();
        return 1;
    }

    INetConnection* pNetConnection = NULL;
    hr = pNetConnectionManager->GetConnectivity(&pNetConnection);
    if (SUCCEEDED(hr) && pNetConnection != NULL) {
        NETCON_PROPERTIES* pProps = NULL;
        hr = pNetConnection->GetProperties(&pProps);
        if (SUCCEEDED(hr) && pProps != NULL) {
            std::wcout << L"Currently connected Ethernet network: " << pProps->pszwName << std::endl;
            CoTaskMemFree(pProps);
        }
        pNetConnection->Release();
    }

    pNetConnectionManager->Release();
    CoUninitialize();

    return 0;
}