Exemplo n.º 1
0
        /// <summary>
        /// Add key to start DisplaySwitch user UI on startup for all users
        /// </summary>
        public static void SetStartup()
        {
            RegistryKey regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            regkey.SetValue(appname, "\"" + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\DisplaySwitch for RealVNC\\DisplaySwitch.exe\" -startup", RegistryValueKind.String);
            regkey.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Run required actions when Service starts
        /// </summary>
        protected override void OnStart(string[] args)
        {
            //Add entries for DisplaySwitch to all users' startup
            RegistryManagement.SetStartup();

            //Make sure VNC Server is configured to log connections to EventLog - required for automatic UI display when VNC session starts/ends
            if (RegistryManagement.GetRegistryValue(RegHives.HKLM, "Log") == "")
            {
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "Log", "*:EventLog:10");
            }
            else if (RegistryManagement.GetRegistryValue(RegHives.HKLM, "Log") != "*:EventLog:10" && !RegistryManagement.GetRegistryValue(RegHives.HKLM, "Log").Contains("Connections:EventLog"))
            {
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "Log", RegistryManagement.GetRegistryValue(RegHives.HKLM, "Log") + ",Connections:EventLog:10");
            }

            //Configure our timer to poll for Registry changes
            tmrCheckDisplayDevice           = new System.Timers.Timer(100);
            tmrCheckDisplayDevice.Elapsed  += new ElapsedEventHandler(tmrCheckDisplayDevice_Tick);
            tmrCheckDisplayDevice.AutoReset = true;

            //When service starts, default to last recorded Primary display (this will usually be on system start)
            RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKLM_DS, "PrimaryMonitor"));
            if (!tmrCheckDisplayDevice.Enabled)
            {
                tmrCheckDisplayDevice.Start();
            }
            ;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Remove key to stop DisplaySwitch user UI appearing on startup for all users
        /// </summary>
        public static void ClearStartup()
        {
            RegistryKey regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (regkey.GetValue(appname) != null)
            {
                regkey.DeleteValue(appname);
            }
            regkey.Close();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Event called when tmrCheckDisplayDevice elapses
        /// </summary>
        private void tmrCheckDisplayDevice_Tick(object sender, ElapsedEventArgs e)
        {
            //If the master record for DisplayDevice does not match the user's selection, update it
            if (RegistryManagement.GetRegistryValue(RegHives.HKLM, "DisplayDevice") != RegistryManagement.GetRegistryValue(RegHives.HKCU, "DisplayDevice"))
            {
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKCU, "DisplayDevice"));
            }

            //If the master record for Primary Monitor does not match the user's selection, update it
            if (RegistryManagement.GetRegistryValue(RegHives.HKLM_DS, "PrimaryMonitor") != RegistryManagement.GetRegistryValue(RegHives.HKCU, "PrimaryMonitor"))
            {
                RegistryManagement.SetRegistryValue(RegHives.HKLM_DS, "PrimaryMonitor", RegistryManagement.GetRegistryValue(RegHives.HKCU, "PrimaryMonitor"));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets the value for a specified String value in the Registry Key for DisplaySwitch or VNC Server
        /// </summary>
        /// <param name="reg">Whether the key to be set belongs to Local Machine or Current User</param>
        /// <param name="valuename">The name of the String to set</param>
        /// <param name="value">The value to set</param>
        public static void SetRegistryValue(RegHives reg, string valuename, string value)
        {
            RegistryKey regkey;

            if (reg == RegHives.HKLM)
            {
                regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\RealVNC\\vncserver", true);
            }
            else if (reg == RegHives.HKLM_DS)
            {
                regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\RealVNC_DisplaySwitch", true);
            }
            else
            {
                regkey = Registry.Users.CreateSubKey(GetSIDForCurrentUser() + "\\SOFTWARE\\RealVNC_DisplaySwitch", true);
            }
            regkey.SetValue(valuename, value, RegistryValueKind.String);
            regkey.Close();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Retrieves the current value for a specified String value in the Registry Key for DisplaySwitch or VNC Server
        /// </summary>
        /// <param name="reg">Whether the key to be set belongs to Local Machine or Current User</param>
        /// <param name="valuename">The name of the String to retrieve</param>
        /// <returns>The value of the String</returns>
        public static string GetRegistryValue(RegHives reg, string valuename)
        {
            RegistryKey regkey;

            if (reg == RegHives.HKLM)
            {
                regkey = RegistryManagement.GetHKLM().OpenSubKey("SOFTWARE\\RealVNC\\vncserver", false);
            }
            else if (reg == RegHives.HKLM_DS)
            {
                regkey = RegistryManagement.GetHKLM().OpenSubKey("SOFTWARE\\RealVNC_DisplaySwitch", false);
            }
            else
            {
                regkey = Registry.Users.OpenSubKey(GetSIDForCurrentUser() + "\\SOFTWARE\\RealVNC_DisplaySwitch", false);
            }
            string value = "";

            try { value = regkey.GetValue(valuename).ToString(); regkey.Close(); }
            catch { }
            return(value);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Tracks session changes reported by Windows and makes changes to the display served by VNC Server
        /// </summary>
        /// <param name="sessionChangeDescription">The session change as reported by Windows</param>
        protected override void OnSessionChange(SessionChangeDescription sessionChangeDescription)
        {
            RegistryManagement.SetRegistryValue(RegHives.HKCU, "SessionChange", sessionChangeDescription.Reason.ToString());

            switch (sessionChangeDescription.Reason)
            {
            //When user logs on, set VNC Server to use user's selected screen
            case SessionChangeReason.SessionLogon:
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKCU, "DisplayDevice"));
                if (!tmrCheckDisplayDevice.Enabled)
                {
                    tmrCheckDisplayDevice.Start();
                }
                ;
                break;

            //When user logs off, set VNC Server to primary screen (to see login screen)
            case SessionChangeReason.SessionLogoff:
                tmrCheckDisplayDevice.Stop();
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKCU, "PrimaryMonitor"));
                break;

            //When user locks their session, set VNC Server to primary screen (to see lock screen)
            case SessionChangeReason.SessionLock:
                tmrCheckDisplayDevice.Stop();
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKCU, "PrimaryMonitor"));
                break;

            //When user unlocks their session, set VNC Server to use user's selected screen
            case SessionChangeReason.SessionUnlock:
                RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKCU, "DisplayDevice"));
                if (!tmrCheckDisplayDevice.Enabled)
                {
                    tmrCheckDisplayDevice.Start();
                }
                ;
                break;
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// If a shutdown event is detected, make sure we set Display to Primary so user can see logon screen over VNC when system comes back up
 /// </summary>
 protected override void OnShutdown()
 {
     tmrCheckDisplayDevice.Stop();
     RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKLM_DS, "PrimaryMonitor"));
     EventLogManagement.WriteEvent();
 }
Exemplo n.º 9
0
 /// <summary>
 /// If service is stopped, default to Primary and remove entries for DisplaySwitch from user startup folder
 /// </summary>
 protected override void OnStop()
 {
     tmrCheckDisplayDevice.Stop();
     RegistryManagement.SetRegistryValue(RegHives.HKLM, "DisplayDevice", RegistryManagement.GetRegistryValue(RegHives.HKLM_DS, "PrimaryMonitor"));
     RegistryManagement.ClearStartup();
 }