Windows 7 如何在Windows7上具有设备ID的驱动器号

Windows 7 如何在Windows7上具有设备ID的驱动器号,windows-7,get,drive,Windows 7,Get,Drive,我是一个编程新手,所以不要粗鲁 我正在开发一个用于管理大容量存储设备的程序。在Windows Xp和Windows 7上 我已经完成了XP的管理。为此,我使用了这个函数: 我试图理解这个函数在做什么。。但是没有用。。我告诉过你,我是一个初学者,我没有找到一本关于它的教程 所以,我的问题是:我在Windows XP中使用了此功能,如您所见: int _tmain(int argc, _TCHAR* argv[]) { //char id_voulue[TAILLE_MAX2]= "USBS

我是一个编程新手,所以不要粗鲁

我正在开发一个用于管理大容量存储设备的程序。在Windows Xp和Windows 7上

我已经完成了XP的管理。为此,我使用了这个函数:

我试图理解这个函数在做什么。。但是没有用。。我告诉过你,我是一个初学者,我没有找到一本关于它的教程

所以,我的问题是:我在Windows XP中使用了此功能,如您所见:

int _tmain(int argc, _TCHAR* argv[])
{
    //char id_voulue[TAILLE_MAX2]= "USBSTOR\DISK&VEN_KINGSTON&PROD_READER__MICSD/M2&REV_0200\AA00000000135539&1";
    WCHAR cDrive;
    LPSTR dev_ID = "USBSTOR\\DISK&VEN_GENERIC-&PROD_COMPACT_FLASH&REV_1.01\\9&19571B1B&0&058F63646476&1"/*USBSTOR\\DISK&VEN_KINGSTON&PROD_READER_____SD/MS&REV_0200\\AA00000000135539&0"*/;
    int code_erreur = 0;

    //id_voulue[strlen(id_voulue) - 1] = 0x00;

    //dev_ID = id_voulue; 


    if(!GetAllRemovableDisks())
    {
        code_erreur = -1 ;
    }

    cDrive = GetSpecificDrive(dev_ID);

    printf("L'ID est : %s \n", dev_ID);

    printf("La lettre correspondante est : %c \n", cDrive);


    puts("\n\n");
    system("pause");
    return 0;
}


//                My functions 


struct tagDrives
{
    WCHAR letter;
    WCHAR volume[BUFFER_SIZE];
}g_drives[26];


BOOL GetAllRemovableDisks()
{
    WCHAR caDrive[4];
    WCHAR volume[BUFFER_SIZE];
    int nLoopIndex;
    DWORD dwDriveMask;
    UINT g_count;

    caDrive[0] = 'A';
    caDrive[1] = ':';
    caDrive[2] = '\\';
    caDrive[3] = 0;

    g_count = 0;

    // Get all drives in the system.
    dwDriveMask = GetLogicalDrives();

    if(dwDriveMask == 0)
    {
        puts("Error - GetLogicalDrives failed\n");
        return FALSE;
    }

    // Loop for all drives (MAX_DRIVES = 26)

    for(nLoopIndex = 0; nLoopIndex< MAX_DRIVES; nLoopIndex++)
    {
        // if a drive is present,
        if(dwDriveMask & 1)
        {
            caDrive[0] = 'A' + nLoopIndex;

            // If a drive is removable
            if(GetDriveType(caDrive) == DRIVE_REMOVABLE)
            {
                //Get its volume info and store it in the global variable.
                if(GetVolumeNameForVolumeMountPoint(caDrive, volume, BUFFER_SIZE))
                {
                    g_drives[g_count].letter = caDrive[0];
                    wcscpy(g_drives[g_count].volume, volume);
                    g_count ++;
                }
            }
        }
        dwDriveMask >>= 1;
    }

    // success if atleast one removable drive is found.
    if(g_count == 0)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

WCHAR GetSpecificDrive(LPSTR lpDevID)
{
    HDEVINFO hDevInfo;
    GUID guid;
    BYTE buffer[BUFFER_SIZE];
    DWORD dwRequiredSize ;
    WCHAR buf[BUFFER_SIZE];
    DEVINST devInstParent;
    DWORD dwIndex;
    WCHAR volume[BUFFER_SIZE];
    UINT nLength,nLoopIndex,g_count;
    g_count = 26;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    SP_DEVINFO_DATA devInfoData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;

    if(!lpDevID)
    {
        return 0;
    }

    // GUID_DEVINTERFACE_VOLUME is interface Guid for Volume class devices.
    guid = GUID_DEVINTERFACE_VOLUME;


    // Get device Information handle for Volume interface
    hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL,
    DIGCF_DEVICEINTERFACE |
    DIGCF_PRESENT);

    if(hDevInfo == INVALID_HANDLE_VALUE)
    {
        puts("Error - SetupDiGetClassDevs failed\n");
        return 0;
    }

    // Loop until device interfaces are found.
    for(dwIndex = 0; ;dwIndex ++)
    {
        ZeroMemory(&devInterfaceData, sizeof(devInterfaceData));
        devInterfaceData.cbSize = sizeof(devInterfaceData);

        // Get device Interface data.

        if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid, dwIndex,&devInterfaceData))
        {
            break;
        }

        ZeroMemory(&devInfoData, sizeof(devInfoData));
        devInfoData.cbSize = sizeof(devInfoData);

        pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
        pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        // Get device interface detail data to get
        // Device Instance from SP_DEVINFO_DATA and
        // Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA

        SetupDiGetDeviceInterfaceDetail(hDevInfo,
        &devInterfaceData,
        pDevDetail, // SP_DEVICE_INTERFACE_DETAIL_DATA
        BUFFER_SIZE,
        &dwRequiredSize,
        &devInfoData); // SP_DEVINFO_DATA

        // Get the device instance of parent. This points to USBSTOR.
        CM_Get_Parent(&devInstParent,devInfoData.DevInst, 0);
        CM_Get_Device_ID(devInstParent, buf, BUFFER_SIZE,0);
        // Get the device instance of grand parent. This points to USB root.
        CM_Get_Parent(&devInstParent,devInstParent, 0);
        // Get the device ID of the USB root.
        //CM_Get_Device_ID(devInstParent, buf, BUFFER_SIZE,0);

        // If USB root device matches with the input device ID, it is the target
        //device.

        if( buf != NULL && compare(lpDevID,buf)==0)
        {
            // Append \ to the DevicePath of SP_DEVICE_INTERFACE_DETAIL_DATA
            if(lpDevID[50] == buf[50] && lpDevID[55] == buf[55] && lpDevID[60] == buf[60])
            {
                nLength = wcslen(pDevDetail->DevicePath);
                pDevDetail->DevicePath[nLength] = '\\';
                pDevDetail->DevicePath[nLength] = '\\';
                pDevDetail->DevicePath[nLength+1] = 0;

                // Get Volume mount point for the device path.
                if(GetVolumeNameForVolumeMountPoint(pDevDetail->DevicePath, volume, BUFFER_SIZE))
                {
                    for(nLoopIndex=0; nLoopIndex< g_count; nLoopIndex++)
                    {
                        // Compare volume mount point with the one stored earlier.
                        // If both match, return the corresponding drive letter.

                        if(wcscmp(g_drives[nLoopIndex].volume, volume)==0)
                        {
                            SetupDiDestroyDeviceInfoList(hDevInfo);
                            return g_drives[nLoopIndex].letter;
                        }

                    }
                }
            }
        }
    }

    SetupDiDestroyDeviceInfoList(hDevInfo);
    puts("Error - No drives found in GetSpecificDrives\n");
    return 0;
}

UINT compare(LPSTR lpDevID,WCHAR* buf)
{
    UINT nombre_caractere1, 
         confirmation = 0, 
         i = 0;
    nombre_caractere1 = strlen(lpDevID);

        for(i=1 ; i <= nombre_caractere1 ; i++)
        {
            if(lpDevID[i]==buf[i])
            {
                confirmation += 1;
            }

        }

        if(confirmation == nombre_caractere1)
        {
            return 0;
        }
        else
        {
            return -1;
        }
}
因此,这段代码在XP上可以正常工作,但在Windows7上不行。。似乎如果我们没有从寄存器基的正确位置开始。在比较过程中,我可以看到变量buf是HTREE\ROOT\0,在第二个循环中,buf=ACPI\PNP0A08\0,直到最后,我再次看到buf=HTREE\ROOT\0


有人能给我解释一下发生了什么事吗?我真的试着去理解。我在msdn上做了很多,但也许我很愚蠢,因为我不了解它。

o\0哦,那肯定是一堵古老的文字墙
        Console.WriteLine("Boot Drive: " + Environment.SystemDirectory.Substring(0,2));
        ManagementObjectCollection wmiDiskDrives =
        new ManagementObjectSearcher(
        @"Select Caption, DeviceID From Win32_DiskDrive ").Get();

        foreach (ManagementObject wmiDiskDrive in wmiDiskDrives)
        {
            Console.WriteLine();
            Console.WriteLine("---------------");
            Console.WriteLine("Disk Drive Caption: " + wmiDiskDrive["Caption"] + "\r\n" +"DeviceID: " + " (" + wmiDiskDrive["DeviceID"]+")");

            ManagementObjectCollection wmiDiskPartitions  =
            new ManagementObjectSearcher
            ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + wmiDiskDrive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get();

            foreach (ManagementObject wmiDiskPartition  in wmiDiskPartitions)
            {
            ManagementObjectCollection wmiLogicalDisks   =
            new ManagementObjectSearcher
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"   + wmiDiskPartition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition" ).Get();

                foreach (ManagementObject wmiLogicalDisk  in wmiLogicalDisks)
                {
                    Console.WriteLine("Drive Letter associated with Disk drive = "
                        + wmiDiskDrive["Caption"] 
                        + wmiDiskDrive["DeviceId"]
                        + "\r\n" + "Partition = "
                        + wmiDiskPartition["DeviceID"]
                        + "\r\n" + " is "
                        + wmiLogicalDisk["DeviceID"]);
                }
            }
        }