Jango Posted February 10, 2009 Posted February 10, 2009 Hello,I'm looking for a way to lock a file for exclusive access and i found this:LockFile FunctionBut how to do it in AutoIt ? Is a FileOpen sufficient ?
herewasplato Posted February 10, 2009 Posted February 10, 2009 it has been requested and was being worked on http://www.autoitscript.com/trac/autoit/ticket/546 [size="1"][font="Arial"].[u].[/u][/font][/size]
KaFu Posted February 10, 2009 Posted February 10, 2009 (edited) 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 ... Edited February 10, 2009 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
KaFu Posted February 10, 2009 Posted February 10, 2009 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. OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
Authenticity Posted February 10, 2009 Posted February 10, 2009 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.
Jango Posted February 11, 2009 Author Posted February 11, 2009 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.
KaFu Posted February 11, 2009 Posted February 11, 2009 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. OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now