Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Removable drive events?

Back to Forum: Tech Off
  • User profile image


    I have spend a silly amount of time googling this, and decided this was the only place I am likely to get an answer...

    Is there an event that I can hook into that will alert me when a Removable Drive is either added or taken away?

    Also, it would be really useful to find out what the drive letter is...


  • User profile image

    Good question. I did a bit of searching and found this:

    Apparently there is a WM_DEVICECHANGE message, which would be handled via a wndproc function (in C/C++). I haven't actually tried this, however...but it looks interesting.

  • User profile image

    Sorry about the formatting, and that it is only snippets but it is ripped out of some code that I was playing with based on a sample I found from somewhere.  Sorry I can't post the attribution Sad

    static readonly int WM_DEVICECHANGE          = 0x219;
    static readonly int DBT_DEVICEARRIVAL        = 0x8000;
    static readonly int DBT_DEVICEREMOVECOMPLETE = 0x8004;
    static readonly int DBT_DEVTYP_VOLUME        = 0x00000002;

      [StructLayout( LayoutKind.Sequential )]
        public struct DEV_BROADCAST_VOLUME
            public int dbcv_size;
            public int dbcv_devicetype;
            public int dbcv_reserved;
            public int dbcv_unitmask;

     protected override void WndProc(ref Message msg)
                    if( msg.Msg == WM_DEVICECHANGE )
                        if( msg.WParam.ToInt32() == DBT_DEVICEARRIVAL )
                            int devType = Marshal.ReadInt32( msg.LParam,4 );
                            if( devType == DBT_DEVTYP_VOLUME )
                                DEV_BROADCAST_VOLUME vol;
                                vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure( msg.LParam,typeof( DEV_BROADCAST_VOLUME ) );

                base.WndProc( ref msg );

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.