如何获得驱动器号,Windows7上带有设备ID [英] How to have the drive letter, with the device ID on Windows7

查看:183
本文介绍了如何获得驱动器号,Windows7上带有设备ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,所以不要粗鲁^^ ..

我正在开发一个用来管理海量存储设备的程序。在Windows XP和Windows 7上。



我已经完成了XP的管理器。对于它,我使用了这个函数:驱动器号到设备实例ID



我试图理解这个函数在做什么..但徒劳无功......正如我告诉过你的,ima初学者,我没有找到一个单一的教程它是。



所以,这是我的问题:我已经在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 \\\
,dev_ID);

printf( (\\\
\\\
);
系统(暂停);


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


//我的函数


struct tagDrives
{
WCHAR字母;
WCHAR卷[BUFFER_SIZE];
} g_drives [26];


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

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

g_count = 0;

//获取系统中的所有驱动器。
dwDriveMask = GetLogicalDrives();

if(dwDriveMask == 0)
{
puts(Error - GetLogicalDrives failed\\\
);
返回FALSE;


//所有驱动器循环(MAX_DRIVES = 26)

(nLoopIndex = 0; nLoopIndex< MAX_DRIVES; nLoopIndex ++)
{
//如果存在驱动器,
if(dwDriveMask& 1)
{
caDrive [0] ='A'+ nLoopIndex;

//如果驱动器是可移动的
if(GetDriveType(caDrive)== DRIVE_REMOVABLE)
{
//获取其音量信息并将其存储在全局变量。
if(GetVolumeNameForVolumeMountPoint(caDrive,volume,BUFFER_SIZE))
{
g_drives [g_count] .letter = caDrive [0];
wcscpy(g_drives [g_count] .volume,volume);
g_count ++;
}
}
}
dwDriveMask>> = 1;
}

//如果找到至少一个可移动驱动器,则成功。
if(g_count == 0)
{
return FALSE;
}
else
{
return TRUE;
}
}

WCHAR GetSpecificDrive(LPSTR lpDevID)
{
HDEVINFO hDevInfo;
GUID guid;
BYTE缓冲区[BUFFER_SIZE];
DWORD dwRequiredSize;
WCHAR buf [BUFFER_SIZE];
DEVINST devInstParent;
DWORD dwIndex;
WCHAR音量[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是Volume类设备的接口Guid。
guid = GUID_DEVINTERFACE_VOLUME;


//获取卷接口的设备信息句柄
hDevInfo = SetupDiGetClassDevs(& guid,NULL,NULL,
DIGCF_DEVICEINTERFACE |
DIGCF_PRESENT);

if(hDevInfo == INVALID_HANDLE_VALUE)
{
puts(Error - SetupDiGetClassDevs failed \\\
);
返回0;
}

//循环直到找到设备接口。 $ d $ b $ for(dwIndex = 0;; dwIndex ++)
{
ZeroMemory(& devInterfaceData,sizeof(devInterfaceData));
devInterfaceData.cbSize = sizeof(devInterfaceData);

//获取设备接口数据。 $!
$ b 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);

//获取设备接口详细信息以获取
//设备实例,来自SP_DEVINFO_DATA和
//来自SP_DEVICE_INTERFACE_DETAIL_DATA的设备路径

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

//获取父设备实例。这指向USBSTOR。
CM_Get_Parent(& devInstParent,devInfoData.DevInst,0);
CM_Get_Device_ID(devInstParent,buf,BUFFER_SIZE,0);
//获取父母的设备实例。这指向USB根。
CM_Get_Parent(& devInstParent,devInstParent,0);
//获取USB根的设备ID。
// CM_Get_Device_ID(devInstParent,buf,BUFFER_SIZE,0);

//如果USB根设备与输入设备ID匹配,则它是目标
//设备。

if(buf!= NULL&&& amp; amp; compare(lpDevID,buf)== 0)
{
//追加\到SP_DEVICE_INTERFACE_DETAIL_DATA $ b $的DevicePath b 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;

//获取设备路径的卷装载点。
if(nLoopIndex = 0; nLoopIndex< g_count; nLoopIndex ++)

/ /将卷装入点与先前存储的装入点比较。
//如果两者匹配,则返回相应的驱动器号。

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

}
}
}
}
}

SetupDiDestroyDeviceInfoList(hDevInfo);
puts(Error - No drives found in GetSpecificDrives \\\
);
返回0;
}

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

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

}

if(confirmation == nombre_caractere1)
{
return 0;
}
else
{
return -1;






$ b

所以这段代码在XP上工作正常,但没有Windows Seven ..看起来好像我们没有从注册基地的正确位置开始。在我的比较中,我可以看到我的变量buf是HTREE \ ROOT \0,在第二个循环中,buf=ACPI\PNP0A08\0,直到最后,我看到再次buf=HTREE\ROOT\0..



有人可以向我解释发生了什么事吗?我真的很想明白。我已经在msdn上做了很多工作,但也许是愚蠢的,因为我没有得到它。

Console.WriteLine(Boot Drive:+ Environment.SystemDirectory.Substring(0,2));
ManagementObjectCollection wmiDiskDrives =
New ManagementObjectSearcher(
@Select Caption,DeviceID From Win32_DiskDrive).Get();

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

ManagementObjectCollection wmiDiskPartitions =
New ManagementObjectSearcher
(Win32_DiskDrive.DeviceID ='+ wmiDiskDrive [DeviceID] +'} WHERE AssocClass = Win32_DiskDriveToDiskPartition)的关联者。得到();

foreach(wmiDiskPartitions中的ManagementObject wmiDiskPartition)
{
ManagementObjectCollection wmiLogicalDisks =
New ManagementObjectSearcher
({Win32_DiskPartition.DeviceID =''+ wmiDiskPartition [DeviceID] +'} WHERE AssocClass = Win32_LogicalDiskToPartition).Get();

foreach(wmiLogicalDisks中的ManagementObject wmiLogicalDisk)
{
Console.WriteLine(与磁盘驱动器相关的驱动器盘符=
+ wmiDiskDrive [Caption]
+ wmiDiskDrive [DeviceId]
+\r\\\
+Partition =
+ wmiDiskPartition [DeviceID]
+\r\\\
+是
+ wmiLogicalDisk [DeviceID]);
}
}
}


I am a beginner in programming, so don't be rude ^^..

I'm developing a programme which will use to manage mass storage device. On Windows Xp and Windows 7.

I' have already done the XP's manager. And for it, i used this function : Drive Letter to Device Instance ID

I've try to understand what's this function was doing.. But in vain.. As i told you, i m a beginner, and i did'nt found one single tutorial about it.

So, here's my problem : I've used this function for Windows XP as you can see :

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;
        }
}

So this code work fine on XP, but no on Windows Seven.. It seems like if we don't start at the right place in the register base. During my comparaison, I can see that my variable "buf" is "HTREE\ROOT\0", at the second loop, "buf" = "ACPI\PNP0A08\0", and until the end, i see again "buf" = "HTREE\ROOT\0"..

Can someone explain to me what's going on ? I've really tried to understand. I've been a lot on msdn, but maybe i m stupid, because i don't got it.

解决方案

        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"]);
                }
            }
        }

这篇关于如何获得驱动器号,Windows7上带有设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆