java弹出U盘
功能描述:java調(diào)用exe,根據(jù)檢測到的盤符進(jìn)行U盤彈出。? ?
?exe地址:https://download.csdn.net/download/weixin_38959210/14503393
cmd調(diào)用,模擬要彈出的U盤盤符為E
自動彈出U盤.exe Ejava調(diào)用,模擬要彈出的U盤盤符為E
public static void main(String[] args) throws IOException {String[] cmd = {"D:\\自動彈出U盤.exe","E"};Runtime.getRuntime().exec(cmd);}C語言寫的執(zhí)行文件?也就是上面的exe 。推薦簡單的代碼編寫工具Dev-C++?以及?CodeBlocks
#include <windows.h> #include <dbt.h> #include <stdio.h> #include <math.h>#define LOCK_TIMEOUT 10000 // 10 Seconds #define LOCK_RETRIES 20 /*************** Function: UninstallUsb Description: 根據(jù)盤符將usb設(shè)備彈出 Parameters: discId 要彈出的盤符 Returns: -1 失敗 0 成功 ***************/ static int UninstallUsb(char *discId) {DWORD accessMode = GENERIC_WRITE | GENERIC_READ;DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;HANDLE hDevice;long bResult = 0;DWORD retu = 0;DWORD dwError;DWORD dwBytesReturned;DWORD dwSleepAmount;int nTryCount;char szDriv[10];if(discId == NULL){return 0;}dwSleepAmount = LOCK_TIMEOUT/LOCK_RETRIES;sprintf(szDriv,"\\\\.\\%s:",discId);hDevice = CreateFile(szDriv,accessMode,shareMode,NULL,OPEN_EXISTING,0,NULL);if(hDevice == INVALID_HANDLE_VALUE){printf("uninstallusb createfile failed error:%d\n",GetLastError());return -1;} #if 0//此循環(huán)是用于鎖定要彈出的U盤設(shè)備,如果U盤在使用,則循環(huán)等待// Do this in a loop until a timeout period has expiredfor(nTryCount = 0;nTryCount < LOCK_RETRIES;nTryCount++){if(DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&dwBytesReturned,NULL)){break;}}//卸載U盤卷,不論是否在使用dwBytesReturned = 0;if(!DeviceIoControl(hDevice,FSCTL_DISMOUNT_VOLUME,NULL,0,NULL,0,&dwBytesReturned,NULL)){printf("deviceIoConrol FSCTL_DISMOUNT_VOLUME failed\n");} #endifdwBytesReturned = 0;PREVENT_MEDIA_REMOVAL PMRBuffer;PMRBuffer.PreventMediaRemoval = FALSE;if(!DeviceIoControl(hDevice,IOCTL_STORAGE_MEDIA_REMOVAL,&PMRBuffer,sizeof(PREVENT_MEDIA_REMOVAL),NULL,0,&dwBytesReturned,NULL)){printf("DeviceIoControl IOCTL_STORAGE_MEDIA_REMOVAL failed:%d\n",GetLastError());}bResult = DeviceIoControl(hDevice,IOCTL_STORAGE_EJECT_MEDIA,NULL,0,NULL,0,&retu,NULL);if(!bResult){CloseHandle(hDevice);printf("uninstallusb DeviceIoControl failed error:%d\n",GetLastError());return -1;}CloseHandle(hDevice);return 0; }int main(int argc, char ** argv) {int i;for (i=1; i < argc; i++)UninstallUsb(argv[1]);return 0; }?
總結(jié)
- 上一篇: Layui中设置公共的全局变量接口
- 下一篇: 平安好车主APP如何在线微信支付