在Compact Framework的检测“网络电缆被拔出”
-
20-09-2019 - |
题
我已经通过所有的堆栈溢出的答案搜索来了,也不谷歌或冰都出现了我爱所有。我需要知道当网络电缆已经连接或断开的Windows CE设备上,preferrably,从精简框架的应用程序。
解决方案
我知道我在这里回答我的问题,但它实际上是要求通过电子邮件的问题,居然花了我很长一段时间寻找答案,所以我在这里张贴。
因此,对于这是怎么检测的一般的答案是,你必须通过一个IOCTL向下调入NDIS驱动程序,并告诉它你感兴趣的通知。这与 IOCTL_NDISUIO_REQUEST_NOTIFICATION 值完成(文档说这不是支持的WinMo,但该文档是错误的)。当然,接收通知并非如此简单 - 你son't只是得到一些不错的回调。相反,你必须旋转了一个点对点消息队列和发送在该IOCTL呼叫,与具体通知你想有一个面具一起。然后,当有新的变化(如电缆被拉),你会得到一个 NDISUIO_DEVICE_NOTIFICATION 结构(MSDN再次错误地说,这是CE-只)队列,然后你就可以分析发现,有事件,什么确切的事件的适配器。
从托管代码的角度来看,这实际上是一个很大的代码有写 - CreateFile打开NDIS,所有排队的API,用于通知等的结构幸运的是,我已经在这条路上,把它添加到智能设备框架了。所以,如果你正在使用自卫队,得到通知如下:
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));
}
}
}
不隶属于 StackOverflow