For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > .Net code to Detect When an External Drive is Mounted?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author .Net code to Detect When an External Drive is Mounted?
Sped

2005-01-20, 3:59 pm


Hi,

I'm looking for some code to monitor for when an external drive is
connected to a computer (USB ThumbDrive, External Hard Drive, etc.) and
is assigned a drive letter.

I'd want my code to trigger off of the mounting process so I can search
for a specific file on that hard drive (which is always guaranteed to
be in a specific spot).

Is this a Windows Event I can attach to?

Either Vb.net or C# code would be extremely helpful.
Thanks,
Spedlists@erstads.com
(sorry for the cross-post)

Adam

2005-01-26, 3:58 pm

Here you go. I know that this code works for CD removal/insertion. I
haven't tested it for other devices, but it really ought to work.

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Whatever {

public class HookEventArgs : EventArgs {
public int HookCode;
public IntPtr wParam;
public IntPtr lParam;
}

// Hook Types
public enum HookType : int {
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}

/// <summary>
/// Summary description for Win32Hooks.
/// </summary>
public class Win32Hook {

public delegate int HookProc(int code, IntPtr wParam, IntPtr
lParam);

protected IntPtr m_hhook = IntPtr.Zero;
protected HookProc m_filterFunc = null;
protected HookType m_hookType;

public delegate void HookEventHandler(object sender, HookEventArgs
e);

public event HookEventHandler HookInvoked;
protected void OnHookInvoked(HookEventArgs e) {
if (HookInvoked != null) {
HookInvoked(this, e);
}
}

public Win32Hook(HookType hook) {
m_hookType = hook;
m_filterFunc = new HookProc(this.CoreHookProc);
}
public Win32Hook(HookType hook, HookProc func) {
m_hookType = hook;
m_filterFunc = func;
}

public int CoreHookProc(int code, IntPtr wParam, IntPtr lParam) {

if (code < 0) {
return CallNextHookEx(m_hhook, code, wParam, lParam);
}

HookEventArgs e = new HookEventArgs();
e.HookCode = code;
e.wParam = wParam;
e.lParam = lParam;
OnHookInvoked(e);

return CallNextHookEx(m_hhook, code, wParam, lParam);
} // CoreHookProc

public void Install() {

m_hhook = SetWindowsHookEx(m_hookType, m_filterFunc, IntPtr.Zero,
(int) AppDomain.GetCurrentThreadId());
}

public void Uninstall() {
UnhookWindowsHookEx(m_hhook);
}

#region Win32 Imports
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code,
HookProc func, IntPtr hInstance, int threadID);

[DllImport("user32.dll")]
protected static extern int UnhookWindowsHookEx(IntPtr hhook);

[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook, int code,
IntPtr wParam, IntPtr lParam);
#endregion
}

public class DeviceChangeEventArgs {
public string drive;
public DirectoryInfo driveInfo;
public IntPtr eventHwnd;
}

public class DeviceChangeHook : Win32Hook {
const int WM_DEVICECHANGE = 0x0219;
private static IntPtr _forHWnd;
public static IntPtr ForHWnd {
get {
return DeviceChangeHook._forHWnd;
}

set {
DeviceChangeHook._forHWnd = value;
}
}

protected enum DbtHookActions {
DeviceArrival = 0x8000,
DeviceRemoveComplete = 0x8004
}

[StructLayout(LayoutKind.Sequential)]
protected class DevBroadcastVolume {
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
public short dbcv_flags;
}

// class cwp maps the unmanaged Win32 struct CWPSTRUCT that is
declared in
// winuser.h
// See
http://msdn.microsoft.com/library/d...s/CWPSTRUCT.asp
[StructLayout(LayoutKind.Sequential)]
protected class cwp {
public IntPtr lParam;
public int wParam;
public ushort message;
public IntPtr hwnd;
}

public delegate void DeviceChangeEventHandler(object sender,
DeviceChangeEventArgs dce);
public event DeviceChangeEventHandler DeviceArrived;
public event DeviceChangeEventHandler DeviceRemoved;

static DeviceChangeHook() {
DeviceChangeHook._forHWnd = IntPtr.Zero;
}

public DeviceChangeHook() : base(HookType.WH_CALLWNDPROC) {
this.HookInvoked += new HookEventHandler(GetMessageHookInvoked);

}

public DeviceChangeHook(HookProc func) :
base(HookType.WH_CALLWNDPROC, func) {
this.HookInvoked += new HookEventHandler(GetMessageHookInvoked);

}

private void GetMessageHookInvoked(object sender, HookEventArgs he)
{

cwp eventMessage = new cwp();
eventMessage = (cwp) Marshal.PtrToStructure(he.lParam,
eventMessage.GetType());
if ((eventMessage.message == 0x0219) && (eventMessage.hwnd ==
DeviceChangeHook._forHWnd)) {
DbtHookActions action = (DbtHookActions) eventMessage.wParam;
DevBroadcastVolume eventInfo = new DevBroadcastVolume();
eventInfo = (DevBroadcastVolume)
Marshal.PtrToStructure(eventMessage.lParam, eventInfo.GetType());

DeviceChangeEventArgs changeArgs = new DeviceChangeEventArgs();

switch (action) {
case DbtHookActions.DeviceArrival:
changeArgs.drive =
decodeDriveLetter(eventInfo.dbcv_unitmask);
changeArgs.driveInfo = new DirectoryInfo(changeArgs.drive +
":\\");
changeArgs.eventHwnd = eventMessage.hwnd;
DeviceArrived(this, changeArgs);
break;
case DbtHookActions.DeviceRemoveComplete:
changeArgs.drive =
decodeDriveLetter(eventInfo.dbcv_unitmask);
changeArgs.driveInfo = null;
changeArgs.eventHwnd = eventMessage.hwnd;
DeviceRemoved(this, changeArgs);
break;
default:
break;
} // switch
}
} // GetMessageHookInvoked

protected string decodeDriveLetter(int unitMask) {

char driveLetter;
for (driveLetter = 'A'; driveLetter <= 'Z'; ++driveLetter) {
if ((unitMask & 0x1) == 0x1)
break;
unitMask = unitMask >> 1;
}

if (driveLetter > 'Z') // Catch any unitMask failure
driveLetter = '0';

return driveLetter.ToString();
}
} // class DeviceChangeHook
}

Sped wrote:
> Hi,
>
> I'm looking for some code to monitor for when an external drive is
> connected to a computer (USB ThumbDrive, External Hard Drive, etc.)

and
> is assigned a drive letter.
>
> I'd want my code to trigger off of the mounting process so I can

search
> for a specific file on that hard drive (which is always guaranteed to
> be in a specific spot).
>
> Is this a Windows Event I can attach to?
>
> Either Vb.net or C# code would be extremely helpful.
> Thanks,
> Spedlists@erstads.com
> (sorry for the cross-post)


givemepower

2006-01-17, 1:19 am

hi Adam,

could you give us a quick example?

Thanks
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com