Win32 Windows Volume Program and Code Example 17




Creating a Mounted Folder Program Example


From the user point of view we can mount a drive/volume/partition to an NTFS empty folder as stated in the following steps. This will allow you to use a hard-drive as a normal folder on your main system drive and will be useful in many cases in managing your local storage. For example if you have made your original system drive/partition too small for your growing software needs or if you just want to be able to access multiple drives via one single drive letter.

The following sample demonstrates how to create a mounted folder programmatically. This sample uses the following functions: GetVolumeNameForVolumeMountPoint() and SetVolumeMountPoint().

Create a new Win32 console application project and give a suitable project name.


Creating a Mounted Folder Program Example - creating a new Win32 project




Add the source file and give a suitable name.


Creating a Mounted Folder Program Example - adding a new C++ source file


Add the following source code.


#include <windows.h>

#include <stdio.h>


// The following #define already defined in the SDK

// For Windows Server 2003, Windows XP

// #define _WIN32_WINNT 0x0501




int wmain(int argc, WCHAR *argv[])


   BOOL bFlag;

   WCHAR Buf[BUFSIZE];     // temporary buffer for volume name


   if(argc != 3)


      wprintf(LUsage: %s <mount_point> <volume>\n, argv[0] );

      wprintf(L  Example, \%s c:\\mnt\\fdrive\\ f:\\\\n, argv[0]);

      wprintf(L      ...mount the f: volume to c:\\mnt\\fdrive folder\n);




   // We should do some error checking on the inputs. Make sure

   // there are colons and backslashes in the right places, etc.

   bFlag = GetVolumeNameForVolumeMountPoint(

              argv[2], // input volume mount point or directory

                  Buf, // output volume name buffer

              BUFSIZE  // size of volume name buffer



   if (bFlag != TRUE)


      wprintf(LRetrieving volume name for %s failed.\n, argv[2]);

      return (-2);



   wprintf(LVolume name of %s is %s\n, argv[2], Buf);

   bFlag = SetVolumeMountPoint(

              argv[1], // mount point

                  Buf    // volume to be mounted



   if (!bFlag)


         wprintf(LAttempt to mount %s at %s failed.\n, argv[2], argv[1]);

         wprintf(LError code is %d\n, GetLastError());



         wprintf(L%s was mounted on %s successfully!\n, argv[2], argv[1]);


   return (bFlag);



Build and run the project. The following screenshot is an output sample.


Creating a Mounted Folder Program Example - mounting a fixed D drive on a folder


The following sample output shows a thumb drive (G:) was mounted on the C:\testmountdrive folder.


Creating a Mounted Folder Program Example - mounting a removable, G thumb drive on a folder



The following Figure shows the mounted drive on folder testmountdrive seen in the Windows explorer and the property page of the mounted drive.


Creating a Mounted Folder Program Example - a mounted drive seen in Windows explorer



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