我已经浏览了 Stack Overflow 搜索得到的所有答案,但 Google 或 Bing 都没有向我展示任何爱意。我需要知道何时在 Windows CE 设备上(最好是从 Compact Framework 应用程序)连接或断开网络电缆。
我意识到我在这里回答了我自己的问题,但这实际上是通过电子邮件提出的问题,而且我实际上花了很长时间才找到答案,所以我将其发布在这里。
因此,关于如何检测到这一点的一般答案是,您必须通过 IOCTL 调用 NDIS 驱动程序并告诉它您对通知感兴趣。这是通过IOCTL_NDISUIO_REQUEST_NOTIFICATION http://msdn.microsoft.com/en-us/library/aa924905.aspx值(文档说 WinMo 不支持此功能,但文档是错误的)。当然,接收通知并不是那么简单 - 您不仅仅是得到一些不错的回电。相反,你必须旋转一个点对点消息队列 http://msdn.microsoft.com/en-us/library/bb202792.aspx并将其连同您想要的特定通知的掩码一起发送到 IOCTL 调用。然后,当发生变化(例如电缆被拉动)时,您会得到一个NDISUIO_DEVICE_NOTIFICATION http://msdn.microsoft.com/en-us/library/aa912746.aspx队列上的结构(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));
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)