golang:使用struct参数调用winapi

时间:2016-10-10 14:57:59

标签: windows winapi networking go dll

我试图调用WinHttpGetIEProxyConfigForCurrentUser函数来获取自动检测到的IE代理设置。它根据documentation接受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
}

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

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

2 个答案:

答案 0 :(得分:6)

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

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

typedef struct {
  BOOL   fAutoDetect;
  LPWSTR lpszAutoConfigUrl;
  LPWSTR lpszProxy;
  LPWSTR lpszProxyBypass;
} WINHTTP_CURRENT_USER_IE_PROXY_CONFIG;

哪个应转换为

type WINHTTP_CURRENT_USER_IE_PROXY_CONFIG struct {
    fAutoDetect       bool
    lpszAutoConfigUrl *uint16
    lpszProxy         *uint16
    lpszProxyBypass   *uint16
}

这些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]))
}

答案 1 :(得分:1)

您在此处获取地址:

// The type of settings is *WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
settings := new(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG)

// ...

// The type of &settings is **WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
ret, _, callErr := syscall.Syscall(uintptr(getIEProxy), nargs, uintptr(unsafe.Pointer(&settings)), 0, 0)

&电话中移除Syscall它应该有效。