5

我已经完成了所有 Stack Overflow 搜索提供的答案,谷歌或 Bing 都没有向我展示任何爱。我需要知道何时在 Windows CE 设备上连接或断开网络电缆,最好是从 Compact Framework 应用程序。

4

1 回答 1

3

我意识到我在这里回答了我自己的问题,但这实际上是通过电子邮件提出的问题,我实际上花了很长时间寻找答案,所以我在这里发布。

因此,如何检测到这一点的一般答案是您必须通过 IOCTL 调用 NDIS 驱动程序并告诉它您对通知感兴趣。这是通过IOCTL_NDISUIO_REQUEST_NOTIFICATION值完成的(文档说 WinMo 不支持此功能,但文档是错误的)。当然,接收通知并不是那么简单——你儿子不只是得到一些不错的回调。相反,您必须启动一个点对点消息队列并将其发送到 IOCTL 调用,以及您想要哪些特定通知的掩码。然后,当某些事情发生变化(例如电缆被拉动)时,您将获得NDISUIO_DEVICE_NOTIFICATION队列中的结构(MSDN 再次错误地表示这是仅限 CE),然后您可以对其进行解析以找到具有事件的适配器以及确切的事件是什么。

从托管代码的角度来看,这实际上需要编写很多代码 - CreateFile 来打开 NDIS、所有排队 API、通知结构等。幸运的是,我已经走上了这条路并添加了它已经到智能设备框架了。因此,如果您使用的是 SDF,则获取通知如下所示:

public partial class TestForm : Form
{
    public TestForm()
    {
        InitializeComponent();

        this.Disposed += new EventHandler(TestForm_Disposed);

        AdapterStatusMonitor.NDISMonitor.AdapterNotification += 
            new AdapterNotificationEventHandler(NDISMonitor_AdapterNotification);
        AdapterStatusMonitor.NDISMonitor.StartStatusMonitoring();
    }

    void TestForm_Disposed(object sender, EventArgs e)
    {
        AdapterStatusMonitor.NDISMonitor.StopStatusMonitoring();
    }

    void NDISMonitor_AdapterNotification(object sender, 
                                         AdapterNotificationArgs e)
    {
        string @event = string.Empty;

        switch (e.NotificationType)
        {
            case NdisNotificationType.NdisMediaConnect:
                @event = "Media Connected";
                break;
            case NdisNotificationType.NdisMediaDisconnect:
                @event = "Media Disconnected";
                break;
            case NdisNotificationType.NdisResetStart:
                @event = "Resetting";
                break;
            case NdisNotificationType.NdisResetEnd:
                @event = "Done resetting";
                break;
            case NdisNotificationType.NdisUnbind:
                @event = "Unbind";
                break;
            case NdisNotificationType.NdisBind:
                @event = "Bind";
                break;
            default:
                return;
        }

        if (this.InvokeRequired)
        {
            this.Invoke(new EventHandler(delegate
            {
                eventList.Items.Add(string.Format(
                                    "Adapter '{0}' {1}", e.AdapterName, @event));
            }));
        }
        else
        {
            eventList.Items.Add(string.Format(
                                "Adapter '{0}' {1}", e.AdapterName, @event));
        }
    }
}
于 2009-10-09T15:24:01.560 回答