golang:使用struct参数调用winapi

ros*_*euz 5 windows dll networking winapi go

我正在尝试调用WinHttpGetIEProxyConfigForCurrentUser函数来获取自动检测到的IE代理设置.它根据文档接受inout结构参数.我正在使用以下代码:

func GetProxySettings() {
    winhttp, _ := syscall.LoadLibrary("winhttp.dll")
    getIEProxy, _ := syscall.GetProcAddress(winhttp, "WinHttpGetIEProxyConfigForCurrentUser")

    settings := new(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG)
    var nargs uintptr = 1

    ret, _, callErr := syscall.Syscall(uintptr(getIEProxy), nargs, uintptr(unsafe.Pointer(&settings)), 0, 0)
    fmt.Println(ret, callErr)
    if settings != nil {
        fmt.Println(settings.fAutoDetect)
        fmt.Println(settings.lpszAutoConfigUrl)
        fmt.Println(settings.lpszProxy)
        fmt.Println(settings.lpszProxyBypass)
    }
}

type WINHTTP_CURRENT_USER_IE_PROXY_CONFIG struct {
    fAutoDetect       bool
    lpszAutoConfigUrl string
    lpszProxy         string
    lpszProxyBypass   string
}
Run Code Online (Sandbox Code Playgroud)

看起来呼叫是成功的,settings不是零,但一旦我访问它我就会感到恐慌.这是输出:

1 The operation completed successfully.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x1 pc=0x4d2bb4]
Run Code Online (Sandbox Code Playgroud)

Jim*_*imB 6

您需要将指针传递给已使用该new函数创建的已分配结构.&从系统调用中删除额外的内容;uintptr(unsafe.Pointer(settings))

您还需要一个与syscall所期望的C结构具有相同布局的结构.结构定义如下:

typedef struct {
  BOOL   fAutoDetect;
  LPWSTR lpszAutoConfigUrl;
  LPWSTR lpszProxy;
  LPWSTR lpszProxyBypass;
} WINHTTP_CURRENT_USER_IE_PROXY_CONFIG;
Run Code Online (Sandbox Code Playgroud)

哪个应该翻译成

type WINHTTP_CURRENT_USER_IE_PROXY_CONFIG struct {
    fAutoDetect       bool
    lpszAutoConfigUrl *uint16
    lpszProxy         *uint16
    lpszProxyBypass   *uint16
}
Run Code Online (Sandbox Code Playgroud)

每个LPWSTR字段都将是一个以null结尾的16位/字符串字符串.为了将它们转换为Go字符串,首先需要将其转换*uint16[]uint16切片,然后将该切片解码为utf8字符串.

// Convert a *uint16 C string to a Go String
func GoWString(s *uint16) string {
    if s == nil {
        return ""
    }

    p := (*[1<<30 - 1]uint16)(unsafe.Pointer(s))

    // find the string length
    sz := 0
    for p[sz] != 0 {
        sz++
    }

    return string(utf16.Decode(p[:sz:sz]))
}
Run Code Online (Sandbox Code Playgroud)