我试图调用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]
答案 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
它应该有效。