doucuo9126 2016-02-06 08:23
浏览 106
已采纳

golang,调用WinVolumeInformation winapi函数

Tries to call GetVolumeInformation function from golang. Want to get volume name.

Use spec's of api:

BOOL WINAPI GetVolumeInformation(
  _In_opt_  LPCTSTR lpRootPathName,
  _Out_opt_ LPTSTR  lpVolumeNameBuffer,
  _In_      DWORD   nVolumeNameSize,
  _Out_opt_ LPDWORD lpVolumeSerialNumber,
  _Out_opt_ LPDWORD lpMaximumComponentLength,
  _Out_opt_ LPDWORD lpFileSystemFlags,
  _Out_opt_ LPTSTR  lpFileSystemNameBuffer,
  _In_      DWORD   nFileSystemNameSize
);

Use code:

// test
package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

func main() {
    var lpRootPathName = "C:\\"
    var lpVolumeNameBuffer string
    var nVolumeNameSize uint64
    var lpVolumeSerialNumber uint64
    var lpMaximumComponentLength uint64
    var lpFileSystemFlags uint64
    var lpFileSystemNameBuffer string
    var nFileSystemNameSize uint32

    kernel32, _ := syscall.LoadLibrary("kernel32.dll")
    getVolume, _ := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")

    var nargs uintptr = 8
    ret, _, callErr := syscall.Syscall9(uintptr(getVolume),
        nargs,
        uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpRootPathName))),
        uintptr(unsafe.Pointer(&lpVolumeNameBuffer)),
        uintptr(unsafe.Pointer(&nVolumeNameSize)),
        uintptr(unsafe.Pointer(&lpVolumeSerialNumber)),
        uintptr(unsafe.Pointer(&lpMaximumComponentLength)),
        uintptr(unsafe.Pointer(&lpFileSystemFlags)),
        uintptr(unsafe.Pointer(&lpFileSystemNameBuffer)),
        uintptr(unsafe.Pointer(&nFileSystemNameSize)),
        0)
    fmt.Println(ret, callErr, lpVolumeNameBuffer)
}

... and finally have error :(

unexpected fault address 0xffffffffffffffff
fatal error: fault
[signal 0xc0000005 code=0x0 addr=0xffffffffffffffff pc=0x456b11]

Don't understand and google cant'd help with calling winapi functions and returng string as result.

Thank's.

  • 写回答

2条回答 默认 最新

  • douzhuo2002 2016-02-06 13:57
    关注

    Package unsafe

    Package unsafe contains operations that step around the type safety of Go programs.

    type Pointer

    type Pointer *ArbitraryType
    

    Pointer represents a pointer to an arbitrary type. There are four special operations available for type Pointer that are not available for other types.

    1) A pointer value of any type can be converted to a Pointer.

    2) A Pointer can be converted to a pointer value of any type.

    3) A uintptr can be converted to a Pointer.

    4) A Pointer can be converted to a uintptr.

    Pointer therefore allows a program to defeat the type system and read and write arbitrary memory. It should be used with extreme care.

    You failed to heed the warning that unsafe.Pointer "should be used with extreme care."

    Try this:

    package main
    
    import (
        "fmt"
        "syscall"
        "unsafe"
    )
    
    func main() {
        var RootPathName = `C:\`
        var VolumeNameBuffer = make([]uint16, syscall.MAX_PATH+1)
        var nVolumeNameSize = uint32(len(VolumeNameBuffer))
        var VolumeSerialNumber uint32
        var MaximumComponentLength uint32
        var FileSystemFlags uint32
        var FileSystemNameBuffer = make([]uint16, 255)
        var nFileSystemNameSize uint32 = syscall.MAX_PATH + 1
    
        kernel32, _ := syscall.LoadLibrary("kernel32.dll")
        getVolume, _ := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")
    
        var nargs uintptr = 8
        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)
        fmt.Println(ret, callErr, syscall.UTF16ToString(VolumeNameBuffer))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行