Jango Posted February 10, 2009 Share 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 ? Link to comment Share on other sites More sharing options...
herewasplato Posted February 10, 2009 Share 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] Link to comment Share on other sites More sharing options...
KaFu Posted February 10, 2009 Share 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 (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
KaFu Posted February 10, 2009 Share 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 (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Authenticity Posted February 10, 2009 Share 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. Link to comment Share on other sites More sharing options...
Jango Posted February 11, 2009 Author Share 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. Link to comment Share on other sites More sharing options...
KaFu Posted February 11, 2009 Share 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 (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
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