Tech Support > Microsoft Windows > Drivers > How to Get Mount Point from Device Name
How to Get Mount Point from Device Name
Posted by Michelle on February 16th, 2005


I am trying to find the mount point of a partition based on the device name
obtained from Microsoft Cluster. As far as I know, if a drive letter is not
assigned to the partition, the device name returned by Microsoft Cluster is
formatted as "Disk##Partition##". To get the mount point, one way (I am not
sure if it is safe) I have tried is:

------------------------------------------------------------------------------------------
1. Parse the device name to a format like
"\\?\GLOBALROOT\device\Harddisk##\Partition##\ ".

2. GetVolumeNameForVolumeMountPoint() - Using the disk and partition number.
I can use this function to get the unique volume name i.e. pass
\\?\GLOBALROOT\device\Harddisk3\Partition1\
and get back something like
\\?\Volume{1938fbd3-7961-11d9-b611-0050dabfbe25}\

3. GetVolumePathNamesForVolumeName() - Using the unique volume name. I can
use this function to get the path name i.e. pass
\\?\Volume{1938fbd3-7961-11d9-b611-0050dabfbe25}\ and get back the mount
point, eg. Z:\mnt\
--------------------------------------------------------------------------------------------

My concern is about the first step, that is, my code has to rely on the
format of the device name returned by Microsoft Cluster.

Is "Disk##Partition##" the only format of the device name known to Microsoft
Cluster, provided a drive letter is not assigned??? What if the disk is a
remote one?

If the device name is not a good option, are there other ways of getting the
partition's mount point based on the information known to the microsoft
cluster? I have thought about the volume serial number. But I guess the
serial number is only unique in one machine. In a cluster environment, there
might be two partitions formatted by different hosts and have the same serial
number.

Please help!

Posted by Robert Marquardt on February 16th, 2005


From an earlier message here:

Subject:
RE: How to match between physical usb device and its drive letter?
From:
"Kiran" <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;
}