微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Windows 7 中为当前用户在 Windows 服务中执行 InternetSetOptionW

如何解决在 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),0)
        fmt.Println("")

}

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

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。