Você está na página 1de 3

Displaying Volume Paths

The following C++ example shows how to display all paths for each volume and device. For each volume in the system, the example locates the volume, obtains the device
name, obtains all paths for that volume, and displays the paths.

C++

#include<windows.h>
#include<stdio.h>

voidDisplayVolumePaths(
__inPWCHARVolumeName
)
{
DWORDCharCount=MAX_PATH+1;
PWCHARNames=NULL;
PWCHARNameIdx=NULL;
BOOLSuccess=FALSE;

for(;;)
{
//
//Allocateabuffertoholdthepaths.
Names=(PWCHAR)newBYTE[CharCount*sizeof(WCHAR)];

if(!Names)
{
//
//Ifmemorycan'tbeallocated,return.
return;
}

//
//Obtainallofthepaths
//forthisvolume.
Success=GetVolumePathNamesForVolumeNameW(
VolumeName,Names,CharCount,&CharCount
);

if(Success)
{
break;
}

if(GetLastError()!=ERROR_MORE_DATA)
{
break;
}

//
//Tryagainwiththe
//newsuggestedsize.
delete[]Names;
Names=NULL;
}

if(Success)
{
//
//Displaythevariouspaths.
for(NameIdx=Names;
NameIdx[0]!=L'\0';
NameIdx+=wcslen(NameIdx)+1)
{
wprintf(L"%s",NameIdx);
}
wprintf(L"\n");
}

if(Names!=NULL)
{
delete[]Names;
Names=NULL;
}

return;
}

void__cdeclwmain(void)
{
DWORDCharCount=0;
WCHARDeviceName[MAX_PATH]=L"";
DWORDError=ERROR_SUCCESS;
HANDLEFindHandle=INVALID_HANDLE_VALUE;
BOOLFound=FALSE;
size_tIndex=0;
BOOLSuccess=FALSE;
WCHARVolumeName[MAX_PATH]=L"";

//
//Enumerateallvolumesinthesystem.
FindHandle=FindFirstVolumeW(VolumeName,ARRAYSIZE(VolumeName));

if(FindHandle==INVALID_HANDLE_VALUE)
{
Error=GetLastError();
wprintf(L"FindFirstVolumeWfailedwitherrorcode%d\n",Error);
return;
}

for(;;)
{
//
//Skipthe\\?\prefixandremovethetrailingbackslash.
Index=wcslen(VolumeName)1;

if(VolumeName[0]!=L'\\'||
VolumeName[1]!=L'\\'||
VolumeName[2]!=L'?'||
VolumeName[3]!=L'\\'||
VolumeName[Index]!=L'\\')
{
Error=ERROR_BAD_PATHNAME;
wprintf(L"FindFirstVolumeW/FindNextVolumeWreturnedabadpath:%s\n",VolumeName);
break;
}

//
//QueryDosDeviceWdoesnotallowatrailingbackslash,
//sotemporarilyremoveit.
VolumeName[Index]=L'\0';

CharCount=QueryDosDeviceW(&VolumeName[4],DeviceName,ARRAYSIZE(DeviceName));

VolumeName[Index]=L'\\';

if(CharCount==0)
{
Error=GetLastError();
wprintf(L"QueryDosDeviceWfailedwitherrorcode%d\n",Error);
break;
}

wprintf(L"\nFoundadevice:\n%s",DeviceName);
wprintf(L"\nVolumename:%s",VolumeName);
wprintf(L"\nPaths:");
DisplayVolumePaths(VolumeName);

//
//Moveontothenextvolume.
Success=FindNextVolumeW(FindHandle,VolumeName,ARRAYSIZE(VolumeName));

if(!Success)
{
Error=GetLastError();

if(Error!=ERROR_NO_MORE_FILES)
{
wprintf(L"FindNextVolumeWfailedwitherrorcode%d\n",Error);
break;
}

//
//Finishediterating
//throughallthevolumes.
Error=ERROR_SUCCESS;
break;
}
}

FindVolumeClose(FindHandle);
FindHandle=INVALID_HANDLE_VALUE;

return;
}

The following is example output from running the application. For each volume, the output includes a volume device path, a volume GUID path, and a drive letter.

Foundadevice:
\Device\HarddiskVolume2
Volumename:\\?\Volume{4c1b02c1d99011dc99ae806e6f6e6963}\
Paths:C:\
Foundadevice:
\Device\CdRom0
Volumename:\\?\Volume{4c1b02c4d99011dc99ae806e6f6e6963}\
Paths:D:\

Related topics
FindFirstVolume
FindNextVolume
FindVolumeClose
GetVolumePathNamesForVolumeName
QueryDosDevice

2017 Microsoft