Discussion:
Retrieving mapped drives from USB MSC device
(too old to reply)
Sasha
2005-06-21 23:34:01 UTC
Permalink
I can enumerate all connected USB Mass Storage devices using code similar to
usbview. I can get USB_DEVICE_DESCRIPTOR and string descriptors. But how do I
get removable drive letter for each connected device?
Don Burn
2005-06-21 23:39:18 UTC
Permalink
How about just doing it the other way? Check your mapped drives to see if
they are USB with IOCTL_STORAGE_QUERY_PROPERTY?
--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply
Post by Sasha
I can enumerate all connected USB Mass Storage devices using code similar to
usbview. I can get USB_DEVICE_DESCRIPTOR and string descriptors. But how do I
get removable drive letter for each connected device?
Sasha
2005-06-23 01:31:05 UTC
Permalink
I need to get USB device ID from USB MSC device string descriptors. I can
enumerate all USB hubs and ports. I can then find what devices are connected
to which ports and get their serial number in string descriptors. Now I need
to find what mapped drives does this particular USB Mass Storage device have.
I would really appreciate any advise as I am literally stuck. What you
propose is to walk through all removable drives and query their information
with IOCTL_STORAGE_QUERY_PROPERTY. Unfortunately, this does not give me USB
device serial number that I need.
Post by Don Burn
How about just doing it the other way? Check your mapped drives to see if
they are USB with IOCTL_STORAGE_QUERY_PROPERTY?
--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply
Post by Sasha
I can enumerate all connected USB Mass Storage devices using code similar to
usbview. I can get USB_DEVICE_DESCRIPTOR and string descriptors. But how do I
get removable drive letter for each connected device?
Robert Marquardt
2005-06-22 11:56:27 UTC
Permalink
Always good to keep old messages:
------------------
Subject:
RE: How to match between physical usb device and its drive letter?
From:
"Kiran" <***@discussions.microsoft.com>
Date:
Wed, 20 Oct 2004 22:01:02 -0700
Newsgroups:
microsoft.public.development.device.drivers

Hi,

There is no simple/direct way of obtaining the drive letter.

The following code can be used to get the drive letter
But you have to modify them here and there to meet you need.

Kiran


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

//
WCHAR GetUSBDrive()
{
LPTSTR lpDevID;
WCHAR cDrive;
DWORD dwSize = BUFFER_SIZE;

// Get all removable disks on user laptop.
if(!GetAllRemovableDisks())
{
WRITELOG("Error - GetAllRemovableDisks failed\n");
return 0;
}

// Alocate memory to device ID
lpDevID = (LPTSTR)AllocMem(BUFFER_SIZE);

// Get device ID corresponding to USBFM from registry.
if(!GetRegValue(lpDevID, DEVICE_ID, dwSize))
{
WRITELOG("Error - Registry - USBFMDevID failed\n");
FreeMem(lpDevID);
return 0;
}

// Get drive corresponding to the registry entry.
cDrive = GetSpecificDrive(lpDevID);

FreeMem(lpDevID);

// return the drive letter.
return cDrive;
}

/******************************************************************************
* GetAllRemovableDisks - This function retrieves volume information for all
removable disks
*
* In: None
*
* Out: TRUE - Success
* FALSE - Failure
*
*******************************************************************************/

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

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

g_count = 0;

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

if(dwDriveMask == 0)
{
WRITELOG("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;
}
}

/******************************************************************************
* GetSpecificDrive - This function returns the drive corresponding to the
given device ID
*
* In : lpDevID - Device ID
*
* Return: Drive letter corresponding to the given device ID.
*
*******************************************************************************/

WCHAR GetSpecificDrive(
LPTSTR lpDevID)
{
HDEVINFO hDevInfo;
GUID guid;
BYTE buffer[BUFFER_SIZE];
DWORD dwRequiredSize ;
WCHAR buf[BUFFER_SIZE];
DEVINST devInstParent;
DWORD dwIndex;
WCHAR volume[BUFFER_SIZE];
int nLength,nLoopIndex;

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)
{
WRITELOG("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);

// 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 && wcscmp(lpDevID,buf) == 0)
{
// Append \ to the DevicePath of SP_DEVICE_INTERFACE_DETAIL_DATA

nLength = wcslen(pDevDetail->DevicePath);
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);
WRITELOG("Error - No drives found in GetSpecificDrives\n");
return 0;
}
kenichi
2010-06-10 03:43:26 UTC
Permalink
Hi K
but my case is that there is two usb device with same VID plugged into host
at same time. how to indetify now ?
such as, plug into 2 cdrom (same VID) with applicatoin autorun, it is
possible that appliation in cdrom-A will aslo communicate with cdrom-B or it
will find cdrom-B and cdrom-A in sequency.
any way to avoid?

url:http://www.ureader.com/msg/1474538.aspx
kenichi
2010-06-10 03:53:28 UTC
Permalink
Hello:
you code find drive letter by device id.
but how to do it if there is 2 same device plugged into host at same time.
such as, plugged into 2 same CDROM (same VID) with application autorun. it
is possbile that application in cdrom-A will communicate with cdrom-B, how
to avoid it?

url:http://www.ureader.com/msg/1474538.aspx

Loading...