U盘 插入/删除 监测

  1. //重载MFC函数  
  2. CString path="";//盘符如G:,F:  
  3. LRESULT CUpanDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)   
  4. {  
  5.  // TODO: Add your specialized code here and/or call the base class  
  6.    
  7.  switch(wParam){  
  8.  case DBT_DEVICEARRIVAL:  
  9.   if(isUDisk(lParam,path))  
  10.    if(path!=""){  
  11.     AfxMessageBox("u盘插入,盘符为"+path);  
  12.    }  
  13.   break;  
  14.  case DBT_DEVICEREMOVECOMPLETE:  
  15.   if(isUDisk(lParam,path)){  
  16.    if(path!=""){  
  17.     AfxMessageBox("u盘删除,盘符为"+path);  
  18.    }    
  19.   }  
  20.   break;  
  21.  default:  
  22.   break;  
  23.  }  
  24.  return CDialog::DefWindowProc(message, wParam, lParam);  
  25. }  
  26.   
  27. //we must add #include   <DBT.H>  
  28. //判断是否是U盘,是u盘得到u盘盘符  
  29. bool CUpanDlg::isUDisk(LPARAM lParam,CString &path){//返回为真表示是U盘,同时path为盘符  
  30.  bool yes=false;  
  31.  path="";  
  32.  DEV_BROADCAST_HDR* dhr = (DEV_BROADCAST_HDR *)lParam;  
  33.  if(dhr->dbch_devicetype == DBT_DEVTYP_VOLUME){  
  34.   PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)dhr;  
  35.   if(lpdbv->dbcv_flags==0){//判断是U盘    
  36.    DWORD dwData=lpdbv->dbcv_unitmask;  
  37.    for(char i=0;i<26;++i){//得到盘符  
  38.     if(dwData&0x1){  
  39.      CString str=char(i+'A');  
  40.      path=str+_T(":");  
  41.      yes=true;  
  42.      break;   
  43.     }  
  44.     dwData=dwData>>1;  
  45.     if(dwData==0x0000)  
  46.      break;  
  47.    }  
  48.   }  
  49.  }  
  50.  return yes;  
  51. }  
  52.   
  53. //弹出删除界面,安全删除u盘  
  54. void CUpanDlg::OnButton1()   
  55. {  
  56.  // TODO: Add your control notification handler code here  
  57.  char strSystemDirectory[256];   
  58.     GetSystemDirectory(strSystemDirectory, 256 );   
  59.   
  60.     CString strTemp = strSystemDirectory;   
  61.    strTemp += "//rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";   
  62.   
  63.     WinExec( strTemp, SW_SHOW );   
  64. }  
  65.   
  66. //不弹出界面,直接安全删除u盘  
  67. //we must add .h file ,#include <winioctl.h>  
  68. bool CUpanDlg::uninstallUsb(CString vol_string){ //参数是要弹出的盘符如:G:,F:  
  69.  DWORD accessMode = 0, shareMode = 0;  
  70.  HANDLE   hDevice;  
  71.  ULONG returned = 0,bResult = 0;  
  72.  DWORD   dwError;  
  73.  shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;  // default  
  74.  accessMode = GENERIC_WRITE | GENERIC_READ;       // default  
  75.  if (vol_string == ""){  
  76.   return FALSE;  
  77.  }  
  78.   
  79.  hDevice = CreateFile(vol_string,  
  80.   accessMode,shareMode,  
  81.   NULL, OPEN_EXISTING, 0,NULL);  
  82.  if (hDevice == INVALID_HANDLE_VALUE){  
  83.   CString debugInfo;  
  84.   dwError=GetLastError();   
  85.   debugInfo.Format("error = %d ", dwError);  
  86.   TRACE("IOCTL_STORAGE_EJECT_MEDIA  errorcode = %d /n", dwError);  
  87.   AfxMessageBox("打开失败");  
  88.   return FALSE;  
  89.  }  
  90.    
  91.  bResult=DeviceIoControl(hDevice,  
  92.  // IOCTL_STORAGE_LOAD_MEDIA,//加载u盘  
  93.   IOCTL_STORAGE_EJECT_MEDIA, //弹出U盘的具体命令。      
  94.   NULL, 0, NULL, 0,&returned,(LPOVERLAPPED)NULL);  
  95.  if(!bResult){//OCTL   failed     //经实验,有时虽然失败了,U盘还是能弹出  
  96.   CString debugInfo;  
  97.   dwError=GetLastError();   
  98.   debugInfo.Format("error = %d ", dwError);  
  99.   TRACE("IOCTL_STORAGE_EJECT_MEDIA  errorcode = %d /n", dwError);  
  100.   AfxMessageBox("弹出失败");  
  101.        }   
  102.  bResult=CloseHandle(hDevice);       
  103.  if(!bResult){  
  104.   CString debugInfo;  
  105.   dwError=GetLastError();       
  106.   debugInfo.Format("error = %d ", dwError);  
  107.   TRACE("IOCTL_STORAGE_EJECT_MEDIA  errorcode = %d /n", dwError);  
  108.   AfxMessageBox("关闭失败");  
  109.   return FALSE;  
  110.  }     
  111.  return TRUE;  
  112.   
  113. }  
  114.   
  115. //-------查找U盘-------------------------------  
  116. CString CUpanDlg::findUsbDisk()  
  117. {  
  118.  CString strdir="";  
  119.  for(char cc='A';cc<='Z';cc++)  
  120.  {  
  121.   strdir.Format("%c:",cc);  
  122.   if(GetDriveType((LPCTSTR)strdir)==DRIVE_REMOVABLE)//移动盘  
  123.        return strdir;  
  124.     }  
  125.     return strdir="";  
  126. }  
  127.   
  128.   
  129. void CUpanDlg::OnButton2()   
  130. {  
  131.  // TODO: Add your control notification handler code here  
  132.  CString vol_string = ".//";  
  133.  vol_string+=findUsbDisk();  
  134.  AfxMessageBox(vol_string);  
  135.  uninstallUsb(vol_string);  
  136. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值