2009-12-29 8 views
3

J'ai vu quelques exemples sur l'utilisation de RegisterDeviceNotification à partir de l'API Windows, mais je n'ai vu aucun exemple .NET. Je veux écrire une application C# qui est notifiée quand un nouveau lecteur apparaît (en particulier via USB, firewire, etc). Cette application doit être un service Windows, donc je ne peux pas utiliser les messages WM_DEVICECHANGE sans piratage. J'aimerais éviter ça. Quelqu'un peut-il me donner un exemple de comment utiliser RegisterDeviceNotification en C#, ou au moins me donner une bonne alternative?Utilisation de RegisterDeviceNotification dans une application .NET

EDIT: Encore une fois, ceci est un service Windows qui n'a pas d'interface utilisateur graphique. Ainsi, le possible duplicate qui implique des messages de notification WM ne fonctionnera pas dans cette situation.

+0

Possible copie de [Vérifier les événements de modification de périphérique (ajouter/supprimer)] (http://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events) –

+0

@ErwinMayer Nope. Voir éditer. – Phil

Répondre

3
+0

Le lien est mort - pensez à trouver un résumé + ajout de résumé OU trouver un doublon pour la question. Il y en a un similaire http://stackoverflow.com/questions/1783657/get-notification-whena-new-drive-is-connected-via-usb-or-other-means-c?rq=1 (sans haute réponses de qualité) –

0

This answer fournit des exemples de code pertinents pour détecter la suppression des périphériques USB ou plus:

using System; 
using System.Runtime.InteropServices; 

internal static class UsbNotification 
{ 
    public const int DbtDevicearrival = 0x8000; // system detected a new device   
    public const int DbtDeviceremovecomplete = 0x8004; // device is gone  
    public const int WmDevicechange = 0x0219; // device change event  
    private const int DbtDevtypDeviceinterface = 5; 
    private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices 
    private static IntPtr notificationHandle; 

    /// <summary> 
    /// Registers a window to receive notifications when USB devices are plugged or unplugged. 
    /// </summary> 
    /// <param name="windowHandle">Handle to the window receiving notifications.</param> 
    public static void RegisterUsbDeviceNotification(IntPtr windowHandle) 
    { 
     DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface 
     { 
      DeviceType = DbtDevtypDeviceinterface, 
      Reserved = 0, 
      ClassGuid = GuidDevinterfaceUSBDevice, 
      Name = 0 
     }; 

     dbi.Size = Marshal.SizeOf(dbi); 
     IntPtr buffer = Marshal.AllocHGlobal(dbi.Size); 
     Marshal.StructureToPtr(dbi, buffer, true); 

     notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0); 
    } 

    /// <summary> 
    /// Unregisters the window for USB device notifications 
    /// </summary> 
    public static void UnregisterUsbDeviceNotification() 
    { 
     UnregisterDeviceNotification(notificationHandle); 
    } 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags); 

    [DllImport("user32.dll")] 
    private static extern bool UnregisterDeviceNotification(IntPtr handle); 

    [StructLayout(LayoutKind.Sequential)] 
    private struct DevBroadcastDeviceinterface 
    { 
     internal int Size; 
     internal int DeviceType; 
     internal int Reserved; 
     internal Guid ClassGuid; 
     internal short Name; 
    } 
} 

Utilisation d'une fenêtre WPF:

protected override void OnSourceInitialized(EventArgs e) 
    { 
     base.OnSourceInitialized(e); 

     // Adds the windows message processing hook and registers USB device add/removal notification. 
     HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); 
     if (source != null) 
     { 
      windowHandle = source.Handle; 
      source.AddHook(HwndHandler); 
      UsbNotification.RegisterUsbDeviceNotification(windowHandle); 
     } 
    } 

    /// <summary> 
    /// Method that receives window messages. 
    /// </summary> 
    private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) 
    { 
     if (msg == UsbNotification.WmDevicechange) 
     { 
      switch ((int)wparam) 
      { 
       case UsbNotification.DbtDeviceremovecomplete: 
        Usb_DeviceRemoved(); // this is where you do your magic 
        break; 
       case UsbNotification.DbtDevicearrival: 
        Usb_DeviceAdded(); // this is where you do your magic 
        break; 
      } 
     } 

     handled = false; 
     return IntPtr.Zero; 
    } 

Utilisation à partir de Windows Formulaires:

public Form1() 
{ 
    InitializeComponent(); 
    UsbNotification.RegisterUsbDeviceNotification(this.Handle); 
} 

protected override void WndProc(ref Message m) 
{ 
    base.WndProc(ref m); 
     if (m.Msg == UsbNotification.WmDevicechange) 
    { 
     switch ((int)m.WParam) 
     { 
      case UsbNotification.DbtDeviceremovecomplete: 
       Usb_DeviceRemoved(); // this is where you do your magic 
       break; 
      case UsbNotification.DbtDevicearrival: 
       Usb_DeviceAdded(); // this is where you do your magic 
       break; 
     } 
    } 
}  
Questions connexes