|
-
Nov 10th, 2010, 01:12 AM
#1
Thread Starter
New Member
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:
Private Const FILE_LIST_DIRECTORY = &H1
Private Const FILE_SHARE_READ = &H1&
Private Const FILE_SHARE_DELETE = &H4&
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Dim File_Share_Flag As Long
Dim hDir As Long
Private Sub mnublock_Click()
'Block code
Dim PathDir As String
PathDir = itemData + Ext
hDir = CreateFile(PathDir, FILE_LIST_DIRECTORY, File_Share_Flag, _
ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
End Sub
Private Sub mnuUnblock_Click()
'Unblock code
CloseHandle hDir
End Sub
* Note: itemData read a .ini file, the .ini values are set in a Listbox, some like this:
code2 Code:
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
-
Nov 10th, 2010, 12:17 PM
#2
Hyperactive Member
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.
-
Nov 10th, 2010, 12:45 PM
#3
Thread Starter
New Member
Re: Lock And Unlock multiple Directories
 Originally Posted by danecook21
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 =)
-
Nov 10th, 2010, 12:50 PM
#4
Hyperactive Member
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
-
Nov 10th, 2010, 02:34 PM
#5
Thread Starter
New Member
Re: Lock And Unlock multiple Directories
 Originally Posted by danecook21
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:
Private Sub Form_Resize()
On Local Error Resume Next
Dim ret As Long, buff As String
buff = Space(255)
ret = GetWindowsDirectory(buff, 255)
WindowsDirectory = Left$(buff, InStr(buff, vbNullChar) - 1)
Dim Counter As Integer, MaxFls As Integer, sFilePath As String, itemData As String
sFilePath = "C:\"
MaxFls = Val(ReadINI("My protected folders", "MaxFiles", sFilePath & "data.ini"))
If MaxFls < 1 Then Exit Sub
For Counter = 1 To MaxFls
itemData = ReadINI("My protected folders", "File" & CStr(Counter), sFilePath & "data.ini")
If (Me.WindowState = vbMinimized) Then
'block
Dim PathDir As String
PathDir = itemData + Ext
hDir = CreateFile(PathDir, FILE_LIST_DIRECTORY, File_Share_Flag, _
ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
Dim arr() As Long
ReDim Preserve arr(1 To UBound(arr) + 1)
arr(UBound(arr)) = hDir
'end block code
Me.Hide
Call PutSystray
ElseIf (Me.WindowState = vbNormal) Then
'unlock code
Dim l As Long
For l = 1 To UBound(arr)
CloseHandle arr(l)
Next l
CloseHandle hDir
'end unlock code
Call RemoverSystray
End If
Next
End Sub
-
Nov 10th, 2010, 02:50 PM
#6
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|