首页 > 解决方案 > 在 Windows 7 中为当前用户在 Windows 服务中执行 InternetSetOptionW

问题描述

我有一个在 Windows 7 机器上运行的 Windows 服务。此服务为当前用户更改系统注册表中的少量代理设置。但是,与 Windows 10 不同的是,Windows 7 不会检测当前用户在注册表中所做的更改。所以我需要告诉系统刷新互联网设置并注意我的 Windows 服务的更改。我正在使用 wininet.dll 中的 internetsetoptionw 来执行此操作,并使用标志 37,39 调用它。我在 golang 中编写了一个简单的程序:

 package main

import (
    "fmt"
    "unsafe"
    "golang.org/x/sys/windows"
)

var wininet *windows.LazyDLL = windows.NewLazySystemDLL("Wininet")
func main(){
fmt.Println("main function")
refereshSystem()
}
// internetSetOptionW is imported from wininet.h
func internetSetOptionW(hndl uintptr, opt uintptr, val []byte, valLen int) error {
    var e error
    var success uintptr

    // Pointer to data if provided
    if valLen == 0 {
        val = make([]byte, 1)
    }

    success, _, e = wininet.NewProc("InternetSetOptionW").Call(hndl, opt, uintptr(unsafe.Pointer(&val[0])), uintptr(valLen))
    if success == 0 {
        fmt.Println("InternetSetOptionW:", e.Error())
        return fmt.Errorf("InternetSetOptionW: %s", e.Error())
    }

    return nil
}

func refereshSystem() {

        null := uintptr(0)
        var nillbyte []byte
        internetSetOptionW(null, uintptr(39), nillbyte, 0)
        internetSetOptionW(null, uintptr(37), nillbyte, 0)
        fmt.Println("")

}

当我在更改代理设置后从 CMD 运行此程序时,它会更新系统并且系统会知道我对代理相关注册表所做的更改。但是当我在我的 Windows 服务中运行它时,它不起作用。windows服务以系统root权限运行。有没有办法从我的 Windows 服务为当前用户执行 internetSetOptionw 函数?

标签: gowindows-7windows-servicesinternet-options

解决方案


Windows 上的服务可以使用当前用户权限运行另一段代码(作为单独的进程)。这个 git repo已经展示了它。我创建了我在上面发布的 go 程序的二进制文件,并用 git 代码中提到的 notepate.exe 替换了它的路径。


推荐阅读