Mounted Folder Program Examples
The following examples illustrate the mounted folder functions which include:
Displaying Volume Paths
Editing Drive Letter Assignments
Creating a Mounted Folder
Enumerating Volume GUID Paths
Deleting a Mounted Folder
Displaying Volume Paths Program Example
The following 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.
Create a new Win32 console application project and give a suitable project name.
Select the Next button to refine the project template properties.
Select the Empty project tick box and leave others as if. Click the Finish button.
Add the source file and give a suitable name. Select Project > Add New Item menu.
Or, select the Source Files folder in Solution Explorer > Right click mouse > select Add menu > select New Item sub menu.
Select Code for Categories: and C++ File (.cpp) for Templates:. Give a suitable source file name.
Add the following source code. Build and run the project.
// Compile As C++ Code (/TP) and using Unicode character set
#include <windows.h>
#include <stdio.h>
PWCHAR DisplayVolumePaths(PWCHAR VolumeName)
{
DWORD CharCount = MAX_PATH + 1;
PWCHAR Names = NULL;
PWCHAR NameIdx = NULL;
BOOL Success = FALSE;
for (;;)
{
// Allocate a buffer to hold the paths.
Names = (PWCHAR) new BYTE[CharCount * sizeof(WCHAR)];
if (!Names)
{
// If memory can't be allocated, return.
wprintf(L\n Failed to allocate memory!\n);
return LFailed!;
}
wprintf(L\n Memory allocation for buffer should be fine!);
// Obtain all of the paths for this volume.
Success = GetVolumePathNamesForVolumeNameW(VolumeName, Names, CharCount, &CharCount);
if (Success)
{
wprintf(L\n GetVolumePathNamesForVolumeNameW() should be fine!\n);
// Break the loop
break;
}
if (GetLastError() != ERROR_MORE_DATA)
{
wprintf(L\n GetVolumePathNamesForVolumeNameW() failed with error code %d\n, GetLastError());
break;
}
// Try again with the new suggested size
delete [] Names;
Names = NULL;
}
if (Success)
{
// Display the various paths
for (NameIdx = Names; NameIdx[0] != L'\0'; NameIdx += wcslen(NameIdx) + 1 )
{
return NameIdx;
}
wprintf(L\n);
}
// Return the allocated buffer to the system
if (Names != NULL)
{
delete [] Names;
Names = NULL;
}
return LFailed;
}
void wmain(void)
{
DWORD CharCount = 0;
WCHAR DeviceName[MAX_PATH] = L;
DWORD Error = ERROR_SUCCESS;
HANDLE FindHandle = INVALID_HANDLE_VALUE;
BOOL Found = FALSE;
size_t Index = 0;
BOOL Success = FALSE;
WCHAR VolumeName[MAX_PATH] = L;
PWCHAR ret = L;
// Enumerate all volumes in the system.
FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
if (FindHandle == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
wprintf(LFindFirstVolumeW() failed with error code %d\n, Error);
return;
}
else
wprintf(LFindFirstVolumeW() should be OK!\n);
for (;;)
{
// Skip the \\?\ prefix and remove the trailing backslash.
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(LFindFirstVolumeW()/FindNextVolumeW() returned a bad path: %s\n, VolumeName);
break;
}
// Retrieves information about MS-DOS device names.
// QueryDosDeviceW() doesn't allow a trailing backslash, so temporarily remove it.
VolumeName[Index] = L'\0';
CharCount = QueryDosDeviceW(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName));
VolumeName[Index] = L'\\';
// Returned value = 0 means failed
if ( CharCount == 0 )
{
wprintf(LQueryDosDeviceW() failed with error code %d\n, GetLastError());
break;
}
else
wprintf(LQueryDosDeviceW() is OK!\n);
wprintf(L\nFound a device: %s, DeviceName);
wprintf(L\nVolume name: %s, VolumeName);
ret = DisplayVolumePaths(VolumeName);
wprintf(LPaths: %s\n, ret);
// Move on to the next volume.
Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));
if (!Success)
{
if (GetLastError() != ERROR_NO_MORE_FILES)
{
wprintf(L\nFindNextVolumeW() failed with error code %d\n, GetLastError());
break;
}
// Finished iterating through all the volumes.
Error = ERROR_SUCCESS;
break;
}
else
wprintf(L\nFindNextVolumeW() should be fine!\n);
}
// Closes the volume search handle
if( FindVolumeClose(FindHandle) == 0)
wprintf(L\nFindVolumeClose() failed with error code %s\n, GetLastError());
else
wprintf(L\nFindVolumeClose() is OK!\n);
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.