Windows 上网络接口就绪时的通知

发布于 2024-10-10 02:17:58 字数 93 浏览 2 评论 0原文

在 Windows XP 下,当网络接口启动并准备就绪时,如何收到通知?

就绪表示接口已通过 DHCP 获得网络 IP 地址并可供使用。

How do I receive notification when a network interface is brought up and ready, under Windows XP?

Ready means the interface already obtained a network IP address via DHCP and is ready to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-10-17 02:18:04

您可以使用 GetAdaptersAddresses 接收所有适配器的状态,然后检查它是启动还是关闭。
您必须重复该过程,直到状态发生变化。
我不知道有什么方法可以接收通知。

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;

此外,对于每个适配器,您可以在注册表中搜索其 IP 地址。这将位于 SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces##ADAPTERNAME## 中,其中 ##ADAPTERNAME## 是 IP_ADAPTER_ADDRESSES 结构的 AdapterName 成员。
检查EnableDHCP看是否是动态地址,然后查看DhcpIPAddress键。

You can use GetAdaptersAddresses to receive status of all adapters, then check if it is up or down.
You'll have to repeat the process till the status changes.
I'm not aware of any way to receive 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;

Also, for each adapter you can search it's IP address in the registry. That would be in SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces##ADAPTERNAME##, Were ##ADAPTERNAME## is the AdapterName member of the IP_ADAPTER_ADDRESSES structure.
Check the EnableDHCP to find if it is a dynamic address, then look at the DhcpIPAddress key.

我不咬妳我踢妳 2024-10-17 02:18:03

当接口准备好时,你绝对可以得到一个事件!只需使用 IPHelper 即可!您要查找的函数是 NotifyAddrChange http: //msdn.microsoft.com/en-us/library/aa366329%28v=VS.85%29.aspx 并且从 Windows 2000 开始可用。当适配器启动并运行时,将对其进行分配IP地址,从而触发回调。

触发时可以使用 GetAdapterAddress 来获取所需的信息。在 Vista 或更高版本上,有 NotifyIpInterfaceChange 可以直接告诉哪个适配器有 IP 更改。

You can definitely get an event when an interface is ready! Just use IPHelper! The function you shall be looking for is NotifyAddrChange http://msdn.microsoft.com/en-us/library/aa366329%28v=VS.85%29.aspx and it is available starting from Windows 2000. When an adapter is up and running, it will be assigned an IP address, and thus triggered the callback.

A GetAdapterAddress can be used when triggered to figure the information you need. On Vista or above there is NotifyIpInterfaceChange that directly tell which adapter has IP change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文