- //重载MFC函数
- CString path="";//盘符如G:,F:
- LRESULT CUpanDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- switch(wParam){
- case DBT_DEVICEARRIVAL:
- if(isUDisk(lParam,path))
- if(path!=""){
- AfxMessageBox("u盘插入,盘符为"+path);
- }
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- if(isUDisk(lParam,path)){
- if(path!=""){
- AfxMessageBox("u盘删除,盘符为"+path);
- }
- }
- break;
- default:
- break;
- }
- return CDialog::DefWindowProc(message, wParam, lParam);
- }
- //we must add #include <DBT.H>
- //判断是否是U盘,是u盘得到u盘盘符
- bool CUpanDlg::isUDisk(LPARAM lParam,CString &path){//返回为真表示是U盘,同时path为盘符
- bool yes=false;
- path="";
- DEV_BROADCAST_HDR* dhr = (DEV_BROADCAST_HDR *)lParam;
- if(dhr->dbch_devicetype == DBT_DEVTYP_VOLUME){
- PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)dhr;
- if(lpdbv->dbcv_flags==0){//判断是U盘
- DWORD dwData=lpdbv->dbcv_unitmask;
- for(char i=0;i<26;++i){//得到盘符
- if(dwData&0x1){
- CString str=char(i+'A');
- path=str+_T(":");
- yes=true;
- break;
- }
- dwData=dwData>>1;
- if(dwData==0x0000)
- break;
- }
- }
- }
- return yes;
- }
- //弹出删除界面,安全删除u盘
- void CUpanDlg::OnButton1()
- {
- // TODO: Add your control notification handler code here
- char strSystemDirectory[256];
- GetSystemDirectory(strSystemDirectory, 256 );
- CString strTemp = strSystemDirectory;
- strTemp += "//rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";
- WinExec( strTemp, SW_SHOW );
- }
- //不弹出界面,直接安全删除u盘
- //we must add .h file ,#include <winioctl.h>
- bool CUpanDlg::uninstallUsb(CString vol_string){ //参数是要弹出的盘符如:G:,F:
- DWORD accessMode = 0, shareMode = 0;
- HANDLE hDevice;
- ULONG returned = 0,bResult = 0;
- DWORD dwError;
- shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; // default
- accessMode = GENERIC_WRITE | GENERIC_READ; // default
- if (vol_string == ""){
- return FALSE;
- }
- hDevice = CreateFile(vol_string,
- accessMode,shareMode,
- NULL, OPEN_EXISTING, 0,NULL);
- if (hDevice == INVALID_HANDLE_VALUE){
- CString debugInfo;
- dwError=GetLastError();
- debugInfo.Format("error = %d ", dwError);
- TRACE("IOCTL_STORAGE_EJECT_MEDIA errorcode = %d /n", dwError);
- AfxMessageBox("打开失败");
- return FALSE;
- }
- bResult=DeviceIoControl(hDevice,
- // IOCTL_STORAGE_LOAD_MEDIA,//加载u盘
- IOCTL_STORAGE_EJECT_MEDIA, //弹出U盘的具体命令。
- NULL, 0, NULL, 0,&returned,(LPOVERLAPPED)NULL);
- if(!bResult){//OCTL failed //经实验,有时虽然失败了,U盘还是能弹出
- CString debugInfo;
- dwError=GetLastError();
- debugInfo.Format("error = %d ", dwError);
- TRACE("IOCTL_STORAGE_EJECT_MEDIA errorcode = %d /n", dwError);
- AfxMessageBox("弹出失败");
- }
- bResult=CloseHandle(hDevice);
- if(!bResult){
- CString debugInfo;
- dwError=GetLastError();
- debugInfo.Format("error = %d ", dwError);
- TRACE("IOCTL_STORAGE_EJECT_MEDIA errorcode = %d /n", dwError);
- AfxMessageBox("关闭失败");
- return FALSE;
- }
- return TRUE;
- }
- //-------查找U盘-------------------------------
- CString CUpanDlg::findUsbDisk()
- {
- CString strdir="";
- for(char cc='A';cc<='Z';cc++)
- {
- strdir.Format("%c:",cc);
- if(GetDriveType((LPCTSTR)strdir)==DRIVE_REMOVABLE)//移动盘
- return strdir;
- }
- return strdir="";
- }
- void CUpanDlg::OnButton2()
- {
- // TODO: Add your control notification handler code here
- CString vol_string = ".//";
- vol_string+=findUsbDisk();
- AfxMessageBox(vol_string);
- uninstallUsb(vol_string);
- }