检测USB设备的插入和拔出
2017-04-24 18:33
471 查看
检测USB设备的插入和拔出
Win32消息处理API---BroadcastSystemMessage、BroadcastSystemMessageEx
// 例一,这段代码用于检测CD-ROM 中光盘的状态
#include<windows.h>
#include<dbt.h>
#include<strsafe.h>
// 函数声明
void Main_OnDeviceChange(HWND hwnd, WPARAM wParam, LPARAM lParam);
// 函数声明
char FirstDriveFromMask(ULONG unitmask);
// 功能说明 Handles WM_DEVICECHANGE messages sent to the application's top-level window.
void Main_OnDeviceChange(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
char szMsg[80];
switch (wParam)
{
case DBT_DEVICEARRIVAL:
// Check whether a CD or DVD was inserted into a drive.
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags & DBTF_MEDIA)
{
StringCchPrintf(szMsg, 80, _T("Drive %c: Media has arrived./n"),
FirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hwnd, szMsg, _T("WM_DEVICECHANGE"), MB_OK);
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
// Check whether a CD or DVD was removed from a drive.
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags & DBTF_MEDIA)
{
StringCchPrintf(szMsg, 80, _T("Drive %c: Media was removed./n"),
FirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hwnd, szMsg, _T("WM_DEVICECHANGE"), MB_OK);
}
}
break;
default: // 处理其余WM_DEVICECHANGE 事件
break;
}
}
// 功能说明 Finds the first valid drive letter from a mask of drive letters. The mask must be in the
// format bit 0 = A, bit 1 = B, bit 3 = C, etc. A valid drive letter is defined when the corresponding
// bit is set to 1.
// Returns the first drive letter that was found.
char FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1) break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
MFC 如何检测到USB设备插入拔出
点击打开链接
#include "stdafx.h" #include <Windows.h> /* LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DEVICECHANGE: printf("USB Device!\r\n"); break; default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { TCHAR szClassName[] = _T("MyApp"); WNDCLASS wndcls = { 0 }; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION); wndcls.lpfnWndProc = WndProc; wndcls.lpszClassName = szClassName; if (!RegisterClass(&wndcls)) { printf("RegisterClass Failed!\r\n"); return 0; } HWND hWnd = CreateWindow(szClassName, szClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if (NULL == hWnd) { printf("CreateWindow Failed!\r\n"); return 0; } ShowWindow(hWnd, SW_HIDE); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } */ #include <tchar.h> #include <string> #include <iostream> #include <Windows.h> #include <strsafe.h> #pragma warning(disable:4996) #include <dbt.h> /*------------------------------------------------------------------ FirstDriveFromMask( unitmask ) Description Finds the first valid drive letter from a mask of drive letters. The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C, and so on. A valid drive letter is defined when the corresponding bit is set to 1. Returns the first drive letter that was found. --------------------------------------------------------------------*/ char FirstDriveFromMask(ULONG unitmask) { char i; for (i = 0; i < 26; ++i) { if (unitmask & 0x1) break; unitmask = unitmask >> 1; } return(i + 'A'); } LRESULT CALLBACK Wnd bb93 Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; TCHAR szMsg[80]; char driveName; switch (uMsg) { case WM_DEVICECHANGE: switch (wParam) { case DBT_DEVICEARRIVAL: driveName = FirstDriveFromMask(lpdbv->dbcv_unitmask); sprintf(szMsg, "USB Drive %c: has inserted.\n", driveName); printf("%s\r\n", szMsg); MessageBox(hWnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK); break; case DBT_DEVICEREMOVECOMPLETE: driveName = FirstDriveFromMask(lpdbv->dbcv_unitmask); sprintf(szMsg, "USB Drive %c: has removed.\n", driveName); printf("%s\r\n", szMsg); MessageBox(hWnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK); break; default: ; } break; default: ; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { TCHAR szClassName[] = _T("MyApp"); WNDCLASS wndcls = { 0 }; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION); wndcls.lpfnWndProc = WndProc; wndcls.lpszClassName = szClassName; if (!RegisterClass(&wndcls)) { printf("RegisterClass Failed!\r\n"); return 0; } HWND hWnd = CreateWindow(szClassName, szClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if (NULL == hWnd) { printf("CreateWindow Failed!\r\n"); return 0; } ShowWindow(hWnd, SW_HIDE); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
Win32消息处理API---BroadcastSystemMessage、BroadcastSystemMessageEx
// 例一,这段代码用于检测CD-ROM 中光盘的状态
#include<windows.h>
#include<dbt.h>
#include<strsafe.h>
// 函数声明
void Main_OnDeviceChange(HWND hwnd, WPARAM wParam, LPARAM lParam);
// 函数声明
char FirstDriveFromMask(ULONG unitmask);
// 功能说明 Handles WM_DEVICECHANGE messages sent to the application's top-level window.
void Main_OnDeviceChange(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
char szMsg[80];
switch (wParam)
{
case DBT_DEVICEARRIVAL:
// Check whether a CD or DVD was inserted into a drive.
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags & DBTF_MEDIA)
{
StringCchPrintf(szMsg, 80, _T("Drive %c: Media has arrived./n"),
FirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hwnd, szMsg, _T("WM_DEVICECHANGE"), MB_OK);
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
// Check whether a CD or DVD was removed from a drive.
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags & DBTF_MEDIA)
{
StringCchPrintf(szMsg, 80, _T("Drive %c: Media was removed./n"),
FirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hwnd, szMsg, _T("WM_DEVICECHANGE"), MB_OK);
}
}
break;
default: // 处理其余WM_DEVICECHANGE 事件
break;
}
}
// 功能说明 Finds the first valid drive letter from a mask of drive letters. The mask must be in the
// format bit 0 = A, bit 1 = B, bit 3 = C, etc. A valid drive letter is defined when the corresponding
// bit is set to 1.
// Returns the first drive letter that was found.
char FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1) break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
MFC 如何检测到USB设备插入拔出
点击打开链接
相关文章推荐
- USB学习系列之二——USB设备的插入检测机制
- 两种检测USB设备插入和移除的方法(Ver 2)
- USB设备的插入检测机制(转自…
- MFC 如何检测到USB设备插入拔出
- vc检测USB设备插入或移除
- USB设备的插入检测
- USB主机是如何检测到设备的插入的呢?
- USB入门系列之五 —— USB设备的插入检测机制
- USB入门系列之五 —— USB设备的插入检测机制[图]
- 一个很有用的检测USB设备插入拔出的对象(from Torry's Dephi Pages)
- USB设备的插入检测
- USB驱动程序代码分析——检测设备插入拔出
- USB如何检测到设备的插入-usb技术入门教程1
- USB入门系列之五 —— USB设备的插入检测机制【转】
- Android有两种方法检测USB设备插入
- 一个很有用的检测USB设备插入拔出的对象
- 检测USB设备的插入和拔出
- USB主机是如何检测到设备的插入的呢?
- USB设备的插入检测机制
- 检测USB设备插入和拔出