thanks your link and the one provided by SorrowMan in his post was
helpful, but still i have some problems:
first of all the Ying-Shen Yu's solution works only at the Form level
and not on the Control level. It looks like the Control does not receive
the WM_DEVICECHANGE D message at all while the Form does.
The solution from this article:
http://www.codeproject.com/dotnet/de...umemonitor.asp
gave me some clues, so i've created my own class that derives from
NativeWindow and overrides WndProc to filter for WM_DEVICECHANGE D
message. Unfortunately this still needs the Form's Handle to hookup to.
so my code looks now more or less like this:
in my custom control i have:
private DeviceMonitor deviceMonitor;
protected override void OnCreateControl ()
{
if (!this.DesignMo de)
{
/* do some other initialization */
this.deviceMoni tor = new DeviceMonitor(
this.Parent.Han dle);
this.deviceMoni tor.DeviceChang ed
+= /* hook up to the event */
}
base.OnCreateCo ntrol();
}
/* the DeviceMonitor class */
class DeviceMonitor : System.Windows. Forms.NativeWin dow
{
public DeviceMonitor(I ntPtr handle)
{
this.AssignHand le(handle);
}
protected override void WndProc(ref System.Windows. Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt3 2() == DBT_DEVICEARRIV AL)
{
int devType = Marshal.ReadInt 32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLU ME)
{
DEV_BROADCAST_V OLUME vol;
vol = (DEV_BROADCAST_ VOLUME)Marshal. PtrToStructure(
m.LParam, typeof(DEV_BROA DCAST_VOLUME));
/* fire the DeviceChanged event */
}
}
base.WndProc(re f m);
}
public event DeviceEventHand ler DeviceChanged;
protected void OnDeviceChanged (DeviceEventArg s e)
{
if (DeviceChanged != null)
DeviceChanged(t his, e);
}
}
/* end of code */
with this solution everything works fine but it looks so awkward to me,
especially this part during Control initialization:
this.deviceMoni tor = new DeviceMonitor(t his.Parent.Hand le);
it's just counter intuitive that i have to hookup to the Parent's Handle
to get the WM_DEVICECHANGE D event :/ it's just not the all purpose idea
- if the owner is another control then this solution wont work at all. i
know i can provide aditional methods that will let the user to set the
Handle manually but this makes this solution even worse.
does anyone have any idea how can i improve this solution to make it
better and more reasonable?
Michael Nemtsev wrote:
Hello SharpCoderMP,
API WM_DEVICECHANGE message is responsible for this
Googling and you will find several samples how to handle this message in C#
Smth like this
http://www.dotnet247.com/247referenc...43/217435.aspx, Ying-Shen Yu
reply
S> what can i do to have my app be notified when there is a new drive in
S> the system? I need to perform some actions in my application when new
S> drive (like SD card or some usb storage device) shows up in the
S> system.
S> ---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche