execute InternetSetOptionW in a Windows service for current user in windows 7

112 views Asked by At

I have a windows service running on a windows 7 machine. this service changes few proxy settings from the system registry for the current user. However, unlike windows 10, windows 7 does not detect the changes made in the registry for the current user. so I need to tell the system to refresh the internet settings and be aware of the changes that my windows service. I am using internetsetoptionw from wininet.dll to do so and calling it with flag 37,39. I have written a simple program in golang as:

 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("")

}

When I am running this program from the CMD after changing proxy settings, it updates the system and the system becomes aware of changes that I make to the proxy-related registry. but when I m running it inside my windows service, it does not work. windows service is running as system root privilege. Is there a way to execute the internetSetOptionw function for the current user from my windows service?

1

There are 1 answers

0
VikasPushkar On

a service on windows can run another piece of code(as a separate process) with the current user privilege. this git repo has shown it. I created a binary of my go program I had posted above and replaced its path with notepate.exe mentioned in the git code.