RemoveUsb.cpp
上传用户:posgewe
上传日期:2022-07-05
资源大小:164k
文件大小:3k
- #include "stdafx.h"
- #include <windows.h>
- #include <winioctl.h>
- #include <process.h>
- #include <tchar.h>
- #include <stdio.h>
- #include "DoStorageEnum.h"
- #include <stdlib.h>
- #include <string.h>
- #define MAX_DEVICE 16
- DEFINE_GUID(DiskClassGuid, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- DEFINE_GUID(CdRomClassGuid, 0x53f56308L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(PartitionClassGuid, 0x53f5630aL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- DEFINE_GUID(TapeClassGuid, 0x53f5630bL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(WriteOnceDiskClassGuid, 0x53f5630cL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(VolumeClassGuid, 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(MediumChangerClassGuid, 0x53f56310L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(FloppyClassGuid, 0x53f56311L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(CdChangerClassGuid, 0x53f56312L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- //DEFINE_GUID(StoragePortClassGuid, 0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
- char * MakeDriveInfo(LPCTSTR pszDevicePath, PDISK_GEOMETRY pGeometry, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc);
- char * MakeDriveInfo(LPCTSTR pszDevicePath, PDISK_GEOMETRY pGeometry, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
- {
- char* p;
- char* strTmp;
- p= (char*)pDevDesc;
- strTmp = (pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset] : "");
- return strTmp;
- }
- /*-------------------------------------------------------------------------*/
- char* WINAPI GetDiskId(int index)
- {
- char* strInfo;
- HANDLE hDevice;
- PDISK_GEOMETRY pGeometry;
- PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
- int nDevice;
- int i,j;
- char* szDevicePath[MAX_DEVICE]; // 设备路径
- LPGUID lpGuid[] = {
- (LPGUID)&DiskClassGuid,
- (LPGUID)&CdRomClassGuid,
- // (LPGUID)&TapeClassGuid,
- }; // 一些存储设备的GUID
- // 分配需要的空间
- for(i=0;i<MAX_DEVICE;i++) szDevicePath[i]=new char[256];
- pGeometry = new DISK_GEOMETRY;
- pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
- pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
- // 对感兴趣的类型的存储设备,逐一获取设备信息
- for(i=0; i<sizeof(lpGuid)/sizeof(LPGUID); i++)
- {
- // 取设备路径
- nDevice=::GetDevicePath(lpGuid[i], szDevicePath);
- // 对同一类的存储设备,逐一获取设备信息
- for(j=0; j<nDevice; j++)
- {
- // 打开设备
- hDevice=OpenDevice(szDevicePath[j]);
- if(hDevice != INVALID_HANDLE_VALUE)
- {
- // 取设备信息
- GetDriveGeometry(hDevice, pGeometry);
- GetDriveProperty(hDevice, pDevDesc);
- // 制作显示信息串
- if(j==index)
- {
- strInfo = MakeDriveInfo(szDevicePath[j], pGeometry, pDevDesc);
- }
- // 关闭设备
- CloseHandle(hDevice);
- }
- }
- }
- // 释放空间
- delete pDevDesc;
- delete pGeometry;
- for(i=0;i<MAX_DEVICE;i++) delete []szDevicePath[i];
- return strInfo;
- }