728x90
반응형
728x170
▶ USBHelper.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace TestProject
{
/// <summary>
/// USB 헬퍼
/// </summary>
public class USBHelper : IDisposable
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Import
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 장치 통지 등록하기 - RegisterDeviceNotification(recipientHandle, notificationFilter, flag)
/// <summary>
/// 장치 통지 등록하기
/// </summary>
/// <param name="recipientHandle">수취인 핸들</param>
/// <param name="notificationFilter">통지 필터</param>
/// <param name="flag">플래그</param>
/// <returns>장치 통지 핸들</returns>
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr recipientHandle, IntPtr notificationFilter, uint flag);
#endregion
#region 장치 통지 등록 취소하기 - UnregisterDeviceNotification(handle)
/// <summary>
/// 장치 통지 등록 취소하기
/// </summary>
/// <param name="handle">핸들</param>
/// <returns>처리 결과</returns>
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterDeviceNotification(IntPtr handle);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// WM_DEVICECHANGE
/// </summary>
public const int WM_DEVICECHANGE = 0x0219;
/// <summary>
/// DBT_DEVTYP_DEVICEINTERFACE
/// </summary>
public const int DBT_DEVTYP_DEVICEINTERFACE = 0x05;
/// <summary>
/// DEVICE_NOTIFY_WINDOW_HANDLE
/// </summary>
public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 장치 통지 핸들 리스트
/// </summary>
private List<IntPtr> deviceNotificationHandleList = new List<IntPtr>();
private Guid[] DEVINTERFACE_GUID_ARRAY =
{
// GUID_DEVINTERFACE_USB_DEVICE
new Guid(0xA5DCBF10, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED),
// GUID_DEVINTERFACE_COMPORT
new Guid(0x86e0d1e0, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73),
// GUID_DEVINTERFACE_MODEM
new Guid(0x2c7089aa, 0x2e0e, 0x11d1, 0xb1, 0x14, 0x00, 0xc0, 0x4f, 0xc2, 0xaa, 0xe4),
// GUID_DEVINTERFACE_DISK
new Guid(0x53f56307, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b),
// GUID_DEVINTERFACE_HID,
new Guid(0x4D1E55B2, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30),
// GUID_NDIS_LAN_CLASS
new Guid(0xad498944, 0x762f, 0x11d0, 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c),
// GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
new Guid(0x4D36E978, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18),
// GUID_DEVINTERFACE_PARALLEL
new Guid(0x97F76EF0, 0xF883, 0x11D0, 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C),
// GUID_DEVINTERFACE_PARCLASS
new Guid(0x811FC6A5, 0xF728, 0x11D0, 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1)
};
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - USBHelper(windowHandle)
/// <summary>
/// 생성자
/// </summary>
/// <param name="windowHandle">윈도우 핸들</param>
public unsafe USBHelper(IntPtr windowHandle)
{
DEV_BROADCAST_HDR filter = new DEV_BROADCAST_HDR();
filter.Size = DEV_BROADCAST_HDR.DefaultSize;
filter.DeviceType = DBT_DEVTYP_DEVICEINTERFACE;
foreach(Guid guid in DEVINTERFACE_GUID_ARRAY)
{
filter.ClassGUID = guid;
DEV_BROADCAST_HDR* filterPointer = &filter;
IntPtr filterHandle = new IntPtr(filterPointer);
IntPtr deviceNotificationHandle = RegisterDeviceNotification(windowHandle, filterHandle, DEVICE_NOTIFY_WINDOW_HANDLE);
if(deviceNotificationHandle == IntPtr.Zero)
{
throw new InvalidOperationException("장치 통지 등록을 실패했습니다 : " + guid.ToString());
}
else
{
this.deviceNotificationHandleList.Add(deviceNotificationHandle);
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 리소스 해제하기 - Dispose()
/// <summary>
/// 리소스 해제하기
/// </summary>
public void Dispose()
{
foreach(IntPtr deviceNotificationHandle in this.deviceNotificationHandleList)
{
UnregisterDeviceNotification(deviceNotificationHandle);
}
}
#endregion
}
}
728x90
▶ MainForm.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// USB 헬퍼
/// </summary>
private USBHelper helper;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.helper = new USBHelper(Handle);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 닫을 경우 처리하기 - OnClosing(e)
/// <summary>
/// 닫을 경우 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnClosing(CancelEventArgs e)
{
if(this.helper != null)
{
this.helper.Dispose();
}
base.OnClosing(e);
}
#endregion
#region 윈도우 프로시저 처리하기 - WndProc(message)
/// <summary>
/// 윈도우 프로시저 처리하기
/// </summary>
/// <param name="message">메시지</param>
protected override void WndProc(ref Message message)
{
if(message.Msg == USBHelper.WM_DEVICECHANGE)
{
this.listBox.Items.Add(DateTime.Now.ToString("HH:mm:ss") + " : " + message.Msg + ", " + message.WParam.ToInt64() + ", " + message.LParam.ToInt64());
}
base.WndProc(ref message);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] NetworkInterface 클래스 : GetAllNetworkInterfaces 정적 메소드를 사용해 MAC 주소 구하기 (0) | 2019.08.16 |
---|---|
[C#/COMMON] 애플리케이션 제목 구하기 (0) | 2019.08.16 |
[C#/COMMON] StreamReader 클래스 : CurrentEncoding 속성을 사용해 텍스트 파일 인코딩 구하기 (0) | 2019.08.16 |
[C#/COMMON] MailMessage 클래스 : 다음 메일 보내기 (0) | 2019.08.03 |
[C#/COMMON] CancellationToken 클래스 : 작업 취소시키기 (0) | 2019.08.03 |
[C#/COMMON] WindowsIdentity 클래스 : 윈도우즈 로그인 계정 구하기 (0) | 2019.08.02 |
[C#/COMMON] 인증서 설치하기 (0) | 2019.08.02 |
[C#/COMMON] IMessageFilter 인터페이스 : WM_INPUT 메시지 처리하기 (0) | 2019.08.02 |
[C#/COMMON] 지정한 타입의 항목을 갖는 제네릭 리스트 구하기 (0) | 2019.08.01 |
[C#/COMMON] SetConsoleCtrlHandler API 함수를 사용해 콘솔 프로그램 CTRL+C 키 입력시 처리하기 (0) | 2019.08.01 |
댓글을 달아 주세요