Media Inserted into drive

  • Thread starter Thread starter Andrew Tracy
  • Start date Start date
A

Andrew Tracy

How do I detect a media: cd/dvd/compact flash/memory stick
As it is being inserted into the the machines?

the api call listing for the : WM_DEVICECHANGE
no longer works
 
Hi Andrew,

Thanks for your post. We are able to use WM_DEVICECHANGE in .NET
application. I checked it with the following code snippet and it works
properly on my side.

//-------------code snippet-------------
using System.Runtime.InteropServices;

namespace DeviceNotification
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
RegisterHidNotification();
}

protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_DEVICECHANGE: OnDeviceChange(ref m); break;
}
base.WndProc (ref m);
}

void OnDeviceChange(ref Message msg)
{
int wParam = (int)msg.WParam;
if(wParam == Win32.DBT_DEVICEARRIVAL)
MessageBox.Show("Arrival");
else if(wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
MessageBox.Show("Remove");
}

void RegisterHidNotification()
{
Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new
Win32.DEV_BROADCAST_DEVICEINTERFACE();

int size = Marshal.SizeOf(dbi);
dbi.dbcc_size = size;
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved = 0;
dbi.dbcc_classguid = Win32.GUID_IO_MEDIA_ARRIVAL;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterDeviceNotification(Handle, buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE);

if(r == IntPtr.Zero)
MessageBox.Show(Win32.GetLastError().ToString());
}
}

class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIVAL = 0x8000,
DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_WINDOW_HANDLE = 0,
DEVICE_NOTIFY_SERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVICEINTERFACE = 5;
public static Guid
GUID_IO_MEDIA_ARRIVAL = new
Guid("d07433c0-a98e-11d2-917a-00a0c9068ff3");

[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
IntPtr NotificationFilter,
Int32 Flags);

[DllImport("kernel32.dll")]
public static extern int GetLastError();
}
}
//------------------end of-----------------------

In addition, I believe the article below is helpful:
Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
This is exactly what I am looking for
any chance you can help me out on the vb.net side?

Thank in advance,
AT

-----Original Message-----
Hi Andrew,

Thanks for your post. We are able to use WM_DEVICECHANGE in .NET
application. I checked it with the following code snippet and it works
properly on my side.

//-------------code snippet-------------
using System.Runtime.InteropServices;

namespace DeviceNotification
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
RegisterHidNotification();
}

protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_DEVICECHANGE: OnDeviceChange (ref m); break;
}
base.WndProc (ref m);
}

void OnDeviceChange(ref Message msg)
{
int wParam = (int)msg.WParam;
if(wParam == Win32.DBT_DEVICEARRIVAL)
MessageBox.Show("Arrival");
else if(wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
MessageBox.Show("Remove");
}

void RegisterHidNotification()
{
Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new
Win32.DEV_BROADCAST_DEVICEINTERFACE();

int size = Marshal.SizeOf(dbi);
dbi.dbcc_size = size;
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved = 0;
dbi.dbcc_classguid = Win32.GUID_IO_MEDIA_ARRIVAL;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterDeviceNotification (Handle, buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE);

if(r == IntPtr.Zero)
MessageBox.Show(Win32.GetLastError().ToString ());
}
}

class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIVAL = 0x8000,
DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_WINDOW_HANDLE = 0,
DEVICE_NOTIFY_SERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVICEINTERFACE = 5;
public static Guid
GUID_IO_MEDIA_ARRIVAL = new
Guid("d07433c0-a98e-11d2-917a-00a0c9068ff3");

[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
IntPtr NotificationFilter,
Int32 Flags);

[DllImport("kernel32.dll")]
public static extern int GetLastError();
}
}
//------------------end of-----------------------

In addition, I believe the article below is helpful:
Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

.
 
Hi Andrew,

Thanks for your feedback. I translated the code snippet to VB .NET, please
check it on your side.

//--------------code snippet-------------
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

Public Class Win32
Public Const WM_DEVICECHANGE = &H219
Public Const DBT_DEVICEARRIVAL = &H8000
Public Const DBT_DEVICEREMOVECOMPLETE = &H8004
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Public Const DEVICE_NOTIFY_SERVICE_HANDLE = 1
Public Const DBT_DEVTYP_DEVICEINTERFACE = 5
Public Shared GUID_IO_MEDIA_ARRIVAL As Guid = New
Guid("d07433c0-a98e-11d2-917a-00a0c9068ff3")


<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
Public dbcc_classguid As Guid
Public dbcc_name As Short
End Class


<DllImport("user32.DLL", SetLastError:=True)> _
Public Shared Function _
RegisterDeviceNotification(ByVal IntPtr As IntPtr, ByVal
NotificationFilter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function

<DllImport("kernel32.DLL")> _
Public Shared Function _
GetLastError() As Integer
End Function

End Class

Public Sub New()
MyBase.New()

InitializeComponent()
RegisterHidNotification()
End Sub

Public Sub RegisterHidNotification()
Dim dbi = New Win32.DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
dbi.dbcc_size = size
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = Win32.GUID_IO_MEDIA_ARRIVAL
dbi.dbcc_name = 0
Dim Buffer As IntPtr
Buffer = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = Win32.RegisterDeviceNotification(Handle, Buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE)
If r.ToInt32 = IntPtr.Zero.ToInt32 Then
MessageBox.Show(Win32.GetLastError().ToString())
End If

End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = Win32.WM_DEVICECHANGE Then
OnDeviceChange(m)
End If

MyBase.WndProc(m)
End Sub

Private Sub OnDeviceChange(ByVal msg As Message)
Dim wParam As Integer
wParam = msg.WParam.ToInt32()

If wParam = Win32.DBT_DEVICEARRIVAL Then
MessageBox.Show("Arrival")
ElseIf wParam = Win32.DBT_DEVICEREMOVECOMPLETE Then
MessageBox.Show("Remove")
End If
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
Dim t As New Guid("d07433c0-a98e-11d2-917a-00a0c9068ff3")
End Sub
End Class
//-------------------end of---------------------

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top