Windows   发布时间:2022-05-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Windows7 – 32位系统上获取有关磁盘驱动器结果的信息大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在 Windows XP – 32上运行以下代码时,我得到以下结果.

我只插了一个usb笔式驱动器

On Windows XP - 32 Bit
-----------------------

Drive C:
    ProductId: ST3160215AS
    ProductRevision: 3.AAD
    DeviCEType: 7,Devicenumber: 0,Partitionnumber: 1
    DevicePath: \\?\ide#diskst3160215as_____________________________3.aad___#5&2
7db0ed4&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskST3160215AS_____________________________3.AAD___
    Friendly Name: ST3160215AS
    Physical Device Object Name: \Device\Ide\IdeDeviceP2T0L0-5
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&1C1E8A11&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_72671462&R
EV_01\3&11583659&0&FA
    DeviceInstancEID: IDE\DISKST3160215AS_____________________________3.AAD___\5
&27DB0ED4&0&0.0.0
Drive D:
    ProductId: ST3160215AS
    ProductRevision: 3.AAD
    DeviCEType: 7,Partitionnumber: 2
    DevicePath: \\?\ide#diskst3160215as_____________________________3.aad___#5&2
7db0ed4&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskST3160215AS_____________________________3.AAD___
    Friendly Name: ST3160215AS
    Physical Device Object Name: \Device\Ide\IdeDeviceP2T0L0-5
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&1C1E8A11&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_72671462&R
EV_01\3&11583659&0&FA
    DeviceInstancEID: IDE\DISKST3160215AS_____________________________3.AAD___\5
&27DB0ED4&0&0.0.0
Drive E:
    ProductId: ST3160215AS
    ProductRevision: 3.AAD
    DeviCEType: 7,Partitionnumber: 3
    DevicePath: \\?\ide#diskst3160215as_____________________________3.aad___#5&2
7db0ed4&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskST3160215AS_____________________________3.AAD___
    Friendly Name: ST3160215AS
    Physical Device Object Name: \Device\Ide\IdeDeviceP2T0L0-5
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&1C1E8A11&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_72671462&R
EV_01\3&11583659&0&FA
    DeviceInstancEID: IDE\DISKST3160215AS_____________________________3.AAD___\5
&27DB0ED4&0&0.0.0
Drive F:
    ProductId: ST3160215AS
    ProductRevision: 3.AAD
    DeviCEType: 7,Partitionnumber: 4
    DevicePath: \\?\ide#diskst3160215as_____________________________3.aad___#5&2
7db0ed4&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskST3160215AS_____________________________3.AAD___
    Friendly Name: ST3160215AS
    Physical Device Object Name: \Device\Ide\IdeDeviceP2T0L0-5
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&1C1E8A11&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_72671462&R
EV_01\3&11583659&0&FA
    DeviceInstancEID: IDE\DISKST3160215AS_____________________________3.AAD___\5
&27DB0ED4&0&0.0.0
Drive G:
    VendorId: jetFlash
    ProductId: Transcend 2GB
    ProductRevision: 8.07
    DeviCEType: 7,Devicenumber: 1,Partitionnumber: 1
    DevicePath: \\?\usbstor#disk&ven_jetflash&prod_transcend_2gb&rev_8.07#e3o29u
13&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\00000080
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&2B6971CE&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive G: is removeable
Drive X:

When i run the below code on windows7 - 32,i get the below result

i have plugged the same usb pen drive which is used on windows xp - 32 bit...

On Windows 7 - 32 Bit
-----------------------

Drive C:
    ProductId: Hitachi HDS721616PLA380
    ProductRevision: P22OAB3A
    DeviCEType: 7,Partitionnumber: 2
    DevicePath: \\?\ide#diskhitachi_hds721616pla380_________________p22oab3a#5&4
be38f&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\0000006f
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&4E31217&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive C: is removeable
Drive D:
    ProductId: Hitachi HDS721616PLA380
    ProductRevision: P22OAB3A
    DeviCEType: 7,Partitionnumber: 3
    DevicePath: \\?\ide#diskhitachi_hds721616pla380_________________p22oab3a#5&4
be38f&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\0000006f
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&4E31217&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive D: is removeable
Drive E:
    ProductId: Hitachi HDS721616PLA380
    ProductRevision: P22OAB3A
    DeviCEType: 7,Partitionnumber: 4
    DevicePath: \\?\ide#diskhitachi_hds721616pla380_________________p22oab3a#5&4
be38f&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\0000006f
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&4E31217&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive E: is removeable
Drive F:
    ProductId: Hitachi HDS721616PLA380
    ProductRevision: P22OAB3A
    DeviCEType: 7,Partitionnumber: 5
    DevicePath: \\?\ide#diskhitachi_hds721616pla380_________________p22oab3a#5&4
be38f&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\0000006f
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&4E31217&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive F: is removeable
Drive G:
    VendorId: jetFlash
    ProductId: Transcend 2GB
    ProductRevision: 8.07
    DeviCEType: 7,Partitionnumber: 1
    DevicePath: \\?\usbstor#disk&ven_jetflash&prod_transcend_2gb&rev_8.07#e3o29u
13&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskHitachi_HDS721616PLA380_________________P22OAB3A
    Friendly Name: Hitachi HDS721616PLA380 ATA Device
    Physical Device Object Name: \Device\Ide\IdeDeviceP1T0L0-1
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&35CD87E&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_27C08086&R
EV_01\3&2411E6FE&1&FA
    DeviceInstancEID: IDE\DISKHITACHI_HDS721616PLA380_________________P22OAB3A\5
&4BE38F&0&0.0.0
    Drive G: is removeable
Drive Z:
---------------------------------------------------------------------------------

请看两个结果……

我在两个系统(xp和windows7)上插入了一个相同的USB设备,但结果各不相同……

请检查驱动器G:两者的结果……

Drive G: ( On Windows 7 - 32 bit )
-----------------------------------

VendorId: jetFlash
    ProductId: Transcend 2GB
    ProductRevision: 8.07
    DeviCEType: 7,Partitionnumber: 1
    DevicePath: \\?\usbstor#disk&ven_jetflash&prod_transcend_2gb&rev_8.07#e3o29u
13&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: IDE\DiskHitachi_HDS721616PLA380_________________P22OAB3A
    Friendly Name: Hitachi HDS721616PLA380 ATA Device
    Physical Device Object Name: \Device\Ide\IdeDeviceP1T0L0-1
    Device Description: Disk drive
    Parent Device Instance ID: PCIIDE\IDECHAnnEL\4&35CD87E&0&0
    Parent of Parent Device Instance ID: PCI\VEN_8086&dEV_27C0&SUBSYS_27C08086&R
EV_01\3&2411E6FE&1&FA
    DeviceInstancEID: IDE\DISKHITACHI_HDS721616PLA380_________________P22OAB3A\5
&4BE38F&0&0.0.0
    Drive G: is removeable

Drive G: ( On Windows XP - 32 bit )
-----------------------------------
VendorId: jetFlash
    ProductId: Transcend 2GB
    ProductRevision: 8.07
    DeviCEType: 7,Partitionnumber: 1
    DevicePath: \\?\usbstor#disk&ven_jetflash&prod_transcend_2gb&rev_8.07#e3o29u
13&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: DiskDrive
    Hardware ID: USBSTOR\DiskjetFlashTranscend_2GB___8.07
    Friendly Name: jetFlash Transcend 2GB USB Device
    Physical Device Object Name: \Device\00000080
    Device Description: Disk drive
    Parent Device Instance ID: USB\VID_@R_197_11226@F&PID_6387\E3O29U13
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&2B6971CE&0
    DeviceInstancEID: USBSTOR\DISK&VEN_jeTFLASH&PROD_TRANSCEND_2GB&REV_8.07\E3O2
9U13&0
    Drive G: is removeable

感谢您的任何帮助

解决方法

我在C中编写了一个程序,演示了如何接收所需的信息

#include <windows.h>
#include <devguid.h>    // for GUID_DEVclass_cDROM etc
#include <setupapi.h>
#include <cfgmgr32.h>   // for MAX_DEVICE_ID_LEN,CM_Get_Parent and CM_Get_Device_ID
#include <tchar.h>
#include <stdio.h>

#define ARRAY_SIZE(arr)     (sizeof(arr)/sizeof(arr[0]))

#pragma comment (lib,"setupapi.lib")

//
// Define the varIoUs device characteristics flags (defined in wdm.h)
//
#define FILE_REMOVABLE_MEDIA                    0x00000001
#define FILE_READ_ONLY_DEVICE                   0x00000002
#define FILE_FLOPPY_DISKETTE                    0x00000004
#define FILE_WRITE_ONCE_MEDIA                   0x00000008
#define FILE_REMOTE_DEVICE                      0x00000010
#define FILE_DEVICE_IS_MOUNTED                  0x00000020
#define FILE_VIRTUAL_VOLUME                     0x00000040
#define FILE_AUTOGENERATED_DEVICE_NAME          0x00000080
#define FILE_DEVICE_SECURE_OPEN                 0x00000100
#define FILE_CHARACTERISTIC_PNP_DEVICE          0x00000800
#define FILE_CHARACTERISTIC_TS_DEVICE           0x00001000
#define FILE_CHARACTERISTIC_WEBDAV_DEVICE       0x00002000

#pragma warning (disable: 4201)
typedef struct _IO_STATUS_BLOCK {
    union {
        NTSTATUS Status;
        PVOID Pointer;
    } DUMMYUNIONNAME;

    ULONG_PTR Information;
} IO_STATUS_BLOCK,*PIO_STATUS_BLOCK;
#pragma warning (default: 4201)

typedef enum _FSINFOCLASS {
    FileFsVolumeInformation       = 1,FileFsLabelInformation,// 2
    FileFsSizeInformation,// 3
    FileFsDeviceInformation,// 4
    FileFsAttributeInformation,// 5
    FileFsControlInformation,// 6
    FileFsFullSizeInformation,// 7
    FileFsObjectIdInformation,// 8
    FileFsDriverPathInformation,// 9
    FileFsVolumeFlagsInformation,// 10
    FileFsmaximumInformation
} FS_INFORMATION_CLASS,*PFS_INFORMATION_CLASS;

typedef struct _FILE_FS_DEVICE_INFORMATION {
    DEVICE_TYPE DeviCEType;
    ULONG characteristics;
} FILE_FS_DEVICE_INFORMATION,*PFILE_FS_DEVICE_INFORMATION;

typedef NTSTATUS (NTAPI *LPFN_NT_QUERY_VOLUME_INFORMATION_FILE) (HANDLE FileHandle,PIO_STATUS_BLOCK IoStatusBlock,PVOID FsInformation,ULONG Length,FS_INFORMATION_CLASS FsInformationClass);

BOOL GetDriveTypeAndcharacteristics (HANDLE hDevice,DEVICE_TYPE *pDeviCEType,ULONG *pcharacteristics)
{
    HMODULE hNtDll;
    LPFN_NT_QUERY_VOLUME_INFORMATION_FILE lpfnNtQueryVolumeInformationFile;
    NTSTATUS ntStatus;
    IO_STATUS_BLOCK IoStatusBlock;
    FILE_FS_DEVICE_INFORMATION FileFsDeviceInfo;
    BOOL bsuccess = falSE;

    hNtDll = GetModuleHandle (TEXT("ntdll.dll"));
    if (hNtDll == NULL)
        return falSE;

    lpfnNtQueryVolumeInformationFile = (LPFN_NT_QUERY_VOLUME_INFORMATION_FILE)GetProcAddress (hNtDll,"NtQueryVolumeInformationFile");
    if (lpfnNtQueryVolumeInformationFile == NULL)
        return falSE;

    ntStatus = lpfnNtQueryVolumeInformationFile (hDevice,&IoStatusBlock,&FileFsDeviceInfo,sizeof(FileFsDeviceInfo),FileFsDeviceInformation);
    if (ntStatus == NO_ERROR) {
        bsuccess = TRUE;
        *pDeviCEType = FileFsDeviceInfo.DeviCEType;
        *pcharacteristics = FileFsDeviceInfo.characteristics;
    }

    return bsuccess;
}

void FildVolumename (LPCTSTR pszdeviceName)
{
    TCHAR szVolumename[MAX_PATH] = TEXT("");
    TCHAR szdeviceName[MAX_PATH] = TEXT("");
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwCharCount;
    BOOL bsuccess;

    hFind = FindFirstVolume (szVolumename,ARRAYSIZE(szVolumeName));
    if (hFind == INVALID_HANDLE_value) return;

    while(true) {
        //  Skip the \\?\ prefix and remove the Trailing BACkslash.
        size_t Index = lstrlen(szVolumeName) - 1;
        if (szVolumename[0]     != TEXT('\\') ||
            szVolumename[1]     != TEXT('\\') ||
            szVolumename[2]     != TEXT('?')  ||
            szVolumename[3]     != TEXT('\\') ||
            szVolumename[Index] != TEXT('\\')) return; // error

        //  QueryDosDeviceW doesn't allow a Trailing BACkslash,//  so temporarily remove it.
        szVolumename[Index] = TEXT('\0');
        dwCharCount = QueryDosDevice (&szVolumename[4],szdeviceName,ARRAYSIZE(szdeviceName));
        szVolumename[Index] = TEXT('\\');
        if (dwCharCount == 0) return; // error

        if (lstrcmp (pszdeviceName,szdeviceName) == 0) {
            _tprintf (TEXT("    Volume Device Name: %s\n"),szVolumeName);
            return;
        }

        bsuccess = FindNextVolume (hFind,szVolumename,ARRAYSIZE(szVolumeName));
        if (!bsuccess) {
            DWORD dwErrorCode = GetLastError();
            if (dwErrorCode == ERROR_NO_MORE_ITEMS)
                break;
            else 
                break;  // ERROR!!!
        }
    }
}

void DumpVidPidMi (LPCTSTR pszDeviceInstancEID)
{
    TCHAR szDeviceInstancEID[MAX_DEVICE_ID_LEN];
    const static LPCTSTR arPrefix[3] = {TEXT("VID_"),TEXT("PID_"),TEXT("MI_")};
    LPTSTR pszToken,pszNextToken;
    int j;

    lstrcpy (szDeviceInstancEID,pszDeviceInstancEID);

    pszToken = _tcstok_s (szDeviceInstancEID,TEXT("\\#&"),&pszNextToken);
    while(pszToken != NULL) {
        for (j = 0; j < 3; j++) {
            if (_tcsncmp(pszToken,arPrefix[j],lstrlen(arPrefix[j])) == 0) {
                switch(j) {
                    case 0:
                        _tprintf (TEXT("        vid: \"%s\"\n"),pszToken + lstrlen(arPrefix[j]));
                        break;
                    case 1:
                        _tprintf (TEXT("        pid: \"%s\"\n"),pszToken + lstrlen(arPrefix[j]));
                        break;
                    case 2:
                        _tprintf (TEXT("        mi: \"%s\"\n"),pszToken + lstrlen(arPrefix[j]));
                        break;
                    default:
                        break;
                }
            }
        }
        pszToken = _tcstok_s (NULL,&pszNextToken);
    }
}

BOOL FindDiInfos (LPCGUID pGuidInferface,LPCGUID pGuidClass,LPCTSTR pszEnumerator,DEVICE_TYPE DeviCEType,DWORD Devicenumber,DWORD dwDeviceInstancEIDSize,// MAX_DEVICE_ID_LEN
                  OUT LPTSTR pszDeviceInstancEID,OUT PDWORD pdwRemovalPolicy)
//#define CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL             1
//#define CM_REMOVAL_POLICY_EXPECT_ORDERLY_REMOVAL        2
//#define CM_REMOVAL_POLICY_EXPECT_SURPRISE_REMOVAL       3
{
    HDEVINFO hIntDevInfo = NULL;
    DWORD dwIndex;
    BOOL bFound = falSE; 
    HANDLE hDev = INVALID_HANDLE_VALUE;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = NULL;

    // set defaults
    *pdwRemovalPolicy = 0;
    pszDeviceInstancEID[0] = TEXT('\0');

    __try {
        hIntDevInfo = SetupDiGetClassDevs (pGuidInferface,pszEnumerator,NULL,pGuidInferface != NULL? DIGCF_PRESENT | DIGCF_DEVICEINTERFACE:
                                                                   DIGCF_ALLCLASSES | DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
        if (hIntDevInfo == INVALID_HANDLE_value)
            __leave;

        for (dwIndex = 0; ;dwIndex ++) {
            SP_DEVICE_INTERFACE_DATA interfaceData;
            SP_DEVINFO_DATA deviceInfoData;
            DWORD dwDataType,dwrequiredSize;
            BOOL bsuccess;

            ZeroMemory (&interfaceData,sizeof(interfaceData));
            interfaceData.cbSize = sizeof(interfaceData);
            bsuccess = SetupDiEnumDeviceInterfaces (hIntDevInfo,pGuidInferface,dwIndex,&interfaceData);
            if (!bsuccess) {
                DWORD dwErrorCode = GetLastError();
                if (dwErrorCode == ERROR_NO_MORE_ITEMS)
                    break;
                else 
                    break;  // ERROR!!!
            }

            dwrequiredSize = 0;
            bsuccess = SetupDiGetDeviceInterfaceDetail (hIntDevInfo,&interfaceData,&dwrequiredSize,null);
            if ((!bsuccess && GetLastError() != ERROR_INSUFFICIENT_BUFFER) || dwrequiredSize == 0)
                conTinue;  // ERROR!!!

            if (pInterfaceDetailData)
                pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalFree (pInterfaceDetailData);

            pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc (LPTR,dwrequiredSizE);
            pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
            ZeroMemory (&deviceInfoData,sizeof(deviceInfoData));
            deviceInfoData.cbSize = sizeof(deviceInfoData);
            bsuccess = SetupDiGetDeviceInterfaceDetail (hIntDevInfo,pInterfaceDetailData,dwrequiredSize,&deviceInfoData);
            if (!bsuccess)
                conTinue;

            hDev = CreateFile (pInterfaceDetailData->DevicePath,// no access to the drive
                               FILE_SHARE_READ | FILE_SHARE_WRITE,// share mode
                               NULL,// default security attributes
                               OPEN_EXISTinG,// disposition
                               0,// file attributes
                               null);                               // do not copy file attributes
            if (hDev != INVALID_HANDLE_value) {
                STORAGE_DEVICE_numbER sdn;
                DWORD cbBytesReturned;
                bsuccess = DeviceIoControl (hDev,// device to be queried
                                            IOCTL_STORAGE_GET_DEVICE_numbER,// no input buffer
                                            (LPVOID)&sdn,sizeof(sdn),// output buffer
                                            &cbBytesReturned,// # bytes returned
                                            (LPOVERLAPPED) null);           // synchronous I/O
                if (bsuccess) {
                    if (sdn.DeviCEType == DeviCEType &&
                        sdn.Devicenumber == Devicenumber) {

                        DEVINST dnDevInstParent,dnDevInstParentParent;
                        CONfigRET ret;
                        // device found !!!
                        TCHAR szBuffer[4096];

                        _tprintf (TEXT("    DevicePath: %s\n"),pInterfaceDetailData->DevicePath);

                        bsuccess = SetupDiGetDeviceInstancEID (hIntDevInfo,&deviceInfoData,pszDeviceInstancEID,dwDeviceInstancEIDSize,&dwrequiredSizE);
                        if (dwrequiredSize > MAX_DEVICE_ID_LEN)
                            conTinue;

                        bsuccess = SetupDiGetDeviceRegistryProperty (hIntDevInfo,SPDRP_REMOVAL_POLICY,&dwDataType,(PBYTE)pdwRemovalPolicy,sizeof(DWORD),&dwrequiredSizE);
                        // SPDRP_characteristics - Device characteristics
                        //         FILE_FLOPPY_DISKETTE,FILE_REMOVABLE_MEDIA,and FILE_WRITE_ONCE_MEDIA characteristics 
                        //         FILE_READ_ONLY_DEVICE
                        bsuccess = SetupDiGetDeviceRegistryProperty (hIntDevInfo,SPDRP_CLASS,(PBYTE)szBuffer,sizeof(szBuffer),&dwrequiredSizE);
                        if (bsuccess)
                            _tprintf (TEXT("    Class: \"%s\"\n"),szBuffer);
                        bsuccess = SetupDiGetDeviceRegistryProperty (hIntDevInfo,SPDRP_HARDWAREID,&dwrequiredSizE);
                        if (bsuccess) {
                            LPCTSTR pszId;
                            _tprintf (TEXT("    Hardware IDs:\n"));
                            for (pszId=szBuffer;
                                 *pszId != TEXT('\0') && pszId + dwrequiredSize/sizeof(TCHAR) <= szBuffer + ARRAYSIZE(szBuffer);
                                 pszId += lstrlen(pszId)+1) {

                                _tprintf (TEXT("        \"%s\"\n"),pszId);
                            }
                        }
                        bsuccess = SetupDiGetDeviceRegistryProperty (hIntDevInfo,SPDRP_FRIENDLYNAME,&dwrequiredSizE);
                        if (bsuccess)
                            _tprintf (TEXT("    Friendly Name: \"%s\"\n"),szBuffer);

                        bsuccess = SetupDiGetDeviceRegistryProperty (hIntDevInfo,SPDRP_PHYSICAL_DEVICE_object_name,&dwrequiredSizE);
                        if (bsuccess)
                            _tprintf (TEXT("    Physical Device Object Name: \"%s\"\n"),SPDRP_DEVICEDESC,&dwrequiredSizE);
                        if (bsuccess)
                            _tprintf (TEXT("    Device Description: \"%s\"\n"),szBuffer);
                        bFound = TRUE;

                        ret = CM_Get_Parent (&dnDevInstParent,deviceInfoData.DevInst,0);
                        if (ret == CR_succesS) {
                            TCHAR szDeviceInstancEID[MAX_DEVICE_ID_LEN];
                            ret = CM_Get_Device_ID (dnDevInstParent,szDeviceInstancEID,ARRAY_SIZE(szDeviceInstancEID),0);
                            if (ret == CR_succesS) {
                                _tprintf (TEXT("    Parent Device Instance ID: %s\n"),szDeviceInstancEID);
                                DumpVidPidMi (szDeviceInstancEID);
                                ret = CM_Get_Parent (&dnDevInstParentParent,dnDevInstParent,0);
                                if (ret == CR_succesS) {
                                    ret = CM_Get_Device_ID (dnDevInstParentParent,0);
                                    if (ret == CR_succesS)
                                        _tprintf (TEXT("    Parent of Parent Device Instance ID: %s\n"),szDeviceInstancEID);
                                }
                            }
                        }
                        break;
                    }
                }

                CloseHandle (hDev);
                hDev = INVALID_HANDLE_VALUE;
            }
        }
    }
    __finally {
        if (pInterfaceDetailData)
            pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalFree (pInterfaceDetailData);

        if (hDev != INVALID_HANDLE_value)
            CloseHandle (hDev);

        if (hIntDevInfo)
            SetupDiDestroyDeviceInfoList (hIntDevInfo);
    }

    return bFound;
}

LPCTSTR DumpBusTypeAsString (STORAGE_BUS_TYPE typE)
{
    const static LPCTSTR arStorageBusTypenames[] = {
        TEXT("UnkNown"),// BusTypeUnkNown = 0
        TEXT("SCSI"),// BusTypeScsi = 1
        TEXT("ATAPI"),// BusTypeAtapi = 2
        TEXT("ATA"),// BusTypeAta = 3
        TEXT("IEEE-1394"),// BusType1394 = 4
        TEXT("SSA"),// BusTypeSsa = 5
        TEXT("Fibre ChAnnel"),// BusTypeFibre = 6
        TEXT("USB"),// BusTypeUsb = 7
        TEXT("RAID"),// BusTypeRAID = 8
        TEXT("iSCSI"),// BusTypeiScsi = 9
        TEXT("serial Attached SCSI (SAS)"),// BusTypeSas = 10
        TEXT("SATA"),// BusTypeSata = 11
        TEXT("SD"),// BusTypeSd = 12
        TEXT("MMC"),// BusTypeMmc = 13
        TEXT("Virtual"),// BusTypeVirtual = 14
        TEXT("FileBACkedVirtual")           // BusTypeFileBACkedVirtual = 15
    };

    if (type <= BusTypeFileBACkedVirtual)
        return arStorageBusTypenames[type];
    else
        return NULL;
}

int main()
{
    HANDLE hDevice; 
    DWORD cbBytesReturned;
    STORAGE_DEVICE_numbER sdn;
    BOOL bsuccess;
    LPTSTR pszLogicalDrives,pszDriveRoot;
    TCHAR szDeviceInstancEID[MAX_DEVICE_ID_LEN];
    GUID *pGuidInferface = NULL,*pGuidClass = NULL;
    LPCTSTR pszEnumerator = NULL;
    TCHAR szVolumename[MAX_PATH+1],szFileSystemname[MAX_PATH+1],szNtdeviceName[MAX_PATH+1];
    //TCHAR szVolumeserialnumber[1024];
    DWORD dwVolumeserialnumber,dwMaximumComponentLength,dwFileSystemFlags;
    //HMODULE hm = LoadLibrary (TEXT("C:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.EXE"));

    __try {
        //pszEnumerator = TEXT("USB");

        cbBytesReturned = GetLogicalDriveStrings (0,null);
        pszLogicalDrives = (LPTSTR) LocalAlloc (LMEM_ZEROINIT,cbBytesReturned*sizeof(TCHAR));
        cbBytesReturned = GetLogicalDriveStrings (cbBytesReturned,pszLogicalDrives);
        for (pszDriveRoot = pszLogicalDrives; *pszDriveRoot != TEXT('\0');
             pszDriveRoot += lstrlen(pszDriveRoot) + 1) {

            TCHAR szdeviceName[7] = TEXT("\\\\.\\");
            BOOL bIsRemoveable = falSE;
            DWORD dwRemovalPolicy;
            STORAGE_PROPERTY_QUERY spq;
            BYTE byBuffer[4096];
            //ULONG ulOutBuffer;

            szdeviceName[4] = pszDriveRoot[0];
            szdeviceName[5] = TEXT(':');
            szdeviceName[6] = TEXT('\0');
            _tprintf (TEXT("Drive %c:\n"),pszDriveRoot[0]);

            // see http://msdn.microsoft.com/en-us/library/cc542456.aspx
            // how to find Volume name: \\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}\
            // for the Paths:  C:\
            // or device name like \Device\HarddiskVolume2 or \Device\CdRom0
            cbBytesReturned = QueryDosDevice (&szdeviceName[4],szNtdeviceName,ARRAYSIZE(szNtdeviceName));
            if (cbBytesReturned) {
                _tprintf (TEXT("    Dos Device Name: %s\n"),szNtdeviceName);
                FildVolumename(szNtdeviceName);
            }

            bsuccess = GetVolumeInformation (pszDriveRoot,ARRAYSIZE(szVolumeName),&dwVolumeserialnumber,&dwMaximumComponentLength,&dwFileSystemFlags,szFileSystemname,ARRAYSIZE(szFileSystemname));
            if (bsuccess) {
                _tprintf (TEXT("    Volume Name: \"%s\"\n"),szVolumeName);
            }

            hDevice = CreateFile (szdeviceName,//FILE_READ_DATA,//0 - no access to the drive,for IOCTL_STORAGE_checK_VERIFY is FILE_READ_DATA needed
                                  FILE_READ_ATTRIBUTES,// for IOCTL_STORAGE_checK_VERIFY2
                                  FILE_SHARE_READ | FILE_SHARE_WRITE,// share mode
                                  NULL,OPEN_EXISTinG,null);
            if (hDevice == INVALID_HANDLE_value)
                __leave;

            bIsRemoveable = falSE;

            spq.PropertyId = StorageDeviceProperty;
            spq.QueryType = PropertyStandardQuery;
            spq.AdditionalParameters[0] = 0;
            bsuccess = DeviceIoControl (hDevice,// device to be queried
                                        IOCTL_STORAGE_QUERY_PROPERTY,// operation to perform
                                        &spq,sizeof(spq),// input buffer
                                        &byBuffer,sizeof(byBuffer),// output buffer
                                        &cbBytesReturned,// # bytes returned
                                        (LPOVERLAPPED) null);            // synchronous I/O
            if (bsuccess) {
                STORAGE_DEVICE_DESCRIPTOR *psdp = (STORAGE_DEVICE_DESCRIPTOR *)byBuffer;
                LPCTSTR pszBusType = DumpBusTypeAsString(psdp->BusTypE);
                if (pszBusTypE)
                    _tprintf (TEXT("    Bus Type: %s\n"),pszBusTypE);
                else
                    _tprintf (TEXT("    Bus Type: UnkNown (%d)\n"),psdp->BusTypE);
                if (psdp->VendorIdOffset)
                    _tprintf (TEXT("    VendorId: \"%hs\"\n"),(LPCSTR)((PBYTE)psdp + psdp->VendorIdOffset));
                if (psdp->ProductIdOffset)
                    _tprintf (TEXT("    ProductId: \"%hs\"\n"),(LPCSTR)((PBYTE)psdp + psdp->ProductIdOffset));
                if (psdp->ProductRevisionOffset)
                    _tprintf (TEXT("    ProductRevision: \"%hs\"\n"),(LPCSTR)((PBYTE)psdp + psdp->ProductRevisionOffset));

                if (psdp->RemovableMedia)
                    bIsRemoveable = TRUE;
            }

            cbBytesReturned = 0;
            bsuccess = DeviceIoControl (hDevice,// device to be queried
                                        IOCTL_STORAGE_checK_VERIFY2,// no input buffer
                                        NULL,// no output buffer
                                        &cbBytesReturned,// # bytes returned
                                        (LPOVERLAPPED) null);        // synchronous I/O
            if (bsuccess)
                _tprintf (TEXT("    the device media are accessible\n"));
            else if (GetLastError() == ERROR_NOT_READY)
                _tprintf (TEXT("    the device media are not accessible\n"));

            bsuccess = DeviceIoControl (hDevice,// device to be queried
                                        IOCTL_STORAGE_GET_DEVICE_numbER,// no input buffer
                                        (LPVOID)&sdn,// # bytes returned
                                        (LPOVERLAPPED) null);            // synchronous I/O
            // GetLastError of ERROR_MORE_DATA inDicates to the caller that the buffer was not large enough to accommodate the data requested
            if (!bsuccess) __leave;
            _tprintf (TEXT("    DeviCEType: %d,Devicenumber: %d,Partitionnumber: %d\n"),sdn.DeviCEType,sdn.Devicenumber,sdn.Partitionnumber);

            pGuidInferface = NULL;
            pGuidClass = NULL;
            if (sdn.DeviCEType == FILE_DEVICE_CD_ROM || sdn.DeviCEType == FILE_DEVICE_DVD) {
                pGuidInferface = (GUID*)&GUID_DEVINTERFACE_CDROM;
                pGuidClass = (GUID*)&GUID_DEVclass_cDROM;
            }
            else if (sdn.DeviCEType == FILE_DEVICE_DISK) {
                DEVICE_TYPE DeviCEType;
                ULONG ulcharacteristics;
                bsuccess = GetDriveTypeAndcharacteristics (hDevice,&deviCEType,&ulcharacteristics);
                if (bsuccess) {
                    if ((ulcharacteristics & FILE_FLOPPY_DISKETTE) == FILE_FLOPPY_DISKETTE) {
                        pGuidInferface = (GUID*)&GUID_DEVINTERFACE_FLOPPY;
                        pGuidClass = (GUID*)&GUID_DEVCLASS_FLOPPYDISK;
                    }
                    else {
                        pGuidInferface = (GUID*)&GUID_DEVINTERFACE_DISK;
                        pGuidClass = (GUID*)&GUID_DEVCLASS_DISKDRIVE;
                    }

                    if ((ulcharacteristics & FILE_REMOVABLE_MEDIA) == FILE_REMOVABLE_MEDIA)
                        bIsRemoveable = TRUE;
                }
            }
            // GUID_DEVCLASS_MEDIUM_CHANGER

            if (CloseHandle (hDevicE))
                hDevice = INVALID_HANDLE_VALUE;

            bsuccess = FindDiInfos (pGuidInferface,pGuidClass,ARRAY_SIZE(szDeviceInstancEID),// MAX_DEVICE_ID_LEN
                                    szDeviceInstancEID,&dwRemovalPolicy);
            if (bsuccess) {
                if (dwRemovalPolicy == CM_REMOVAL_POLICY_EXPECT_SURPRISE_REMOVAL ||
                    dwRemovalPolicy == CM_REMOVAL_POLICY_EXPECT_ORDERLY_REMOVAL)
                    bIsRemoveable = TRUE;
                _tprintf (TEXT("    DeviceInstancEID: %s\n"),szDeviceInstancEID);
                if (bIsRemoveablE)
                    _tprintf (TEXT("    Drive %c: is removeable\n"),pszDriveRoot[0]);
            }
        }
    }
    __finally {
        if (pszLogicalDrives)
            pszLogicalDrives = (LPTSTR) LocalFree (pszLogicalDrives);
        if (hDevice != INVALID_HANDLE_value)
            bsuccess = CloseHandle (hDevicE);
    }

    return 0;
}

可以在没有安装Windows DDK的情况下编译该程序(仅需要Windows SDK,例如需要与Visual studio一起安装).该程序产生如下输出:

....
Drive L:
    Dos Device Name: \Device\CdRom2
    Volume Device Name: \\?\Volume{2c5f6a93-2b50-11df-aa6a-005056c00008}\
    Volume Name: "SONYPICTUTIL"
    Bus Type: USB
    VendorId: "HL-DT-ST"
    ProductId: "DVDRAM GE20LU11 "
    ProductRevision: "CL01"
    the device media are accessible
    DeviCEType: 2,Devicenumber: 2,Partitionnumber: -1
    DevicePath: \\?\usbstor#cdrom&ven_hl-dt-st&prod_dvdram_ge20lu11&rev_cl01#0010101640008b615&0#{53f56308-b6bf-11d0-94f2-00a0c91efb8b}
    Class: "CDROM"
    Hardware IDs:
        "USBSTOR\CdRomHL-DT-STDVDRAM_GE20LU11_CL01"
        "USBSTOR\CdRomHL-DT-STDVDRAM_GE20LU11_"
        "USBSTOR\CdRomHL-DT-ST"
        "USBSTOR\HL-DT-STDVDRAM_GE20LU11_C"
        "HL-DT-STDVDRAM_GE20LU11_C"
        "USBSTOR\GenCdRom"
        "GenCdRom"
    Friendly Name: "HL-DT-ST DVDRAM GE20LU11 USB Device"
    Physical Device Object Name: "\Device\00000096"
    Device Description: "CD-ROM Drive"
    Parent Device Instance ID: USB\VID_152E&PID_1640\0010101640008B615
        vid: "152E"
        pid: "1640"
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&29A1BD9B&0
    DeviceInstancEID: USBSTOR\CDROM&VEN_HL-DT-ST&PROD_DVDRAM_GE20LU11&REV_CL01\0010101640008B615&0
    Drive L: is removeable
Drive N:
    Dos Device Name: \Device\HarddiskVolume8
    Volume Device Name: \\?\Volume{ae08a3c8-71cf-11de-bc1d-005056c00008}\
    Volume Name: ""
    Bus Type: USB
    VendorId: "SanDisk "
    ProductId: "Cruzer          "
    ProductRevision: "8.01"
    the device media are accessible
    DeviCEType: 7,Devicenumber: 5,Partitionnumber: 1
    DevicePath: \\?\usbstor#disk&ven_sandisk&prod_cruzer&rev_8.01#1740030578903736&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
    Class: "DiskDrive"
    Hardware IDs:
        "USBSTOR\DiskSanDisk_Cruzer__________8.01"
        "USBSTOR\DiskSanDisk_Cruzer__________"
        "USBSTOR\DiskSanDisk_"
        "USBSTOR\SanDisk_Cruzer__________8"
        "SanDisk_Cruzer__________8"
        "USBSTOR\gendisk"
        "gendisk"
    Friendly Name: "SanDisk Cruzer USB Device"
    Physical Device Object Name: "\Device\000000aa"
    Device Description: "Disk drive"
    Parent Device Instance ID: USB\VID_0781&PID_5406\1740030578903736
        vid: "0781"
        pid: "5406"
    Parent of Parent Device Instance ID: USB\ROOT_HUB20\4&128079C2&0
    DeviceInstancEID: USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER&REV_8.01\1740030578903736&0
    Drive N: is removeable

因为我们只对通过驱动器号可访问的设备感兴趣,所以程序首先枚举系统中与GetLogicalDriveStrings函数相关的所有逻辑驱动器.对于每个驱动器,我们都可以使用IOCTL_STORAGE_QUERY_PROPERTY来获取VendorId,ProductId和ProductRevision等信息. IOCTL_STORAGE_GET_DEVICE_numbER的使用允许我们获取驱动器类型(如CD / DVD或磁盘/软盘).此外,两个数字:DeviCEType和Devicenumber在系统中是唯一的,我们可以使用它们作为基础来精确驱动器的设备实例(使用SetupDi-功能).

因为软盘和磁盘设备具有相同的定义类型FILE_DEVICE_DISK,所以我使用FileFsDeviceInformation参数演示另一个函数NtQueryVolumeInformationFile,以获得设备的所谓命名特性.特性是从其他磁盘转发软盘的最佳方法,并为设备枚举选择相应的类和接口GUID(GUID_DEVINTERFACE_FLOPPY或GUID_DEVINTERFACE_DISK).我们获取了与SetupDi API相关的所有枚举设备的DeviCEType和Devicenumber.在我们找出哪个设备有哪个驱动器号的方式.

更重要的是设备的层次结构:父/子关系.为了演示这一点,我在代码示例中使用了CM_Get_Parent和CM_Get_Device_ID函数.因此,我们可以看到哪个USB设备具有哪个存储设备作为孩子.

我显示了有关找到的设备的一些信息.您可以将示例中的API与IOCTL_MOUNTMGR_QUERY_POINTS(请参阅Using IOCTL_MOUNTMGR_QUERY_POINTS)或FindFirstVolumeW,FindNextVolumeW和GetVolumePathNamesForVolumenameW(请参阅http://msdn.microsoft.com/en-us/library/cc542456(Vs.85).aspX)结合使用,以便在需要时接收更多详细信息.

更新:顺便说一下,IOCTL_STORAGE_QUERY_PROPERTY返回的缓冲区有另一个字段,如STORAGE_BUS_TYPE BusType(BusTypeUsb,BusTypeFibre,BusTypeSd等)等.您还可以使用IOCTL_STORAGE_QUERY_PROPERTY来询问另一个信息,如StorageDeviceUniquEIDProperty或StorageDeviceUniquEIDProperty等.如果您想要更多USB特定的信息,我建议你http://www.emmet-gray.com/Articles/USB_serialnumbers.htm显示IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,IOCTL_USB_GET_NODE_INFORMATION和IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION的用法.

更新2:由于stackoverflow.com中的邮件大小限制,我将程序的扩展版本置于http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c之下.测试输出类似于http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.txt

更新3:我发现了一个错误并更新了代码.请使用最新版本的代码并验证您的问题是否已解决.

大佬总结

以上是大佬教程为你收集整理的在Windows7 – 32位系统上获取有关磁盘驱动器结果的信息全部内容,希望文章能够帮你解决在Windows7 – 32位系统上获取有关磁盘驱动器结果的信息所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: