Results 1 to 6 of 6

Thread: Lock And Unlock multiple Directories

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    9

    Exclamation Lock And Unlock multiple Directories

    Hi friends i need your help, please! im trying to lock and unlock multiple directories (folders) (im creating a folder lock), I can block many folders but only unlock one, when trying to unlock another one vb6 tells me: err 75. I understand that this happens because it's saving or creating the file in only one registry location and need store in a Collection list, but i dont know how (i have no idea). I use this code:

    Code Code:
    1. Private Const FILE_LIST_DIRECTORY = &H1
    2. Private Const FILE_SHARE_READ = &H1&
    3. Private Const FILE_SHARE_DELETE = &H4&
    4. Private Const OPEN_EXISTING = 3
    5. Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
    6. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal PassZero As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal PassZero As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    8.  
    9. Dim File_Share_Flag As Long
    10. Dim hDir As Long
    11.  
    12.  
    13. Private Sub mnublock_Click()
    14.  
    15. 'Block code
    16.             Dim PathDir As String
    17.     PathDir = itemData + Ext
    18.     hDir = CreateFile(PathDir, FILE_LIST_DIRECTORY, File_Share_Flag, _
    19.                       ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
    20.  
    21. End Sub
    22.            
    23.  
    24. Private Sub mnuUnblock_Click()
    25. 'Unblock code
    26. CloseHandle hDir
    27. End Sub

    * Note: itemData read a .ini file, the .ini values are set in a Listbox, some like this:

    code2 Code:
    1. itemData = ReadINI("My files protected", "File" & CStr(Counter), iniPath & "data.ini")


    The problem is when try unlock all the previous locked folders, only one can be unlocked
    thanks a lot for the help And sorry for my poor English

  2. #2
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Lock And Unlock multiple Directories

    Honestly I don't really know what this "locking" is but I can see the problem. Only one is unlocked because you are Closing the handle to hDir. hDir is set to the last CreateFile return value that you got from clicking the mnuBlock. So it's only applying to the very last one. You will need to save all these handles so you can close them all.

    Edit: It looks like you are "locking" the files by literally having the OS lock access to them because there is an open file handle. If you are doing this for security, that is a very poor way to handle it. File security should be handled by either encrypting the file, or by higher level user access control.
    Last edited by danecook21; Nov 10th, 2010 at 12:22 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    9

    Re: Lock And Unlock multiple Directories

    Quote Originally Posted by danecook21 View Post
    Honestly I don't really know what this "locking" is but I can see the problem. Only one is unlocked because you are Closing the handle to hDir. hDir is set to the last CreateFile return value that you got from clicking the mnuBlock. So it's only applying to the very last one. You will need to save all these handles so you can close them all.

    Edit: It looks like you are "locking" the files by literally having the OS lock access to them because there is an open file handle. If you are doing this for security, that is a very poor way to handle it. File security should be handled by either encrypting the file, or by higher level user access control.
    Hi, thanks for your help you're right, blocking is only part of protection, while the program is running the folders can not be eliminated, I know I should use a Collection object and pass the result of CreateFile to the collection's Add method, but I do not know how to implement it, you could help me with an example based on my problem, I appreciate it =)

  4. #4
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Lock And Unlock multiple Directories

    For a collection just:
    Code:
    Dim c As Collection
    c.Add hDir
    Or you could just use an array, type of Long.
    Code:
        Dim arr() As Long 'outside of click event
        
        'after CreateFile call
        ReDim Preserve arr(1 To UBound(arr) + 1) 'make a new element
        arr(UBound(arr)) = hDir 'assign handle to last element
        
        'to close
        Dim l As Long
        For l = 1 To UBound(arr)
            CloseHandle arr(l)
        Next l

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    9

    Re: Lock And Unlock multiple Directories

    Quote Originally Posted by danecook21 View Post
    For a collection just:
    Code:
    Dim c As Collection
    c.Add hDir
    Or you could just use an array, type of Long.
    Code:
        Dim arr() As Long 'outside of click event
        
        'after CreateFile call
        ReDim Preserve arr(1 To UBound(arr) + 1) 'make a new element
        arr(UBound(arr)) = hDir 'assign handle to last element
        
        'to close
        Dim l As Long
        For l = 1 To UBound(arr)
            CloseHandle arr(l)
        Next l
    i try this and can't: maximize (all folders unlocked) minimized (all folders locked) i have the same problem only one can be unlocked =(:

    Max & Min Code:
    1. Private Sub Form_Resize()
    2.  
    3. On Local Error Resume Next
    4.     Dim ret As Long, buff As String
    5.     buff = Space(255)
    6.     ret = GetWindowsDirectory(buff, 255)
    7.     WindowsDirectory = Left$(buff, InStr(buff, vbNullChar) - 1)
    8. Dim Counter As Integer, MaxFls As Integer, sFilePath As String, itemData As String
    9. sFilePath = "C:\"
    10. MaxFls = Val(ReadINI("My protected folders", "MaxFiles", sFilePath & "data.ini"))
    11. If MaxFls < 1 Then Exit Sub
    12. For Counter = 1 To MaxFls
    13. itemData = ReadINI("My protected folders", "File" & CStr(Counter), sFilePath & "data.ini")
    14.  
    15.  
    16.     If (Me.WindowState = vbMinimized) Then
    17.    
    18.     'block
    19.                    
    20.             Dim PathDir As String
    21.     PathDir = itemData + Ext
    22.     hDir = CreateFile(PathDir, FILE_LIST_DIRECTORY, File_Share_Flag, _
    23.                       ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
    24.                      
    25.     Dim arr() As Long
    26.     ReDim Preserve arr(1 To UBound(arr) + 1)
    27.     arr(UBound(arr)) = hDir
    28.            
    29.             'end block code    
    30.    
    31.         Me.Hide
    32.         Call PutSystray
    33.    
    34.    ElseIf (Me.WindowState = vbNormal) Then
    35.    
    36.     'unlock code
    37.     Dim l As Long
    38.     For l = 1 To UBound(arr)
    39.         CloseHandle arr(l)
    40.     Next l
    41.    
    42.     CloseHandle hDir
    43.  
    44.     'end unlock code
    45.    
    46.         Call RemoverSystray
    47.        
    48.     End If
    49. Next
    50. End Sub

  6. #6
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Lock And Unlock multiple Directories

    Well my suggestion was on how to save all the file handles. As for the functionality of your code, I can't really help because I am not familiar with those APIs. Sorry. Perhaps someone else will comment...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width