Notification of when the network interface is ready for use in Windows

How to receive notifications when a network interface is brought up and ready in Windows XP?

Ready means that the interface has already received a network IP address through DHCP and is ready to use.

+6
windows winapi networking
source share
2 answers

You can definitely get an event when the interface is ready! Just use IPHelper! The function you are looking for is NotifyAddrChange http://msdn.microsoft.com/en-us/library/aa366329%28v=VS.85%29.aspx , and it is available with Windows 2000. When the adapter works and works, it will assign an IP address and thus trigger a callback.

The GetAdapterAddress attribute can be used when it is run to determine the information you need. In Vista or higher, there are NotifyIpInterfaceChange that directly indicate which adapter has an IP change.

+7
source share

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.

+1
source share

All Articles