|
本帖最后由 CodeHz 于 2020-11-10 16:53 编辑
- #include <windows.h>
- #include <stdio.h>
- char fetchbit(int mask, int idx) {
- return mask & (1 << idx) ? 'A' + idx : 0;
- }
- char *showtype(int res) {
- switch (res) {
- #define X(x) case DRIVE_##x: return #x
- X(UNKNOWN);
- X(NO_ROOT_DIR);
- X(REMOVABLE);
- X(FIXED);
- X(REMOTE);
- X(CDROM);
- X(RAMDISK);
- #undef X
- default: return "ERROR";
- }
- }
- char *busType(char c) {
- char volumeAccessPath[] = "\\\\.\\?:";
- volumeAccessPath[4] = c;
- HANDLE deviceHandle = CreateFileA(
- volumeAccessPath,
- 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL
- );
- STORAGE_PROPERTY_QUERY query = {};
- query.PropertyId = StorageDeviceProperty;
- query.QueryType = PropertyStandardQuery;
-
- DWORD bytes;
- STORAGE_DEVICE_DESCRIPTOR devd;
- STORAGE_BUS_TYPE busType = BusTypeUnknown;
-
- if (DeviceIoControl(deviceHandle, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), &devd, sizeof(devd), &bytes, NULL)) {
- busType = devd.BusType;
- } else {
- return "UNKNOWN";
- }
-
- CloseHandle(deviceHandle);
- switch (busType) {
- #define X(v, a) case v: return #a
- X(0x1, SCSI);
- X(0x2, ATAPI);
- X(0x3, ATA);
- X(0x4, 1394);
- X(0x5, SSA);
- X(0x6, FIBRE);
- X(0x7, USB);
- X(0x8, RAID);
- X(0x9, iSCSI);
- X(0xA, SAS);
- X(0xB, SATA);
- X(0xC, SD);
- X(0xD, MMC);
- X(0xE, VIRTUAL);
- X(0xF, FILE_BACKED_VIRTUAL);
- X(0x10, SPACES);
- #undef X
- default: return "UNKNOWN";
- }
- }
- int main() {
- int mask = GetLogicalDrives();
- char buf[] = "?:\";
- for (int i = 0; i < 26; i++) {
- char c = fetchbit(mask, i);
- if (!c) continue;
- buf[0] = c;
- char *type = showtype(GetDriveType(buf));
- char *usb = busType(c);
- printf("%c:\\ -> %s %s\n", c, type, usb);
- }
- }
复制代码 源码在这里,顺便改进了下,把更多的细节属性都暴露了
test.exe
(4.5 KB, 下载次数: 27)
|
评分
-
查看全部评分
|