GetVolumeNameForVolumeMountPoint(), SetVolumeMountPoint(), GetVolumePathName()



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Mounting a logical drive to a folder

To show: Using GetVolumeNameForVolumeMountPoint(), SetVolumeMountPoint(), GetVolumePathName() functions


// Mounting a disk or tape

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>



// This program run at the command prompt

// This is wide character/unicode version...

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


BOOL bFlag, test;

// temporary buffer for volume name

char Buf[BUFSIZE];

DWORD cchBufferLength;


// If not enough arguments supplied...

// for this example you have to create the mount point first

if(argc != 3)


printf("\"%S\" command mounts a volume at a mount point.\n", argv[0]);

printf("Usage: %S <mount> <volume>\n", argv[0]);

printf("And make sure the <mount> exists!!!\n");

printf("Example: \"%S c:\\mymnt\\gdrive\\ g:\\\"\n", argv[0]);

// Just exit

return (1);



// Some verification, check the inputs validity

bFlag = GetVolumeNameForVolumeMountPoint(

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

(LPWSTR)Buf, // output volume name buffer

BUFSIZE // size of volume name buffer



if(bFlag != TRUE)


printf("Retrieving volume name for %S failed. Error: %d\n", argv[2], GetLastError());

// Just exit

return (2);


printf("Volume name of %S is %S\n", argv[2], Buf);


bFlag = SetVolumeMountPoint(

(LPCWSTR)argv[1], // mount point

(LPCWSTR)Buf // volume to be mounted



// Another verification, error checking


printf("Attempt to mount %S at %S failed.\n", argv[2], argv[1]);



printf("%S is successfully mounted at %S\n", argv[2], argv[1]);

cchBufferLength = 100;

// If the function succeeds, the return value is nonzero.

// If the function fails, the return value is 0 (zero).

// To get extended error information, call GetLastError.

test = GetVolumePathName(






if(test != 0)

printf("The Volume path name for %S is %S.\n", argv[2], argv[1]);


printf("GetVolumePathName() failed, error %d", GetLastError());


return (bFlag);



Output example:

(This program run at the command prompt)



"cplus" command mounts a volume at a mount point.

Usage: cplus <mount> <volume>

And make sure the <mount> exists!!!

Example: "cplus c:\mymnt\gdrive\ g:\"


F:\vc2005project\cplus\d.e.b.u.g>cplus C:\mymount\ K:\

Volume name of K:\ is \\?\Volume{4039899b-f63b-11d9-9648-806d6172696f}\

K:\ is successfully mounted at C:\mymount\

The Volume path name for K:\ is K:\.





C and C++ Programming Resources | C & C++ Code Example Index