Library code snippets

Virtual Drive

Here is all that you need to know to create a virtual CD program. First create a module and insert the following code.

Public Type BrowseInfo
   lngHwnd As Long
   pIDLRoot As Long
   pszDisplayName As Long
   lpszTitle As Long
   ulFlags As Long
   lpfnCallback As Long
   lParam As Long
   iImage As Long
End Type

Public Const BIF_RETURNONLYFSDIRS = 1
Public Const MAX_PATH = 260

Declare Sub CoTaskMemFree Lib "ole32.dll" _
(ByVal hMem As Long)

Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, _
ByVal lpString2 As String) As Long

Declare Function SHBrowseForFolder Lib "shell32" _
(lpbi As BrowseInfo) As Long

Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, ByVal lpBuffer As String) As Long

Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Global Drives(1 To 26) As String

Public Function BrowseForFolder(ByVal lngHwnd As Long, ByVal strPrompt As String) As String

   On Error GoTo ehBrowseForFolder 'Trap for errors
   
   Dim intNull As Integer
   Dim lngIDList As Long, lngResult As Long
   Dim strPath As String
   Dim udtBI As BrowseInfo
   
   'Set API properties (housed in a UDT)
   With udtBI
       .lngHwnd = lngHwnd
       .lpszTitle = lstrcat(strPrompt, "")
       .ulFlags = BIF_RETURNONLYFSDIRS
   End With
   
   'Display the browse folder...
   lngIDList = SHBrowseForFolder(udtBI)
   
   If lngIDList <> 0 Then
       'Create string of nulls so it will fill in with the path
       strPath = String(MAX_PATH, 0)
       
       'Retrieves the path selected, places in the null
       'character filled string
       lngResult = SHGetPathFromIDList(lngIDList, strPath)
       
       'Frees memory
       Call CoTaskMemFree(lngIDList)
       
       'Find the first instance of a null character,
       'so we can get just the path
       intNull = InStr(strPath, vbNullChar)
       'Greater than 0 means the path exists...
       If intNull > 0 Then
           'Set the value
           strPath = Left(strPath, intNull - 1)
       End If
   End If
   
   'Return the path name
   BrowseForFolder = strPath
   Exit Function 'Abort
   
ehBrowseForFolder:
   
   'Return no value
   BrowseForFolder = Empty

End Function

Sub SetDrives()
Drives(1) = "a:"
Drives(2) = "b:"
Drives(3) = "c:"
Drives(4) = "d:"
Drives(5) = "e:"
Drives(6) = "f:"
Drives(7) = "g:"
Drives(8) = "h:"
Drives(9) = "i:"
Drives(10) = "j:"
Drives(11) = "k:"
Drives(12) = "l:"
Drives(13) = "m:"
Drives(14) = "n:"
Drives(15) = "o:"
Drives(16) = "p:"
Drives(17) = "q:"
Drives(18) = "r:"
Drives(19) = "s:"
Drives(20) = "t:"
Drives(21) = "u:"
Drives(22) = "v:"
Drives(23) = "w:"
Drives(24) = "x:"
Drives(25) = "y:"
Drives(26) = "z:"
End Sub

'end module code


Then, add the following code to a form.

Sub MountVirtualDrive(strVirtualDrive, strPhysicPath)
Shell "subst.exe " & strVirtualDrive & Chr(32) & strPhysicPath, vbHide
End Sub

Sub UnMountVirtualDrive(strVirtualDrive)
Shell "subst.exe " & strVirtualDrive & " /d", vbHide
End Sub
'end form code

Now all that you have to do to add a virtual drive is call MountVirtualDrive("g:","g:\my mounted folder"). To remove a virtual drive just do UnMountVirtualDrive("f:")

Comments

  1. 07 Jun 2007 at 20:53

    Hello,

    I have the same problem. have you found a solution?

    Cheers,

    Roeland

  2. 21 Feb 2007 at 21:01
    Hi,

    I have the same problem, so do you have any idea for make that?



  3. 23 Dec 2006 at 12:05

    Hey Friends,
    I want to develop a virtual storage drive project in .Net. but confused about how

    to start?
    I have alerady created a web drive solution which working using WebDav protocol,

    but it uses Iexplorer to display files and folders.
    Whereas i want to display the folders and files in Windows explorer (Something

    exactly like What Gdrive software does)

    I want to something like this..
    Once i input "https://<server address>", "User Name", "Password". I want to get

    the all of my folders and files from taht server folder should be displayed in

    windows explorer

    Can anyone help me to distingush the webdrive and virtual windows drive?
    Please post the articles or sourcecode related to this topic..

    Thanks in advance

  4. 23 Dec 2006 at 12:02

    Hey syed..

    I have same problem....

    I also want to map virtual drive in windows 2000 to some webserver folder or ftp folders and allow client to work as his physical drive..

    can any body help this out?

    I have already made a mapping by asp.net but it is just working in internet explorer window..

    Thanks in advance

     

  5. 07 Aug 2006 at 20:05

    Sorry for being a bit of a thick. Please can tell me how you got the code to work. I'm using VB 2005 express edition, and have managed to build and execute the code. But all I get is a virtual hard drive. I don't see how the code in the module gets called. Its the same as if I  use subst from the command line.

    Regards ianp

  6. 11 Dec 2005 at 03:49

    I have XP with my primary drive NTFS but I intend to use this program for an external FAT32 drive. Will I still risk NTFS corruption, or am I safe with the FAT32?

  7. 02 Nov 2004 at 00:53

    OK..nice code.. but how about this..can anybody help e to solve this problems.


    -->i'm planning to create the virtual drive in windows 2000 and map some webserver folder or ftp folders to that  
        drive and allow client to work as his physical drive..
    --> pls Help me as soon as possible

  8. 02 Nov 2003 at 03:32

    But here's a much cleaner method...




    Option Explicit


    Private Const DDDREMOVEDEFINITION As Long = &H2


    Private Declare Function DefineDosDevice Lib "kernel32" Alias "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName As String, Optional ByVal lpTargetPath As String = vbNullString) As Long



    Public Function Subst(ByVal Drive As String, ByVal Path As String) As Boolean
      Subst = CBool(DefineDosDevice(0, Drive, Path))
    End Function


    Public Function UnSubst(ByVal Drive As String) As Boolean
      UnSubst = CBool(DefineDosDevice(DDDREMOVEDEFINITION, Drive))
    End Function




    ...and an example...



    If Not Subst("P:", "C:\My Documents\Shameless Porn") Then MsgBox Err.LastDLLError


    ...and, subsequently....


    If Not UnSubst("P:") Then MsgBox Err.LastDLLError



    ... Groovy, no?

  9. 02 Jun 2003 at 12:20

    Be very careful using the SUBST command on WinNT/2000/XP, it is dangerous.  NTFS permissions can conflict with each other say drive F: is actually a drive created from a subdir of Drive C:, applying NTFS permissions to drive C: that conflict with drive F: can cause NTFS corruption.  You may find yourself logged in as Admin and no longer able to access files or whole directories due to the corruption.  Microsoft has acknowledged this and suggests against the usgae of SUBST on WinNT based machines.  Win9x/ME does not have this issue due to lack of NTFS.

  10. 19 May 2003 at 12:50

    It really works!
    And fast too!!

  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Virtual Drive.

Leave a comment

Sign in or Join us (it's free).

 joshualaymon

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback