Страницы

Поиск по вопросам

среда, 22 января 2020 г.

Имя интернет сети с++

#cpp #visual_studio #winapi #сеть #cpp17


Как узнать имя интернет сети к которой подключен комп. Подключен ли интернет проверяю так:

#include 
typedef BOOL(WINAPI* INETCHECKPROC) (LPDWORD lpdwFlags, DWORD dwReserved);
bool GetStatusConnect()
{
    bool bRetVal;
    HINSTANCE hLib = NULL;
    INETCHECKPROC pfnInternetGetConnectedState;
    hLib = LoadLibrary("wininet.dll");
    if (!hLib)
        return 0;
    pfnInternetGetConnectedState = (INETCHECKPROC)GetProcAddress(hLib, "InternetGetConnectedState");
    if (!pfnInternetGetConnectedState)
        return false;
    DWORD lpdwFlags;
    bRetVal = pfnInternetGetConnectedState(&lpdwFlags, 0) != 0;
    FreeLibrary(hLib);
    return bRetVal;
}


Вот только как узнать имя сети?
    


Ответы

Ответ 1



Так можно получить названия адаптера и соответствующего сетевого подключения: #include #include #include #include #include #include #include #pragma comment(lib, "ole32.lib") // Link with Iphlpapi.lib #pragma comment(lib, "IPHLPAPI.lib") #define WORKING_BUFFER_SIZE 15000 #define MAX_TRIES 3 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) /********************************************************************************/ //Получает информацию о соединении по GUID адаптера NLM_CONNECTIVITY GetAdapterConnectivity(LPCWSTR adapter){ INetworkListManager *pNetworkListManager = NULL; HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID *)&pNetworkListManager); if(FAILED(hr)){ printf("CoCreateInstance failed: 0x%x\n",(UINT)hr); return (NLM_CONNECTIVITY)0; } IEnumNetworkConnections* pEnum = NULL; hr = pNetworkListManager->GetNetworkConnections(&pEnum); if(FAILED(hr)){ pNetworkListManager->Release(); printf("GetNetworkConnections failed: 0x%x\n",(UINT)hr); return (NLM_CONNECTIVITY)0; } const int NUM_CONNECTION = 10; INetworkConnection* pNetworkConnections[NUM_CONNECTION]; ULONG cFetched = 0; BOOL bDone = FALSE; GUID guid; NLM_CONNECTIVITY nlmc=(NLM_CONNECTIVITY)0; while (!bDone) { hr = pEnum->Next(NUM_CONNECTION, pNetworkConnections, &cFetched); if (SUCCEEDED(hr) && (cFetched > 0)) { for (ULONG i = 0; i < cFetched; i++) { pNetworkConnections[i]->GetAdapterId(&guid); OLECHAR* guidString; StringFromCLSID(guid, &guidString); if(wcscmp(adapter,guidString) == 0){ pNetworkConnections[i]->GetConnectivity(&nlmc); } // ensure memory is freed ::CoTaskMemFree(guidString); pNetworkConnections[i]->Release(); } } else { bDone = TRUE; } } pEnum->Release(); pNetworkListManager->Release(); return nlmc; } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL,"Russian"); CoInitialize(NULL); /* Declare and initialize variables */ DWORD dwSize = 0; DWORD dwRetVal = 0; unsigned int i = 0; // Set the flags to pass to GetAdaptersAddresses ULONG flags = GAA_FLAG_INCLUDE_PREFIX; // default to unspecified address family (both) ULONG family = AF_UNSPEC; LPVOID lpMsgBuf = NULL; PIP_ADAPTER_ADDRESSES pAddresses = NULL; ULONG outBufLen = 0; ULONG Iterations = 0; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL; PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL; PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL; IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL; IP_ADAPTER_PREFIX *pPrefix = NULL; // Allocate a 15 KB buffer to start with. outBufLen = WORKING_BUFFER_SIZE; do { pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen); if (pAddresses == NULL) { printf ("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n"); goto End; } dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen); if (dwRetVal == ERROR_BUFFER_OVERFLOW) { FREE(pAddresses); pAddresses = NULL; } else { break; } Iterations++; } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES)); if (dwRetVal == NO_ERROR) { // If successful, output some information from the data we received wprintf(L"*** Network connections ***\n\n"); pCurrAddresses = pAddresses; while (pCurrAddresses) { WCHAR* adapter = NULL; int c = strlen(pCurrAddresses->AdapterName); adapter = (WCHAR*)MALLOC((c+1) * 2); ZeroMemory(adapter,(c+1) * 2); MultiByteToWideChar(CP_ACP,0,pCurrAddresses->AdapterName,c,adapter,c); //wprintf(L"\tGUID: %s\n", adapter); NLM_CONNECTIVITY nlmc = GetAdapterConnectivity(adapter); if(nlmc != NLM_CONNECTIVITY_DISCONNECTED){ wprintf(L"Adapter: %s\n", pCurrAddresses->Description); wprintf(L"Network: %s\n", pCurrAddresses->FriendlyName); wprintf(L"Connected to:", (UINT)nlmc); if (nlmc & NLM_CONNECTIVITY_IPV4_LOCALNETWORK) wprintf(L" IPv4 LAN;"); if (nlmc & NLM_CONNECTIVITY_IPV4_INTERNET) wprintf(L" IPv4 Internet;"); if (nlmc & NLM_CONNECTIVITY_IPV6_LOCALNETWORK) wprintf(L" IPv6 LAN;"); if (nlmc & NLM_CONNECTIVITY_IPV6_INTERNET) wprintf(L" IPv6 Internet"); wprintf(L"\n\n"); } FREE(adapter); pCurrAddresses = pCurrAddresses->Next; } } else { printf("Call to GetAdaptersAddresses failed with error: %d\n", dwRetVal); if (dwRetVal == ERROR_NO_DATA) printf("\tNo addresses were found for the requested parameters\n"); else { if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) & lpMsgBuf, 0, NULL)) { printf("\tError: %s", lpMsgBuf); LocalFree(lpMsgBuf); if (pAddresses) FREE(pAddresses); goto End; } } } if (pAddresses) { FREE(pAddresses); } End: system("PAUSE"); return 0; } А так - имя сети: #include #include #include #include #include #pragma comment(lib, "ole32.lib") /********************************************************************************/ int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL,"Russian"); CoInitialize(NULL); INetworkListManager *pNetworkListManager = NULL; IEnumNetworkConnections* pEnum = NULL; //инициализация Network List Manager API HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID *)&pNetworkListManager); if(FAILED(hr)){ printf("CoCreateInstance failed: 0x%x\n",(UINT)hr); goto End; } hr = pNetworkListManager->GetNetworkConnections(&pEnum); //получаем сетевые подключения if(FAILED(hr)){ printf("GetNetworkConnections failed: 0x%x\n",(UINT)hr); goto End; } wprintf(L"*** Networks *** \n\n"); const int NUM_CONNECTION = 10; INetworkConnection* pNetworkConnections[NUM_CONNECTION]; ULONG cFetched = 0; BOOL bDone = FALSE; NLM_CONNECTIVITY nlmc; INetwork* pNetwork = NULL; BSTR name; while (!bDone) { hr = pEnum->Next(NUM_CONNECTION, pNetworkConnections, &cFetched); if (SUCCEEDED(hr) && (cFetched > 0)) { for (ULONG i = 0; i < cFetched; i++) { nlmc=(NLM_CONNECTIVITY)0; name = NULL; hr = pNetworkConnections[i]->GetNetwork(&pNetwork); //получаем сеть, соответствующую подключению if(SUCCEEDED(hr)){ hr = pNetwork->GetName(&name); //получаем имя сети if(SUCCEEDED(hr) && name != NULL){ wprintf(L"%s ",name); pNetworkConnections[i]->GetConnectivity(&nlmc); if(nlmc != 0) wprintf(L"- connected to:"); if (nlmc & NLM_CONNECTIVITY_IPV4_LOCALNETWORK) wprintf(L" IPv4 LAN;"); if (nlmc & NLM_CONNECTIVITY_IPV4_INTERNET) wprintf(L" IPv4 Internet;"); if (nlmc & NLM_CONNECTIVITY_IPV6_LOCALNETWORK) wprintf(L" IPv6 LAN;"); if (nlmc & NLM_CONNECTIVITY_IPV6_INTERNET) wprintf(L" IPv6 Internet"); wprintf(L"\n"); } pNetwork->Release(); } // ensure memory is freed pNetworkConnections[i]->Release(); } } else { bDone = TRUE; } } End: if(pEnum!=NULL)pEnum->Release(); if(pNetworkListManager!=NULL)pNetworkListManager->Release(); system("PAUSE"); return 0; } Ссылки: Network Awareness in Windows Vista and Windows 7 GetAdaptersAddresses function

Комментариев нет:

Отправить комментарий