首页 > 后端开发 > Golang > 正文

使用使用 GetFileVersionInfoSize 初始化的句柄调用 GetFileInformationByHandle 时出现'句柄无效”错误

WBOY
发布: 2024-02-09 10:24:20
转载
1080 人浏览过

使用使用 GetFileVersionInfoSize 初始化的句柄调用 GetFileInformationByHandle 时出现“句柄无效”错误

php小编鱼仔在使用GetFileVersionInfoSize初始化的句柄调用GetFileInformationByHandle时遇到了"句柄无效"错误。这个错误通常是由于句柄无法正确地识别文件信息导致的。解决这个问题的方法有多种,例如检查句柄是否正确初始化,确认文件路径是否正确,以及检查文件是否已被其他进程占用。通过仔细排查和处理,可以解决这个问题并顺利获取文件信息。

问题内容

我正在尝试在 windows 上使用 go 以编程方式获取文件信息,包括文件的创建时间。

我在 golang.org/x/sys/windows 中发现一个函数,它返回有关何时创建文件的信息,该函数是 getfileinformationbyhandle (go 文档、windows api 文档)。但是,我使用此函数编写的代码给了我一个 the handle is invalid 错误。

这是我的代码:

package main

import (
    "log"
    "os"

    "golang.org/x/sys/windows"
)

func main() {
    name := `c:\windows\system32\cmd.exe`

    fileinfo, err := os.stat(name)
    if err != nil {
        log.fatalf("unable to stat %s: %s", name, err.error())
    }

    log.printf("%s has base name %s.\n", name, fileinfo.name())

    var handle windows.handle
    _, err = windows.getfileversioninfosize(name, &handle)
    if err != nil && err != windows.error_file_not_found && err != windows.error_resource_type_not_found {
        log.fatalf("getfileversioninfosize error: path: %s %s", name, err.error())
    }

    var hndlfileinfo windows.byhandlefileinformation
    err = windows.getfileinformationbyhandle(handle, &hndlfileinfo)
    if err != nil {
        if err == windows.error_invalid_handle { // https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
            log.println("error is invalid handle error.")
        }
        log.fatalf("getfileinformationbyhandle error: path: %s %s", name, err.error())
    }

    log.println("success!")
}
登录后复制

当我运行它时,我得到以下输出:

2023/01/11 14:43:19 c:\windows\system32\cmd.exe has base name cmd.exe.
2023/01/11 14:43:19 error is invalid handle error.
2023/01/11 14:43:19 getfileinformationbyhandle error: path: c:\windows\system32\cmd.exe the handle is invalid.
登录后复制

我已确认文件路径有效(加上 os.stat 调用不会返回错误):

我知道 system32 目录对于 32 位 windows 程序不可见,但我已经使用 git-bash 上的 file 工具验证了我的可执行文件是 64 位程序:

$ file win_handle_test.exe
win_handle_test.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows
登录后复制

既然路径应该是有效的,我可能做错了什么,导致我得到无效的句柄?

解决方法

根据这个答案,我找到了另一种获取创建时间的方法:

package main

import (
    "log"
    "os"
    "time"
    "syscall"
)

func main() {
    name := `c:\windows\system32\cmd.exe`

    fileinfo, err := os.stat(name)
    if err != nil {
        log.fatalf("unable to stat %s: %s", name, err.error())
    }

    log.printf("%s has base name %s.\n", name, fileinfo.name())

    data := fileinfo.sys().(*syscall.win32fileattributedata)
    ctime := time.unix(0, data.creationtime.nanoseconds())

    log.printf("ctime in utc           : %v\n", ctime.utc())
    log.printf("ctime in local timezone: %v\n", ctime)
}
登录后复制

输出:

2023/01/11 15:03:58 C:\Windows\System32\cmd.exe has base name cmd.exe.
2023/01/11 15:03:58 cTime in UTC           : 2022-05-10 17:34:57.9429156 +0000 UTC
2023/01/11 15:03:58 cTime in local timezone: 2022-05-10 13:34:57.9429156 -0400 EDT
登录后复制

此输出与文件属性视图中的创建时间匹配。

尽管 filetimefiletime本身的时间自 1601 年 1 月 1 日 utc 起,time.unixnanoseconds本身的时间自 1601 年 1 月 1 日 utc 起,time.unixnanoseconds 函数

基于自 1970 年 1 月 1 日 utc 以来的时间。 🎜

以上是使用使用 GetFileVersionInfoSize 初始化的句柄调用 GetFileInformationByHandle 时出现'句柄无效”错误的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!