如何知道移动驱动器有没有插入软盘(譬如那种插入SD卡的 ...
如何知道移动驱动器有没有插入软盘(譬如那种插入SD卡的)? [转贴 2010-01-30 09:05:28]   
写程序的时候遇到一点困难,怎样得知驱动器中有没有插入盘?譬如插入SD卡的移动存储器?  

---------------------------------------------------------------  

 

你用这个函数试试DeviceIoControl:  

 

IOCTL_STORAGE_CHECK_VERIFY  

The  IOCTL_STORAGE_CHECK_VERIFY  DeviceIoControl  operation  determines  whether  a  device's  media  is  accessible;  for  example,  whether  the  media  is  in  the  device.    

 

BOOL  DeviceIoControl(  

    (HANDLE)  hDevice,                //  handle  to  device  of  interest  

    IOCTL_STORAGE_CHECK_VERIFY,    //  dwIoControlCode,  control  code  of  

                                                      //  operation  to  perform  

    NULL,                                        //  lpInBuffer  is  not  used;  must  be  NULL  

    0,                                              //  nInBufferSize  is  not  used;  must  be  zero  

    NULL,                                        //  lpOutBuffer  is  not  used;  must  be  NULL  

    0,                                              //  nOutBufferSize  is  not  used;  

                                                      //  must  be  zero  

    (LPDWORD)  lpBytesReturned,      //  pointer  to  variable  to  receive  

                                                      //  output  byte  count  

    (LPOVERLAPPED)  lpOverlapped    //  pointer  to  OVERLAPPED  structure  

                                                      //  for  asynchronous  operation  

);  

 

---------------------------------------------------------------  

 

bool  IsMediaExist(char  *Drive)  

{  

            char                        DeviceName[MAX_PATH];  

            HANDLE                        hDevice;  

            DWORD                        cbBytesAct  =  0;  

            bool                        fMediaExist  =  false;  

 

            sprintf(DeviceName,  "\\\\.\\%s",  Drive);  

 

            hDevice  =  CreateFile(  

                        DeviceName,  

                        GENERIC_READ    &brvbar  GENERIC_WRITE,  

                        FILE_SHARE_READ,  

                        NULL,  

                        OPEN_EXISTING,  

                        NULL,  

                        NULL);  

 

            if  (hDevice  !=  INVALID_HANDLE_VALUE)  {  

                        if  (DeviceIoControl(  

                                    hDevice,  

                                    IOCTL_DISK_CHECK_VERIFY,  

                                    NULL,  

                                    0,  

                                    NULL,  

                                    0,  

                                    &cbBytesAct,  

                                    NULL))  {  

                                    fMediaExist  =  true;  

                        }  

 

                        CloseHandle(hDevice);  

            }  

 

            return(  fMediaExist  );  

}
郑重声明:资讯 【如何知道移动驱动器有没有插入软盘(譬如那种插入SD卡的 ...】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——