首頁 > 後端開發 > Golang > 主體

為什麼我的 Go 程式碼在使用 GetVolumeInformation 和 unsafe.Pointer 時失敗?

Linda Hamilton
發布: 2024-10-27 18:30:02
原創
907 人瀏覽過

Why Does My Go Code Fail When Using GetVolumeInformation with unsafe.Pointer?

在Go 中調用GetVolumeInformation WinAPI 函數

問題:

嘗試檢索卷使用GetVolumeInformation WinAPI 函數時,程式碼失敗並發生意外錯誤。

答案:

錯誤源自於不正確地使用 unsafe.Pointer。具體來說,轉換為 unsafe.Pointer 時,應將變數名稱中的 & 運算子去掉。

解決方案:

修改程式碼如下,解決指針問題:

<code class="go">var nVolumeNameSize = uint32(len(VolumeNameBuffer))
ret, _, callErr := syscall.Syscall9(uintptr(getVolume),
    nargs,
    uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(RootPathName))),
    uintptr(unsafe.Pointer(&VolumeNameBuffer[0])),
    uintptr(nVolumeNameSize),
    uintptr(unsafe.Pointer(&VolumeSerialNumber)),
    uintptr(unsafe.Pointer(&MaximumComponentLength)),
    uintptr(unsafe.Pointer(&FileSystemFlags)),
    uintptr(unsafe.Pointer(&FileSystemNameBuffer[0])),
    uintptr(nFileSystemNameSize),
    0)</code>
登入後複製

此修改後的程式碼正確地傳遞了適當的指針,消除了故障錯誤並允許檢索磁碟區名稱。

以上是為什麼我的 Go 程式碼在使用 GetVolumeInformation 和 unsafe.Pointer 時失敗?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!