I tested the following 3 functions on Notepad (Windows 7)
and MS Paint (Windows XP version copied to Windows 7),
to check if saving is in progress, however, sometimes
the functions seemed to impede saving.
Notepad error message:
The process cannot access the file because it is being used by another process.
MS Paint error message:
A sharing violation occurred while accessing C:\MyImage.png.
Warning: be careful when using FileOpen,
because you could overwrite a file's data,
thus use a test file rather than an important file.
Code: Select all
q::
vPath = C:\MyImage.png.
Loop
{
if JEE_FileGetIsLocked(vPath)
MsgBox % "file is locked"
if (A_Index = 500000)
break
}
MsgBox % "done"
Return
;==================================================
JEE_FileGetIsLocked(vPath)
{
oFile := FileOpen(vPath, "r -w")
if IsObject(oFile)
Return 0, oFile.Close() ;unlocked
else
Return 1 ;locked
Return
}
;==================================================
JEE_FileGetIsLocked3(vPath)
{
if !(oFile := FileOpen(vPath, "r-rwd"))
Return 1 ;locked
else
Return 0, oFile.Close() ;unlocked
Return
}
;==================================================
JEE_FileGetIsLocked2(vPath)
{
static GENERIC_WRITE:=1073741824,GENERIC_READ:=2147483648,FILE_SHARE_WRITE:=2,OPEN_EXISTING:=3,FILE_ATTRIBUTE_NORMAL:=128,INVALID_HANDLE_VALUE:=-1
;GENERIC_WRITE := 1073741824
;GENERIC_READ := 2147483648
;FILE_SHARE_WRITE := 2
;OPEN_EXISTING := 3
;FILE_ATTRIBUTE_NORMAL := 128
;INVALID_HANDLE_VALUE := -1
;GENERIC_ACCESS := 268435456
;EXCLUSIVE_ACCESS := 0
;OPEN_EXISTING := 3
;both versions work
hFile := DllCall("CreateFile", Str,vPath, UInt,GENERIC_WRITE, UInt,FILE_SHARE_WRITE ,Ptr,0, UInt,OPEN_EXISTING, UInt,FILE_ATTRIBUTE_NORMAL, Ptr,0, Ptr)
;hFile := DllCall("CreateFile", Str,vPath, UInt,GENERIC_READ, UInt,EXCLUSIVE_ACCESS, Ptr,0, UInt,OPEN_EXISTING, Uint,0, Ptr,0, Ptr)
if (hFile < 0)
Return 1
else
Return 0, DllCall("CloseHandle", Ptr,hFile)
Return
}
;==================================================