在 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中文网其他相关文章!