Author Topic: Device Notification  (Read 12752 times)

vanweric

  • Member
  • ***
  • Posts: 24
Device Notification
« on: June 14, 2011, 06:11:19 pm »
I've been attempting to get messages on the insertion/removal of USB devices with minimal luck.

As far as I can tell, the GenericHID and the usbhidio examples don't explicitly register for these messages - they receive all windows messages by virtue of being "form"s, and then filter for the relevant messages based on m.Msg.   

Is there a way to get these messages from within a console application?  I have used the "NativeWindow" class and the RegisterForDeviceNotifications function from GenericHID, but I do not receive anything beyond the initial 4:
           msg=0x24 (WM_GETMINMAXINFO) hwnd=0x2021f4 wparam=0x0 lparam=0x68be4a8 result=0x0
           msg=0x81 (WM_NCCREATE) hwnd=0x2021f4 wparam=0x0 lparam=0x68be49c result=0x0
           msg=0x83 (WM_NCCALCSIZE) hwnd=0x2021f4 wparam=0x0 lparam=0x68be488 result=0x0
           msg=0x1 (WM_CREATE) hwnd=0x2021f4 wparam=0x0 lparam=0x68be49c result=0x0
 
I am working with C#.

Thank You
 - Eric

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Device Notification
« Reply #1 on: June 14, 2011, 08:57:34 pm »
This has a note about use in console applications:

http://msdn.microsoft.com/en-us/library/aa363432%28VS.85%29.aspx

Jan

vanweric

  • Member
  • ***
  • Posts: 24
Re: Device Notification
« Reply #2 on: June 15, 2011, 07:23:04 pm »
Thanks Jan.  I don't have the console part working yet, but I did stumble across a way to make DEV_BROADCAST_DEVICE_INTERFACE_1 unnecessary (i think):

Code: [Select]
        /// <summary>
        /// Contains information about a class of devices.
        ///
        /// <para>Use this one in the call to RegisterDeviceNotification() and
        /// in checking dbch_devicetype in a DEV_BROADCAST_HDR structure:</para>
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
        public class DEV_BROADCAST_DEVICEINTERFACE
        {
            /// <summary>
            /// The size of this structure, in bytes. This is the size of the members
            /// plus the actual length of the dbcc_name string (the null character is
            /// accounted for by the declaration of dbcc_name as a one-character array.)
            /// </summary>
            public int dbcc_size;
            /// <summary>
            /// Set to DBT_DEVTYP_DEVICEINTERFACE
            /// </summary>
            public DBT_DEVTYP dbcc_devicetype;
            /// <summary>
            /// Reserved; do not use.
            /// </summary>
            public int dbcc_reserved;
            /// <summary>
            /// The GUID for the interface device class.
            /// </summary>
            public Guid dbcc_classguid;
            /// <summary>
            /// A null-terminated string that specifies the name of the device.
            ///
            /// <para>When this structure is returned to a window through the WM_DEVICECHANGE message,
            /// the dbcc_name string is converted to ANSI as appropriate. Services always receive a
            /// Unicode string, whether they call RegisterDeviceNotificationW or RegisterDeviceNotificationA.</para>
            /// </summary>
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
            public string dbcc_name;
        }


It is working in the "it hasn't crashed my machine yet" sense of the word, but I thought it might interest you.  I am using this single structure declaration for registering and receiving.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Device Notification
« Reply #3 on: June 16, 2011, 10:30:23 am »
Thanks for this!

Jan