我通过使用setup api系列函数,枚举得到几个磁盘名字,比如:Hitachi HTS542516K9SA00,Kingston DateTraverl2.0 USB Device,....然后我想使用CreateFile以"\\PhysicalDriveX"打开某个磁盘,如何确定他们之间的关系啊,比如\\PhysicalDrive0就是Hitachi HTS542516K9SA00

解决方案 »

  1.   

    你先用 CreateFile  打开那个盘 取得Handle 后
    再调用  
    BOOL DeviceIoControl(
      (HANDLE) hDevice,                        // handle to device
      IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,    // dwIoControlCode
      NULL,                                    // lpInBuffer
      0,                                       // nInBufferSize
      (LPVOID) lpOutBuffer,                    // output buffer
      (DWORD) nOutBufferSize,                  // size of output buffer
      (LPDWORD) lpBytesReturned,               // number of bytes returned
      (LPOVERLAPPED) lpOverlapped              // OVERLAPPED structure
    );反回的结构  
    typedef struct _VOLUME_DISK_EXTENTS {
        ULONG NumberOfDiskExtents;
        DISK_EXTENT Extents[1];
    } VOLUME_DISK_EXTENTS, *PVOLUME_DISK_EXTENTS;typedef struct _DISK_EXTENT {  DWORD DiskNumber;  LARGE_INTEGER StartingOffset;  LARGE_INTEGER ExtentLength;
    } DISK_EXTENT, DiskNumber 就是这个盘符,所在的磁盘了
      

  2.   


    IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS这个是打开盘符吧,我想要物理设备\\PhysicalDrive跟物理设备名字的对应关系!!
      

  3.   

    WMI 枚举 Win32_DiskDrive 实例 
    DeviceID 得到 "\\PhysicalDrive0"
    Caption  得到名字如我的是 "ST3160815AS"如果需要更详细的信息, 可以获取每个实例关联类如 Win32_PnPEntity....等关联类WQL语句sprintf(szWql, "ASSOCIATORS OF {%s} WHERE AssocClass = %s", (CHAR *)bStrPath, pAssocClassName);
      

  4.   

    用setupapi获得DevicePath后,CreateFile打开,然后发送IOCTL_STORAGE_GET_DEVICE_NUMBER就可以了
      

  5.   

    >>然后我想使用CreateFile以"\\PhysicalDriveX"打开某个磁盘,如何确定他们之间的关系啊,比如\\PhysicalDrive0就是Hitachi HTS542516K9SA00
    用SetupDiGetDeviceRegistryProperty 函数 使用 SPDRP_LOCATION_INFORMATION 参数就能查询磁盘所在位置,组合成\\PhysicalDriveX即可比如,如下代码
    #include <stdio.h>
    #include <windows.h>
    #include <setupapi.h>
    #include <devguid.h>
    #include <regstr.h>
    #include <TCHAR.h>#pragma comment (lib,"setupapi")const TCHAR szDevClass[] = "DiskDrive";int main( int argc, char *argv[ ], char *envp[ ] )
    {
        HDEVINFO hDevInfo;
        SP_DEVINFO_DATA DeviceInfoData;
        DWORD i;
    DWORD PropertyID = SPDRP_CLASS;
    bool isdevflag = false;
        // Create a HDEVINFO with all present devices.
        hDevInfo = SetupDiGetClassDevs(NULL,
            0, // Enumerator
            0,
            DIGCF_PRESENT | DIGCF_ALLCLASSES );
        
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            // Insert error handling here.
            return 1;
        }
        
        // Enumerate through all devices in Set.
        
        DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
            &DeviceInfoData);i++)
        {
            DWORD DataT;
            LPTSTR buffer = NULL;
            DWORD buffersize = 0;
    DWORD dwErrCode;
            
            // 
            // Call function with null to begin with, 
            // then use the returned buffer size 
            // to Alloc the buffer. Keep calling until
            // success or an unknown failure.
            // 
    Nxt:
            while (!SetupDiGetDeviceRegistryProperty(
                hDevInfo,
                &DeviceInfoData,
                PropertyID,
                &DataT,
                (PBYTE)buffer,
                buffersize,
                &buffersize))
            {

                if ((dwErrCode = GetLastError() ) == 
                    ERROR_INSUFFICIENT_BUFFER)
                {
                    // Change the buffer size.
                    if (buffer) LocalFree(buffer);
                    buffer = (TCHAR *)LocalAlloc(LPTR,buffersize);
                }
                else if ( ERROR_INVALID_DATA  == dwErrCode )
    {
    printf("SetupDiGetDeviceRegistryProperty UnSupported\n");
                    goto Exit;
    }
    else
                {
                    // Insert error handling here.
    printf("SetupDiGetDeviceRegistryProperty Error : [%ld]\n",dwErrCode);
                    goto Exit;
                }
            }

            //是磁盘驱动器?
    if ( _tcscmp( szDevClass, buffer ) == 0 )
    {
    PropertyID = SPDRP_LOCATION_INFORMATION    ;
    if (buffer) 
    {
    LocalFree(buffer);
    buffer = NULL;
    }
    buffersize = 0;
    isdevflag = true;
    goto Nxt;
    }
    else if ( isdevflag )
    {
    printf("DevName:\\\\.\\PhysicalDrive%s\n",buffer);
    if (buffer) 
    {
    LocalFree(buffer);
    buffer = NULL;
    }
    buffersize = 0;
    isdevflag = false;

    } PropertyID = SPDRP_CLASS;

            
            
        }
        
        
        if ( GetLastError()!=NO_ERROR &&
             GetLastError()!=ERROR_NO_MORE_ITEMS )
        {
            // Insert error handling here.
            return 1;
        }Exit:
        //  Cleanup
        SetupDiDestroyDeviceInfoList(hDevInfo);
        
        return 0;
    }