在Compact Framework中检测“网络电缆已拔出" [英] Detecting 'Network Cable Unplugged' in the Compact Framework

查看:133
本文介绍了在Compact Framework中检测“网络电缆已拔出"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了所有出现的堆栈溢出答案搜索,而Google或Bing都没有向我显示任何爱意.我需要知道何时在Windows CE设备上(最好是从Compact Framework应用程序)连接或断开了网络电缆.

I've been through all of the Stack Overflow answers search comes up with, and neither Google or Bing are showing me any love. I need to know when a network cable has been connected or disconnected on a Windows CE device, preferrably, from a Compact Framework application.

推荐答案

我知道我在这里回答自己的问题,但这实际上是通过电子邮件提出的问题,我花了相当长的时间找到答案,所以我要在这里发布.

I realize I'm answering my own question here, but it was actually a question asked of via email, and I actually spent quite a while finding the answer, so I'm posting it here.

因此,有关如何检测到此问题的一般答案是,您必须通过IOCTL调用NDIS驱动程序,并告诉它您对通知感兴趣.这是通过 IOCTL_NDISUIO_REQUEST_NOTIFICATION 值完成的(文档说这不是在WinMo中受支持,但文档有误).当然,接收通知并不是那么简单-您不仅会收到一些不错的回调.相反,您必须启动点对点消息队列并发送到IOCTL呼叫中,以及所需的特定通知的掩码.然后,当某些更改(例如电缆被拔出)时,您将得到一个 NDISUIO_DEVICE_NOTIFICATION 队列上的结构(同样,MSDN错误地表示这仅是CE),然后可以对其进行解析以找到具有该事件以及确切事件是什么的适配器.

So the general answer for how this is detected is that you have to call down into the NDIS driver via an IOCTL and tell it that you're interested in notifications. This is done with the IOCTL_NDISUIO_REQUEST_NOTIFICATION value (the docs say this is not supported in WinMo, but the docs are wrong). Of course receiving the notifications isn't so straightforward - you son't just get some nice callback. Instead you have to spin up a point to point message queue and send that in to the IOCTL call, along with a mask of which specific notifications you want. Then, when something changes (like the cable is pulled) you'll get an NDISUIO_DEVICE_NOTIFICATION structure (again MSDN incorrectly says this is CE-only) on the queue, which you can then parse to find the adapter that had the event and what the exact event is.

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

From a managed code perspective, this is actually a lot of code to have to write - CreateFile to open NDIS, all of the queueing APIs, the structures for the notifications, etc. Fortunately, I'd already been down this road and had added it to the Smart Device Framework already. So if you're using the SDF, getting the notifications looks like this:

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));
        }
    }
}

这篇关于在Compact Framework中检测“网络电缆已拔出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆