我是编程初学者,所以不要粗鲁^^..
我正在开发一个用于管理大容量存储设备的程序。在 Windows Xp 和 Windows 7 上。
我已经完成了 XP 的管理器。为此,我使用了这个功能:Drive Letter to Device Instance ID
我试图理解这个函数在做什么。但徒劳无功。正如我告诉你的,我是初学者,我没有找到一个关于它的教程。
所以,这是我的问题:我已经在 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 上可以正常工作,但在 Windows 7 上不行。似乎我们没有从寄存器库中的正确位置开始。在比较过程中,我可以看到我的变量“buf”是“HTREE\ROOT\0”,在第二个循环中,“buf”=“ACPI\PNP0A08\0”,直到最后,我再次看到“buf” =“HTREE\ROOT\0”..
有人可以向我解释发生了什么吗?我真的很努力去理解。我在 msdn 上玩了很多,但也许我很愚蠢,因为我不明白。