独占锁定文件检测 AutoIt

发布于 2024-10-08 23:20:20 字数 1708 浏览 0 评论 0原文

如何检查文件是否在 AutoIt 中被独占锁定?我不是在谈论读/写访问。另外,我做了一些研究,如果文件被锁定,它不会显示在任务管理器进程列表中。

一个例子是在 Perl 中称为集群: 您可以通过 $theRC = farm($HANDLE, LOCK_EX|LOCK_NB); 检查文件是否被锁定。

我正在尝试在 AutoIt 中复制这一点。

我找到了一种可行的解决方案:

Local $f = "C:/log.txt"

MsgBox(0, _FileInUse($f), @error)

;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;
;===============================================================================
Func _FileInUse($sFilename)
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
                                    "dword", 0, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0, _ ;lpSecurityAttributes = NULL
                                    "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        SetError($aRet[0])
        Return 1
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0
    EndIf
EndFunc

How do I check to see if a file is exclusively locked in AutoIt? I am not talking about read/write access. Also, I have done some research, and if a file is locked, it does not show up in the task manager process list.

One example would be in Perl called flock:
You check to see if a file is locked via $theRC = flock($HANDLE, LOCK_EX|LOCK_NB);

I am trying to replicate this in AutoIt.

I have found one working solution:

Local $f = "C:/log.txt"

MsgBox(0, _FileInUse($f), @error)

;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;
;===============================================================================
Func _FileInUse($sFilename)
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
                                    "dword", 0, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0, _ ;lpSecurityAttributes = NULL
                                    "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        SetError($aRet[0])
        Return 1
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0
    EndIf
EndFunc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-10-15 23:20:20

这应该可以解决问题:

Func FileInUse($filename)
    $handle = FileOpen($filename, 1)

    $result = False
    if $handle = -1 then $result = True

    FileClose($handle)

    return $result
EndFunc

;~ usage
$filename = "C:\Windu15f.exe"
if FileInUse($filename) Then
    MsgBox(0, "", "File is in use")
Else
    MsgBox(0, "", "Not in use - go nuts")
EndIf

This should do the trick:

Func FileInUse($filename)
    $handle = FileOpen($filename, 1)

    $result = False
    if $handle = -1 then $result = True

    FileClose($handle)

    return $result
EndFunc

;~ usage
$filename = "C:\Windu15f.exe"
if FileInUse($filename) Then
    MsgBox(0, "", "File is in use")
Else
    MsgBox(0, "", "Not in use - go nuts")
EndIf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文