go - 在 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 函数?
解决方案
Windows 上的服务可以使用当前用户权限运行另一段代码(作为单独的进程)。这个 git repo已经展示了它。我创建了我在上面发布的 go 程序的二进制文件,并用 git 代码中提到的 notepate.exe 替换了它的路径。
推荐阅读
- search - TYPO3 9.5:indexed_search 不可用
- java - 每个循环中的百里香计数匹配(对象数组)
- php - octobercms 中同名的唯一 slug
- java - 根据 xpath 元素中其他属性的条件获取属性值
- python - Azure 找不到 python 版本
- angular - AngularTS:首次实现时出现 OAuth 问题 - 错误类型错误:this.logger.error 不是函数
- python - 如何创建一个字典,其中每个键都有 2 个值?
- r - 从 R 中的不同文件的一行中插入时间
- c# - ASP.NET Core WebAPI 崩溃是因为我没有 StaticFileMiddleware?
- android - 导航抽屉的问题