#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 WndProc(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设备插入拔出 点击打开链接