2009-10-09 48 views
5

我已經通過了所有堆棧溢出答案搜索,並且Google或Bing都沒有向我展示任何愛。我需要知道何時在Windows CE設備上連接或斷開網絡電纜,最好是從Compact Framework應用程序中斷開。在精簡框架中檢測'網絡電纜未拔掉'

回答

3

我意識到我在這裏回答我自己的問題,但它實際上是一個通過電子郵件詢問的問題,實際上我花了很長時間找到答案,所以我在這裏發佈它。

因此,檢測這種情況的一般答案是您必須通過IOCTL調用NDIS驅動程序並告訴它您對通知感興趣。這是通過IOCTL_NDISUIO_REQUEST_NOTIFICATION的值完成的(文檔說這不是在WinMo中支持的,但是文檔是錯誤的)。當然,接收通知並不是那麼直截了當 - 你不會得到一些不錯的回調。相反,您必須啓動一個point to point message queue並將其發送給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)); 
     } 
    } 
}