Win32 Windows Volume Program and Code Example 15

 

 

 

 

 

Mounted Folder Program Examples

 

The following examples illustrate the mounted folder functions which include:

  1. Displaying Volume Paths

  2. Editing Drive Letter Assignments

  3. Creating a Mounted Folder

  4. Enumerating Volume GUID Paths

  5. 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.

 

Creating Mounted Folders Programmatically - creating a new project in Visual Studio 2008

 

 

Creating Mounted Folders Programmatically - creating Win32 console application project

 

Select the Next button to refine the project template properties.

 

Creating Mounted Folders Programmatically - refining the win32 application project

 

Select the Empty project tick box and leave others as if. Click the Finish button.

 

Creating Mounted Folders Programmatically - selecting the empty project type

 

Add the source file and give a suitable name. Select Project > Add New Item menu.

 

Creating Mounted Folders Programmatically - adding a new item to the existing Win32 project

 

Or, select the Source Files folder in Solution Explorer > Right click mouse > select Add menu > select New Item sub menu.

 

Creating Mounted Folders Programmatically - visual studio adding new item short cut

 

Select Code for Categories: and C++ File (.cpp) for Templates:. Give a suitable source file name.

 

Creating Mounted Folders Programmatically - adding C++ source file to the existing Win32 project

 

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 L"Failed!";

        }

 

        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 L"Failed";

}

 

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(L"FindFirstVolumeW() failed with error code %d\n", Error);

        return;

    }

      else

            wprintf(L"FindFirstVolumeW() 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(L"FindFirstVolumeW()/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(L"QueryDosDeviceW() failed with error code %d\n", GetLastError());

            break;

        }

            else

                  wprintf(L"QueryDosDeviceW() is OK!\n");

 

        wprintf(L"\nFound a device: %s", DeviceName);

        wprintf(L"\nVolume name: %s", VolumeName);

        ret = DisplayVolumePaths(VolumeName);

        wprintf(L"Paths: %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.

 

Creating Mounted Folders Programmatically - a sample console output showing dos devices

 

 

 

  < Windows Volume 14 | Win32 Programming Index | Windows Volume Index | Windows Volume 16 >