Using Win32 functions in Visual FoxPro Image Gallery
File System
..msdn
DeleteVolumeMountPoint
FindFirstVolume
FindFirstVolumeMountPoint
FindNextVolume
FindNextVolumeMountPoint
FindVolumeClose
FindVolumeMountPointClose
GetCompressedFileSize
GetDiskFreeSpace
GetDiskFreeSpaceEx
GetFileSize
GetFileSizeEx
GetLogicalDrives
GetLogicalDriveStrings
GetVolumeInformation
GetVolumeNameForVolumeMountPoint
GetVolumePathNamesForVolumeName
SetVolumeLabel
SetVolumeMountPoint
Code examples:
Enumerating Volumes and Volume Mounting Points (NTFS)
Enumerating Volumes and Volume Mounting Points (NTFS)

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Before you begin:
The Windows 2000 NTFS file system and later support volume mount points, which are directories on a volume that an application can use to "mount" a different volume, that is, to set it up for use at the location a user specifies.

After the volume mount points have been established, they are maintained through computer restarts automatically.

The picture shows local CDDRIVE device mounted to D:\mnt\cddrive directory.

Handle this set of functions with care. Do not unmount root directories like C:, unless you know what you are doing.

MSDN article How to create and manipulate NTFS junction points covers the usage of Mountvol.exe, Linkd.exe and Delrp.exe utilities.


LOCAL oVolumes As TVolumes, oVolume As TVolume, oMountPoint
oVolumes = CREATEOBJECT("TVolumes")

*!*        ? "[" + oVolumes.PointToVolume("Z") + "]"
*!*        ? oVolumes.MountVolume("d:\mnt\cddrive\", "E")
*!*        ? oVolumes.UnmountVolume("d:\mnt\cddrive\")

FOR EACH oVolume IN oVolumes.volumes
        WITH oVolume
                ?.volumename, .volumename1,;
                        .serialnumber, .drvletter

                FOR EACH oMountPoint IN .mountpoints
                        ? oMountPoint
                NEXT
        ENDWITH
NEXT
DEFINE CLASS TVolumes As Session
#DEFINE INVALID_HANDLE_VALUE -1
#DEFINE MAX_PATH 260
    volumes=NULL

PROCEDURE Init
    THIS.declare
    THIS.volumes = CREATEOBJECT("Collection")
    THIS.EnumVolumes

PROCEDURE MountVolume(cMountPath, cDriveLetter)
* works similar to MOUNTVOL.EXE utility
* usage: MountVolume("d:\mnt\cddrive\", "E")
* everything is strictly local
* Directory "d:\mnt\cddrive\" must exist
* CDROM must exist with logical drive letter E assigned
* if successful, the CDROM can be accessed via the path on D:

    LOCAL cVolume
    cVolume = THIS.PointToVolume(cDriveLetter)
    IF EMPTY(cVolume)
        RETURN .F.
    ENDIF
RETURN (SetVolumeMountPoint(cMountPath, cVolume) <> 0)

PROCEDURE UnmountVolume(cMountPath)
* a trailing backslash is required
    IF LEN(cMountPath) <= 3
    * I suggest do not unmount root directories
    * like C:\, D:\ and so on
        RETURN .F.
    ENDIF
RETURN (DeleteVolumeMountPoint(cMountPath) <> 0)

FUNCTION PointToVolume(cDriveLetter) As String
* returns a gibberish for a given logical drive letter
* example: "D" --> \\?\Volume{...}\
    LOCAL cBuffer
    cBuffer = REPLICATE(CHR(0), MAX_PATH)
    = GetVolumeNameForVolumeMountPoint(cDriveLetter + ":\",;
        @cBuffer, LEN(cBuffer))
RETURN STRTRAN(cBuffer, CHR(0), "")

PROCEDURE EnumVolumes
    DO WHILE THIS.volumes.Count > 0
        THIS.volumes.Remove(1)
    ENDDO

    LOCAL hFind, cBuffer

    cBuffer = REPLICATE(CHR(0), MAX_PATH+1)
    hFind = FindFirstVolume(@cBuffer, LEN(cBuffer))

    IF hFind = INVALID_HANDLE_VALUE
        RETURN
    ENDIF

    DO WHILE .T.
        LOCAL oVolume As TVolume

        oVolume = CREATEOBJECT("TVolume",;
            STRTRAN(cBuffer, CHR(0), ""))

        THIS.volumes.Add(oVolume)
        oVolume=NULL

        cBuffer = REPLICATE(CHR(0), MAX_PATH+1)
        IF FindNextVolume(hFind, @cBuffer, LEN(cBuffer)) = 0
            EXIT
        ENDIF
    ENDDO
    = FindVolumeClose(hFind)

PROCEDURE declare
    DECLARE INTEGER SetVolumeMountPoint IN kernel32;
        STRING lpszVolumeMountPoint, STRING lpszVolumeName

    DECLARE INTEGER DeleteVolumeMountPoint IN kernel32;
        STRING lpszVolumeMountPoint

    DECLARE INTEGER GetVolumeNameForVolumeMountPoint IN kernel32;
        STRING lpszVolumeMountPoint, STRING @lpszVolumeName,;
        LONG cchBufferLength

    DECLARE INTEGER GetVolumePathNamesForVolumeName IN kernel32;
        STRING lpszVolumeName, STRING @lpszVolumePathNames,;
        LONG cchBufferLength, LONG @lpcchReturnLength

    DECLARE INTEGER FindFirstVolume IN kernel32;
        STRING lpszVolumeName, LONG cchBufferLength

    DECLARE INTEGER FindVolumeClose IN kernel32;
        INTEGER hFindVolume

    DECLARE INTEGER FindNextVolume IN kernel32;
        INTEGER hFindVolume, STRING @lpszVolumeName,;
        LONG cchBufferLength

    DECLARE INTEGER FindFirstVolumeMountPoint IN kernel32;
        STRING lpszRootPathName, STRING @lpszVolumeMountPoint,;
        LONG cchBufferLength

    DECLARE INTEGER FindNextVolumeMountPoint IN kernel32;
        INTEGER hFindVolumeMountPoint, STRING @lpszVolumeMountPoint,;
        LONG cchBufferLength

    DECLARE INTEGER FindVolumeMountPointClose IN kernel32;
        INTEGER hFindVolumeMountPoint

    DECLARE INTEGER GetVolumeInformation IN kernel32;
        STRING lpRootPathName, STRING @lpVolumeNameBuffer,;
        LONG nVolumeNameSize, LONG @lpVolumeSerialNumber,;
        LONG @lpMaximumComponentLength, LONG @lpFlags,;
        STRING @lpFileSystemNameBuffer, LONG nFileSystemNameSize

ENDDEFINE

DEFINE CLASS TVolume As Session
    volumename=""
    filesystemname=""
    volumename1=""
    serialnumber=""
    maxfilenamelen=0
    mountpoints=NULL
    pathnames=NULL
    drvletter=""

PROCEDURE Init(cName)
    THIS.volumename = m.cName
    THIS.GetInfo

    THIS.pathnames = CREATEOBJECT("Collection")
    THIS.EnumPathNames

    THIS.mountpoints = CREATEOBJECT("Collection")
    THIS.EnumMountPoints

PROCEDURE EnumPathNames
    THIS.drvletter=""
    DO WHILE THIS.pathnames.Count > 0
        THIS.pathnames.Remove(1)
    ENDDO

    LOCAL cBuffer, nBufsize, nCount, nIndex
    nBufsize=0
    cBuffer = REPLICATE(CHR(0), 0x4000)

    GetVolumePathNamesForVolumeName(THIS.volumename,;
        @cBuffer, LEN(cBuffer), @nBufsize)

    cBuffer = SUBSTR(cBuffer, 1, AT(CHR(0)+CHR(0), cBuffer))

    nCount = ALINES(arrPathNames, cBuffer, .T., CHR(0))
    THIS.drvletter = arrPathNames[1]
    FOR nIndex=1 TO nCount
        THIS.pathnames.Add(arrPathNames[nIndex])
    NEXT
    RELEASE arrPathNames

PROCEDURE GetInfo
    LOCAL cVNBuffer, nSNBuffer, nMaxCmpLen,;
        nFlags, cOSBuffer, nResult

    STORE REPLICATE(CHR(0), MAX_PATH+1) TO cVNBuffer, cOSBuffer
    STORE 0 TO nSNBuffer, nMaxCmpLen, nFlags

    nResult = GetVolumeInformation(THIS.volumename,;
        @cVNBuffer, LEN(cVNBuffer), @nSNBuffer,;
        @nMaxCmpLen, @nFlags, @cOSBuffer, LEN(cOSBuffer))

    IF nResult <> 0
        THIS.filesystemname=STRTRAN(cOSBuffer, CHR(0),"")
        THIS.volumename1=STRTRAN(cVNBuffer, CHR(0),"")
        THIS.serialnumber=TRANSFORM(nSNBuffer, "@0")
        THIS.maxfilenamelen=nMaxCmpLen
    ENDIF

PROCEDURE EnumMountPoints
    DO WHILE THIS.mountpoints.Count > 0
        THIS.mountpoints.Remove(1)
    ENDDO

    LOCAL hFind, cBuffer

    cBuffer = REPLICATE(CHR(0), MAX_PATH+1)
    hFind = FindFirstVolumeMountPoint(THIS.volumename,;
        @cBuffer, LEN(cBuffer))

    IF hFind = INVALID_HANDLE_VALUE
    * 18=ERROR_NO_MORE_FILES
    * 21=ERROR_NOT_READY
    * 123=ERROR_INVALID_NAME
        RETURN
    ENDIF

    DO WHILE .T.
        THIS.mountpoints.Add(STRTRAN(cBuffer, CHR(0), ""))

        cBuffer = REPLICATE(CHR(0), MAX_PATH+1)
        IF FindNextVolumeMountPoint(hFind,;
            @cBuffer, LEN(cBuffer)) = 0
            EXIT
        ENDIF
    ENDDO
    = FindVolumeMountPointClose(hFind)

ENDDEFINE



User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
5494 bytes  
Created: 2001-08-03 12:00:00  
Modified: 2005-12-18 08:52:00  
Visits in 7 days: 153  
Listed functions:
DeleteVolumeMountPoint
FindFirstVolume
FindFirstVolumeMountPoint
FindNextVolume
FindNextVolumeMountPoint
FindVolumeClose
FindVolumeMountPointClose
GetVolumeInformation
GetVolumeNameForVolumeMountPoint
GetVolumePathNamesForVolumeName
SetVolumeMountPoint
Printer friendly API declarations
My comment:
Related Registry keys:
HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2

* * *
Function GetVolumeNameForVolumeMountPoint returns the volume name for a given drive letter. Same do the Volume names enumeration calls. The volume names, I think, can be used to uniquely identify computers. Example: \\?\Volume{9da8b072-8130-22d6-ff8f-806d6172699f}\
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright 2001-2018 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.6.39), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.226.41.91)
1.4 hrs.Function: 'NetShareSetInfo'
Function group: 'Network Management'
1.41 hrs.Examples
1.42 hrs.Function: 'GetConsoleScreenBufferInfo'
Function group: 'Console'
 Function: 'CeRapiInit'
Function group: 'Remote Application Programming (RAPI)'
1.43 hrs.All Functions
 Links
1.44 hrs.Updates
 Example: 'Pocket PC: base class'
1.45 hrs.Function: 'CeRapiInitEx'
Function group: 'Remote Application Programming (RAPI)'
 All Functions
Google
Advertise here!