使用使用 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
此輸出與檔案屬性檢視中的建立時間相符。
儘管filetime
本身的時間自1601 年1 月1 日utc 起,time.unix
和nanoseconds
函數 基於自1970 年1 月1 日utc 以來的時間。
以上是使用使用 GetFileVersionInfoSize 初始化的句柄呼叫 GetFileInformationByHandle 時出現「句柄無效」錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

自 2009 年問世以來,比特幣成為加密貨幣界的領頭羊,其價格經歷了巨大的波動。為了提供全面的歷史概述,本文匯集了從 2009 年到 2025 年的比特幣價格數據,涵蓋了重大的市場事件、市場情緒變化和影響價格走勢的重要因素。

比特币,作为一种加密货币,自问世以来经历了显著的市场波动。本文将提供比特币自诞生以来的历史价格总览,帮助读者了解其价格趋势和关键时刻。通过分析比特币的历史价格数据,我们可以了解市场对其价值评估、影响其波动的因素,并为未来投资决策提供依据。

比特幣自 2009 年創世以來,價格經歷多次大幅波動,最高漲至 2021 年 11 月的 69,044.77 美元,最低跌至 2018 年 12 月的 3,191.22 美元。截至 2024 年 12 月,最新價格突破 100,204 美元。

實時比特幣美元價格 影響比特幣價格的因素 預測比特幣未來價格的指標 以下是 2018-2024 年比特幣價格的一些關鍵信息:

比特幣歷史價格重要節點2009 年1 月 3 日:創世區塊生成,第一個比特幣產生,價值為 0 美元。 10 月 5 日:第一筆比特幣交易,一名程序員用 10,000 比特幣購買了兩個披薩,相當於 0.008 美元。 2010 年2 月 9 日:Mt. Gox 交易所上線,成為比特幣早期交易的主要平台。 5 月 22 日:比特幣首次突破 1 美元。 7 月 17 日:比特幣價格暴跌至 0.008 美元,觸及歷史低點。 2011 年2 月 9 日:比特幣價格首次突破 10 美元。 4 月 10 日:Mt. Go

ChatGPT時代的技術問答社區:思否(SegmentFault)的應對策略StackOverflow...

如何實現分段器的45度曲線效果?在實現分段器的過程中,如何讓點擊左側按鈕時右側邊框變成45度曲線,而點�...

CSS自定義resize符號的方法與背景色統一在日常開發中,我們經常會遇到需要自定義用戶界面細節的情況,比如調...
