Jump to content
Sign in to follow this  
Jango

LockFile Function

Recommended Posts

Jango

Hello,

I'm looking for a way to lock a file for exclusive access and i found this:

LockFile Function

But how to do it in AutoIt ? Is a FileOpen sufficient ?

Share this post


Link to post
Share on other sites
KaFu

Try if this works for you, then I'll create a UDF of it ^_^...

#include <winapi.au3>

$File = @ScriptDir & '\test.txt'

$hFile = _WinAPI_CreateFile($File,2,2,2)

DllCall("Kernel32.dll", "hwnd", "LockFile", "hwnd", $hFile, "dword", 1, "dword", 0, "dword", 1, "dword", 0)

while MsgBox(0,'','File Locked') <> 1
    sleep(10)
WEnd

_WinAPI_CloseHandle($hFile)

while MsgBox(0,'','File Unlocked') <> 1
    sleep(10)
WEnd

or this, even easier :) ...

$File = @ScriptDir & '\test.txt'

$hFile = FileOpen($File,1)

DllCall("Kernel32.dll", "hwnd", "LockFile", "hwnd", $hFile, "dword", 1, "dword", 0, "dword", 1, "dword", 0); Set file lock

FileWriteLine($hFile,"line1")
FileWriteLine($hFile,"line2")
FileWriteLine($hFile,"line3")

while MsgBox(0,'','File Locked') <> 1
    sleep(10)
WEnd

FileClose($hFile); unlocks file

while MsgBox(0,'','File Unlocked') <> 1
    sleep(10)
WEnd

but strange :) dllcall returns 0, _WinAPI_GetLastErrorMessage() returns 'The handle is invalid.'... but the file is locked for me anyhow :huh2: ...

Edited by KaFu

Share this post


Link to post
Share on other sites
KaFu

Ah, okay, I see why _WinAPI_GetLastErrorMessage() returns the error, cause the dllcall isn't working at all :).

$hFile = FileOpen($File,1)

is totally sufficient to lock the file. Open a handle if you want it locked and close it if you want the file locked, that's all.

Share this post


Link to post
Share on other sites
Authenticity

If you want to lock the whole file you can just open it with iShare set to 0. If you do want the file to be accessible but to lock only parts of it you can use LockFileEx, but the function returns BOOL (int) rather than hwnd.

Share this post


Link to post
Share on other sites
Jango

If you want to lock the whole file you can just open it with iShare set to 0. If you do want the file to be accessible but to lock only parts of it you can use LockFileEx, but the function returns BOOL (int) rather than hwnd.

Thank you. To learn, this is really a nice forum with nice people.

Share this post


Link to post
Share on other sites
KaFu

If you want to lock the whole file you can just open it with iShare set to 0. If you do want the file to be accessible but to lock only parts of it you can use LockFileEx, but the function returns BOOL (int) rather than hwnd.

Nice to know :)... does Fileopen set iShare to 0? Seems so to me.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.