You can use GetAdaptersAddresses to get the status of all adapters, and then check whether it is up or down. You will have to repeat the process until the status changes. I do not know how to get a notification.
ULONG nFlags = 0; DWORD dwVersion = ::GetVersion(); DWORD dwMajorVersion= (DWORD)(LOBYTE(LOWORD(dwVersion))); if (dwMajorVersion>=6) // flag supported in Vista and later nFlags= 0x0100; // GAA_FLAG_INCLUDE_ALL_INTERFACES*/ // during system initialization, GetAdaptersAddresses may return ERROR_BUFFER_OVERFLOW and supply nLen, // but in a subsequent call it may return ERROR_BUFFER_OVERFLOW and supply greater nLen ! ULONG nLen= sizeof (IP_ADAPTER_ADDRESSES); BYTE* pBuf= NULL; DWORD nErr= 0 ; do { delete[] pBuf; pBuf= new BYTE[nLen]; nErr= ::GetAdaptersAddresses(AF_INET, nFlags, NULL, (IP_ADAPTER_ADDRESSES*&)pBuf, &nLen); } while (ERROR_BUFFER_OVERFLOW == nErr); if (NO_ERROR != nErr) { delete[] pBuf; // report GetAdaptersAddresses failed return false; } const IP_ADAPTER_ADDRESSES* pAdaptersAddresses= (IP_ADAPTER_ADDRESSES*&)pBuf; while (pAdaptersAddresses) // for each adapter { // todo: check if this is your adapter... // pAdaptersAddresses->AdapterName // pAdaptersAddresses->Description // pAdaptersAddresses->FriendlyName const IF_OPER_STATUS& Stat= pAdaptersAddresses->OperStatus; // 1:up, 2:down... pAdaptersAddresses= pAdaptersAddresses->Next; } delete[] pBuf; return false;
In addition, for each adapter, you can search for its IP address in the registry. This will be in SYSTEM \ CurrentControlSet \ Services \ Tcpip \ Parameters \ Interfaces ## ADAPTERNAME ## if ## ADAPTERNAME ## is a member of AdapterName of the IP_ADAPTER_ADDRESSES structure. Check EnableDHCP to see if it is a dynamic address, and then look at the DhcpIPAddress key.
Lior kogan
source share