Sign in to follow this  
Followers 0

LockFile() - Lock a file to the current process only.

24 posts in this topic




Posted

Excellent!

It was missing a UDF with this ability!

Thanks for sharing.

Regards,

João Carlos.

Share this post


Link to post
Share on other sites

Posted

Thanks for sharing.

Share this post


Link to post
Share on other sites

Posted

this could be very usefull for me.

thanks!

Share this post


Link to post
Share on other sites

Posted

Thanks to everyone who voted, liked and posted a message of thanks here.

I only created it for the forum and not for my personal use as I see the question asked quite a bit around here, but perhaps I will find use for it as well.

Share this post


Link to post
Share on other sites

Posted (edited)

I've updated the UDF with fixes to the return values of LockErase & included a new function called LockReduce. Download available in the initial post.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Thats awesome.... accept my greetings :oops:

Regards

Deltarocked

Share this post


Link to post
Share on other sites

Posted

I might add something similar in the future but for now for those who use WinAPIEx.au3 by Yashied check out the following functions to accompany this UDF. Just pass the handle returned by LockFile() to the functions.

_WinAPI_FileInUse

_WinAPI_GetFinalPathNameByHandle

Share this post


Link to post
Share on other sites

Posted (edited)

A sample script. Its a culmination of LockFile, WinAPIEx. A lot more things can be done. Basically, wrote this code to test "Unlocker Assistant", which can unlock any locked file.

Why this? well for a small project.

Regards

DeltaRocked

[seems like some issue with fileinuse... testing]

Well I had to download the WinAPIEx by Yashied and reinstalled it (used the installer). now the below script is working fine. and am using the latest version of Autoit v3.3.8.1.

#cs ----------------------------------------------------------------------------
Functions by Yashied WinAPIEx.au3 _WinAPI_FileInUse, _WinAPI_CreateFileEx
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include "access.au3"
#include "array.au3"
#include "date.au3"
#include "LockFile.au3"
#include <WinAPIEx.au3> ; had to download the latest WinAPIEx.au3
AdlibRegister('CheckLock', 200)

Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $lockStat = 0
Global $sFilePath = @ScriptDir & 'test.mdb'
Global $hLock
$hLock = LockFile($sFilePath, 0)
$lockStat = 1


LockUnlock($hLock)
$lockStat = 0
Sleep(2000)
$val = _accessAddRecord($sFilePath, '', 'tran', '|test|10|0|0|' & _Now() & '|0|0', 2)
$hLock = LockFile($sFilePath, 0)
$lockStat = 1

If $val == 0 Then
    SplashOff()
    SplashTextOn('', 'successfull', 500, 50, -1, -1, 1)
    Sleep(2000)
    SplashOff()
Else
    SplashOff()
    SplashTextOn('', 'unsuccessfull ' & @error, 500, 50, -1, -1, 1)
    Sleep(5000)
    SplashOff()
EndIf
While 1
    ;this loop is used to test unlocker assistant which can unlock locked files.
    Sleep(1000)
WEnd

Exit

Func CheckLock()
    If _WinAPI_FileInUse($sFilePath) == 0 Then
        If $lockStat == 1 Then
            $hLock = LockFile($sFilePath, 0)
            ConsoleWrite('locked ' & $lockStat & @CRLF)
        Else
            ConsoleWrite('unlocked by app' & _WinAPI_FileInUse($sFilePath) & $lockStat & @CRLF)
        EndIf
    EndIf
EndFunc   ;==>CheckLock
Edited by deltarocked

Share this post


Link to post
Share on other sites

Posted

Thanks deltarocked, but I'm getting a lot of errors with your code.

Share this post


Link to post
Share on other sites

Posted (edited)

Hi Guinness,

I modified the script (which is now posted in the previous post) and also had to download the latest version of WinAPIEx .

The output of the running script:

Secondly, you will have to create a test.mdb and then execute the addrecord function. I am using a modified version of addrecord (which uses password as one of the parameter) I think in the original UDF password option is not available.

Last but not the least - for a faction of a second the DB is unlocked but you can open the DB in exclusive mode so that it is again inaccessible.

>Running:(3.3.8.1):C:Program FilesAutoIt3autoit3.exe "C:Documents and SettingsDesktopProd_LockNew AutoIt v3 Script.au3"

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

unlocked by app00

locked 1 -----> this happens when I am using unlocker assistant and the file is test.mdb

Func _accessAddRecord($adSource, $adPwd, $adTable, $rData, $adCol)
If Not IsArray($rData) Then
  $rData = StringSplit($rData, '|')
EndIf
$oADO = 'ADODB.Connection'
If IsObj($oADO) Then
  $oADO = ObjGet('', $oADO)
Else
  $oADO = _dbOpen($adSource, $adPwd)
EndIf
If IsObj($oADO) = 0 Then Return SetError(1)
If Not IsObj($oADO) Then Return SetError(2, 0, 0)
$oRec = _dbOpenRecordset();ObjCreate("ADODB.Recordset")
If IsObj($oRec) = 0 Then Return SetError(2)
With $oRec
  .Open("SELECT * FROM " & $adTable, $oADO, $adOpenStatic, $adLockOptimistic)
  .AddNew
  If IsArray($rData) Then
   For $I = $adCol To $rData[0]
    $rData[$I] = StringStripWS($rData[$I], 1)
;~   ConsoleWrite(.Fields.Item($I-1).name &'--'&$rData[$I]&@CRLF)
    .Fields.Item($I-1) = $rData[$I]
   Next
  Else
   .Fields.Item($adCol) = $rData
  EndIf
  .Update
  .Close
EndWith
$oADO.Close()
EndFunc   ;==>_accessAddRecord

Func _dbOpen($adSource, $adPwd)
$oADO = ObjCreate("ADODB.Connection")
$oADO.Provider = $adoProvider
If $adPwd <> '' Then $oADO.Properties("Jet OLEDB:Database Password") = $adPwd
$oADO.Open($adSource)
Return $oADO
EndFunc   ;==>_dbOpen

Func _dbOpenRecordset()
$oRec = ObjCreate("ADODB.Recordset")
Return $oRec
EndFunc   ;==>_dbOpenRecordset
Edited by deltarocked

Share this post


Link to post
Share on other sites

Posted

I don't use Access but I'm glad you're using my UDF to your advantage. Any ideas for new functions let me know and I'll see what I can come up with.

Share this post


Link to post
Share on other sites

Posted

good script

can you create gui that contain folder open dialog and sub folders or files

after that choise some folder to lock it and another bottun to unlock it

Share this post


Link to post
Share on other sites

Posted

good script

can you create gui that contain folder open dialog and sub folders or files

after that choise some folder to lock it and another bottun to unlock it

This is going beyond the idea of the UDF, but there is nothing stopping you from creating it.

Share this post


Link to post
Share on other sites

Posted (edited)

Updated the UDF will only a few cosmetic changes. I also won't be adding _WinAPI_FileInUse or _WinAPI_GetFinalPathNameByHandle, since the latest AutoIt beta/alpha versions contain WinAPIEx.au3, so seems silly to duplicate code that already exists.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Nice UDF, 5*.

Br, FireFox.

Share this post


Link to post
Share on other sites

Posted

Cheers FireFox. Much appreciated.

Share this post


Link to post
Share on other sites

Posted

To make it clear, this UDF is for those who want to 'lock' a file to the current process. If you have no idea as to why you would be required to do that, then you don't need this UDF.

Share this post


Link to post
Share on other sites

Posted

Nice script I think I can find a good use for this.

Is this also possible to do it the other way around.

So to unlock a file that is locked?

Share this post


Link to post
Share on other sites

Posted

Not to my knowledge. You would need to do a bit more work to the UDF to 'unlock' a file by another process, like Unlocker.

Search the Forum as some users have tried to create an Unlocker like application.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.