今天热门
热点:

如何使用U盘内的程序安全删除U盘,u盘内程序


u盘 硬件 VC 我有一个U盘程序,在插入U盘点时候运行,在退出程序后自动或手动安全删除U盘。
目前的方法用winexec调用%windir%\system32\control.exe hotplug.dll。
但是实际是弹出来了系统的安全删除硬件的界面,然后本身应用程序退出。这个时候点删除U盘,提示删除失败。按说这个时候和U盘有关的进程已经都结束了,因为删除硬件使用的是系统的程序。难道是因为我本身的应用程序调用了control.exe导致没有释放??可是我自己的应用也已经退出来了。任务管理器中也没有了进程。
       各位,如何做才能达到我预期的目标。

解决方案

#include   <tchar.h>   
  #include   <stdio.h>   
  #include   <windows.h>   
  #include   <devguid.h>   
    
  #define   DWORD_PTR   DWORD   
  #define   ULONG_PTR   DWORD   
    
    
  extern   "C"   {     
  #include   "hidsdi.h"     
  }   
  //   需加入hid.lib   
    
  #include   <setupapi.h>   
  //   需加入setupapi.lib   
    
  #include   <regstr.h>   
  #include   <winbase.h>   
    
  #include   <cfgmgr32.h>   
  //   需要加入cfgmgr32.lib   
    
  #include   <initguid.h>   
  //#include   <usbiodef.h>   
  DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,   
      0xA5DCBF10L,   0x6530,   0x11D2,   0x90,   0x1F,   0x00,   0xC0,   0x4F,   0xB9,   0x51,   0xED);   
  #define   GUID_CLASS_USB_DEVICE                       GUID_DEVINTERFACE_USB_DEVICE   
    
  int   main(int   argc,   _TCHAR*   argv[])   
  {   
        HDEVINFO   hDevInfo;   
          
        SP_DEVINFO_DATA   DeviceInfoData;   
        DWORD   i;   
    
        //--------------------------------------------------------------------------   
        //   获取设备信息   
        hDevInfo   =   SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE,   
                0,   //   Enumerator   
                0,   
                DIGCF_PRESENT   |   DIGCF_DEVICEINTERFACE   );   
        if   (hDevInfo   ==   INVALID_HANDLE_VALUE)   {   
                //   查询信息失败   
                printf("ERROR   -   SetupDiGetClassDevs()");   
                return   1;   
        }   
        //--------------------------------------------------------------------------   
    
        //   枚举每个USB设备   
        DeviceInfoData.cbSize   =   sizeof(SP_DEVINFO_DATA);   
        for   (i=0;SetupDiEnumDeviceInfo(hDevInfo,   i,   &DeviceInfoData);i++)   
        {   
                  LPTSTR   buffer   =   NULL;   
                  PVOID   buffer2   =   NULL;   
                  DWORD   buffersize   =   0;   
                  ULONG   len;   
                  CONFIGRET       cr;   
                  PNP_VETO_TYPE   pnpvietotype;   
                  CHAR   vetoname[MAX_PATH];   
                  ULONG   ulStatus;   
                  ULONG   ulProblemNumber;   
    
                  cr   =   CM_Get_DevNode_Status(   &ulStatus,   
                                                                          &ulProblemNumber,   
                                                                          DeviceInfoData.DevInst,   
                                                                          0);   
                  if   (   CR_SUCCESS   ==   cr   )   {   
                          printf("OK   -   CM_Get_DevNode_Status()[%d]\n",   cr);   
                          printf("OK   -   CM_Get_DevNode_Status()   sts   [%x]\n",   ulStatus);   
                          printf("OK   -   CM_Get_DevNode_Status()   pro   [%x]\n",   ulProblemNumber);   
                  }   else   {   
                          printf("ERROR   -   CM_Get_DevNode_Status()[%d]\n",   cr);   
                          printf("ERROR   -   CM_Get_DevNode_Status()[%d]\n",   GetLastError());   
                  }   
                  //   DN_DISABLEABLE   or   DN_REMOVABLE   
                  if   ((DN_DISABLEABLE   &   ulStatus   )   !=   0   )   {   
                          printf("HAS   -   DN_DISABLEABLE()[%x]\n",   DN_DISABLEABLE   &   ulStatus);   
                  }   else   {   
                        continue;   
                  }   
                  if   ((DN_REMOVABLE   &   ulStatus   )   !=   0   )   {   
                          printf("HAS   -   DN_REMOVABLE()[%x]\n",   DN_REMOVABLE   &   ulStatus);   
                  }   else   {   
                        continue;   
                  }   
    
                  len   =   MAX_PATH;   
                  //   pnpvietotype   =   PNP_VetoDevice;     
                  cr   =   CM_Request_Device_Eject(   
                                                          DeviceInfoData.DevInst,   
                                                          &pnpvietotype,   
                                                          vetoname,   
                                                          len,   
                                                          0   
                                                          );   
                  if   (   CR_SUCCESS   ==   cr   )   {   
                          printf("OK   -   CM_Request_Device_Eject()[%d]\n",   cr);   
                  }   else   {   
                          printf("ERROR   -   CM_Request_Device_Eject()[%d]\n",   cr);   
                          printf("ERROR   -   CM_Request_Device_Eject()[%d]\n",   GetLastError());   
                  }   
    
        }   
                  
                  
        if   (   GetLastError()!=NO_ERROR   &&   
                  GetLastError()!=ERROR_NO_MORE_ITEMS   )   
        {   
                //   Insert   error   handling   here.   
                return   1;   
        }   
                  
        //     Cleanup   
        SetupDiDestroyDeviceInfoList(hDevInfo);   
    
        return   0;   
  }

这个是别人的一个答案。用的是WIN32的一些接口,其实就是列举设备,找到后弹出

因为你调用的系统界面进程还是属于你u盘里面的程序
一般的做法都是在电脑里另外运行一个程序来关闭u盘
你u盘里的程序可以给系统生成一个程序(或其它途径),这个程序一直在后台运行
检测到你u盘的时候就做一些其它工作, 你u盘的程序在打算关闭u盘的时候,给后台程序发送一条命令
让后台程序等待一段时间后关闭u盘

www.zrccd.nettrue/topics/20180106/6323.htmlTechArticle如何使用U盘内的程序安全删除U盘,u盘内程序 u盘 硬件 VC 我有一个U盘程序,在插入U盘点时候运行,在退出程序后自动或手动安全删除U盘。 目前的方法用winexec调用%windir%\system32\contro...

相关文章

    暂无相关文章

用户评论

大家都在看