一、查看是否有优盘插入
1 #define MESSAGEBOX_TITLE "标题"
2 #define PLEASE_INSERT_UDISK "请插入U盘以保存打印文件"
3 UsbDiskClass g_usbObject;
4 if ( g_usbObject.szMoveDiskName[1] == '\0' )
5 MessageBox(NULL, PLEASE_INSERT_UDISK, MESSAGEBOX_TITLE, MB_OK | MB_ICONWARNING);
二、检测优盘函数
1 /****************************************************************************
2 *
3 * FUNCTION: chFirstDriverFrameMask(ULONG unitmask)
4 *
5 * PURPOSE: get the logic name of driver
6 *
7 ****************************************************************************/
8 char chFirstDriveFromMask (ULONG unitmask)
9 {
10
11 char i;
12 for (i = 0; i < 26; ++i)
13 {
14 if (unitmask & 0x1)
15 break;
16 unitmask = unitmask >> 1;
17 }
18 return (i + 'A');
19 }
20
21 /****************************************************************************
22 *
23 * FUNCTION: ReInitUSB_Disk_Letter()
24 *
25 * PURPOSE: get the usb disks, and filling the 'szMoveDiskName' with them
26 *
27 ****************************************************************************/
28 // Drive type names
29 #define DRVUNKNOWN 0
30 #define DRVFIXED 1
31 #define DRVREMOTE 2
32 #define DRVRAM 3
33 #define DRVCD 4
34 #define DRVREMOVE 5
35
36 void UsbDiskClass::ReInitUSB_Disk_Letter()
37 {
38 int k = 0;
39 DWORD MaxDriveSet, CurDriveSet;
40 DWORD drive, drivetype;
41 TCHAR szBuf[300];
42 HANDLE hDevice;
43 PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
44
45 for(k=0; k<26; k++)
46 szMoveDiskName[k] = '\0';
47 k = 1;
48 // Get available drives we can monitor
49 MaxDriveSet = CurDriveSet = 0;
50 //test code
51 TCHAR TCFwp[50],TCF2[50];
52 MaxDriveSet = GetLogicalDrives();
53
54 //test code output-------------------------------------
55 wsprintf(TCFwp,_T("%ld"), MaxDriveSet);
56 MessageBox(NULL, TCFwp, TEXT("十进制的"), MB_OK); //十进制输出
57 _itot(MaxDriveSet, TCF2, 2); //_itot(MaxDriveSet, TCF2, 2); _UNICODE Defined
58 MessageBox(NULL, TCF2, TEXT("二进制的"), MB_OK); //二进制输出
59 //test code output-------------------------------------
60
61 CurDriveSet = MaxDriveSet;
62 for ( drive = 2/*0*/; drive < 32; ++drive )
63 {
64 if ( MaxDriveSet & (1 << drive) )
65 {
66 DWORD temp = 1<<drive;
67 TCHAR szDrvName[33];
68 _stprintf( szDrvName, _T("%c:\\"), 'A'+drive );
69 switch ( GetDriveType( szDrvName ) )
70 {
71 case DRIVE_REMOVABLE: // The drive can be removed from the drive.
72 drivetype = DRVREMOVE;
73 szMoveDiskName[k] = chFirstDriveFromMask(temp);
74 szMoveDiskName[0]=k;
75 k++;
76 break;
77 }
78 }
79 }
80
81 }
三、直接循环查找
1 CString strdir=_T("");
2 for(char cc='A';cc<='Z';cc++)
3 {
4 strdir.Format("%c:",cc);
5 if(GetDriveType((LPCTSTR)strdir)==DRIVE_REMOVABLE) //判断是不是U盘
6 return strdir;
7 }
8 return strdir=_T(""); //没找到就返回空
缺点:循环次数过多
有限次数查找:
1 char a = 'A;
2 CString DriveName;
3 DWORD dwDrives = GetLogicalDrives();
4 while (dwDrives > 0)
5 {
6 if (dwDrives % 2 == 1)
7 {
8 TCHAR szDrvName[33];
9 _stprintf( szDrvName, _T("%c:\\"), a );
10 if(GetDriveType((LPCTSTR)szDrvName)== DRIVE_REMOVABLE) //判断是不是U盘
11 return szDrvName;
12 }
13 a++;
14 dwDrives /= 2;
15 }
四、其他方法
转自:http://www.cnblogs.com/over140/archive/2010/05/03/1726263.html
前言
本文主要是讲在MFC中使用消息队列,并在消息中捕获系统USB插入、卸载U盘的事件。
正文
一、MFC中使用消息队列
1.1 声明
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
1.2 实现
LRESULT CTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
return CDialog::WindowProc(message,wParam,lParam);
}
本示例是继承CDialog。
二、捕获U盘插入、卸载事件
LRESULT CTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
//WM_DEVICECHANGE,系统硬件改变发出的系统消息
case WM_DEVICECHANGE:
{
PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
switch(wParam)
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://DBT_DEVICEARRIVAL,设备检测结束,并且可以使用
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
TRACE1("检测到U盘:[%s]插入!\n",decDriver.GetBuffer(0));
}
break;
case DBTF_MEDIA: //光盘
TRACE1("检测到光盘:[%c]插入!\n",FirstDriveFromMask(lpdbv ->dbcv_unitmask));
break;
}
}
}
break;
case DBT_DEVICEREMOVECOMPLETE://DBT_DEVICEREMOVECOMPLETE,设备卸载或者拔出
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
TRACE1("检测到U盘:[%s]拔出!\n",decDriver.GetBuffer(0));
}
break;
case DBTF_MEDIA: //光盘
break;
}
}
}
break;
}
}
break;
}
return CDialog::WindowProc(message,wParam,lParam);
}
posted on 2012-03-14 14:01
王海光 阅读(1084)
评论(0) 编辑 收藏 引用 所属分类:
MFC