Tech Off Thread

3 posts

Removable drive events?

Back to Forum: Tech Off
  • User profile image
    jh71283

    Morning...

    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...

    TIA

  • User profile image
    thumbtacks2

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

    http://msdn2.microsoft.com/en-us/library/aa363480.aspx

    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
    Rossj

    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 );
            }

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.