简体   繁体   English

以LocalSystem身份运行的.NET c#服务会更改注册表中的ProxyServer,但浏览器直到(重新)启动后才会注意到

[英].NET c# service running as LocalSystem alters ProxyServer in registry, but browser's won't notice until (re)start

I have a .NET c# service running as LocalSystem on Windows10 64bit. 我有一个在Windows10 64位上作为LocalSystem运行的.NET c#服务。 It's job is to alter the "ProxyEnable" and "ProxyServer" values located under 它的工作是更改位于下面的“ ProxyEnable”和“ ProxyServer”值

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

in the registry. 在注册表中。 It does so, for all "human" Users of the System (that is, the key starting with "S-1-5-21.."). 对于系统的所有“人类”用户,它都是这样做的(即,以“ S-1-5-21 ..”开头的密钥)。 It works fine and all browsers the user starts up use this configuration (proxy or no proxy). 它工作正常,并且用户启动的所有浏览器都使用此配置(代理或无代理)。

However, browsers that are still running won't notice changes (turning the proxy setting on or off) until a long time - OR until i manually just open and close the Windows "Internet Options > Proxy Settings" (without doing changes or saving them) or restart the browser(s). 但是,仍在运行的浏览器很长一段时间都不会注意到更改(打开或关闭代理设置)-或直到我手动打开并关闭Windows“ Internet选项>代理设置”(不进行更改或保存) )或重新启动浏览器。 The latter two make the System re-read and propagate the settings to all (other) open browsers. 后两者使系统重新读取设置并将其传播到所有(其他)打开的浏览器。

Now DllImport'ing wininet.dll and using the InternetSetOption does work 现在DllImport'ing wininet.dll并使用InternetSetOption确实可以工作

[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(
    IntPtr hInternet,
    int dwOption,
    IntPtr lpBuffer,
    int lpdwBufferLength);
private const int INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95;
private const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
private const int INTERNET_OPTION_REFRESH = 37;
...
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

but only when i perform this piece of code as the logged in user. 但仅当我以登录用户身份执行这段代码时。 When the service performs it, it doesn't do anything. 服务执行该操作后,它不会执行任何操作。 I guess it's because as LocalSystem the service is applying it to the wrong scope. 我猜这是因为作为LocalSystem,服务将其应用于错误的范围。

I have tinkered around a lot. 我已经纠结了很多。 Trying to start another process as the user is no option as this would require credentials. 尝试以用户身份启动另一个进程是没有选择的,因为这将需要凭据。 Also the service using LocalSystem is set. 还设置了使用LocalSystem的服务。

I am just convinced that as LocalSystem, that is close to Administrator in terms of privileges, there must be a way to tell all applications and browsers, that the Proxy Settings have changed. 我只是相信,作为LocalSystem(在特权方面与Administrator接近),必须有一种方法可以告诉所有应用程序和浏览器Proxy Settings(代理设置)已更改。 Its just the propagation like opening and closing the Proxy Settings-GUI invokes, that i need to trigger somehow. 它只是像打开和关闭Proxy Settings-GUI调用之类的传播,我需要以某种方式触发。 But i don't know how. 但是我不知道如何。 Do you? 你呢?

This is how environment variables & Reg keys function in windows - as I understand it all new processes get a snapshot of all variables when they start, but thats it. 这就是环境变量和Reg键在Windows中起作用的方式-据我了解,所有新进程在启动时都会获得所有变量的快照,仅此而已。 They will only update when you restart the process. 它们仅在您重新启动过程时更新。

See C# : How to change windows registry and take effect immediately 请参见C#:如何更改Windows注册表并立即生效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM