Cleverly uses process separation to set Automatic startup of a program upon startup! (Source Code)

Source: Internet
Author: User

In the earliest version of Gg (high imitation qq), I added the function of Automatic startup upon startup, which is implemented by operating the registry, but the administrator privilege is required to operate the registry, the GG client must be run as an administrator. This may not be acceptable to some users, because running a program with such high permissions may cause a high risk. From the GG point of view, this permission is required only when you set or cancel the start of the boot, and running GG as an administrator at the beginning is a bit exaggerated and wasted.

I. Solution Evolution

The solution I first came up with was to start GG as a common user and escalate it to administrator permissions when you need to operate the registry. Later, I learned through Information Query:

(1) The Running permission of a thread cannot be upgraded separately. The running permission is bound to the process.

(2) to improve the permissions of a process, you must set the permissions when the process starts.

This means that to implement my solution, you must restart the GG process When GG is set or disabled. This is not elegant. Finally, I came up with a clever solution. The idea is as follows:

(1) set or remove the function (that is, set the permission limit of the Administrator) in gg1. create a single console program autostart.exe and set its running permission to administrator.

(2eput autostart.exeto the running directory of gg.exe.

(3)gg autoautoautoautoautoautoautoautoautoautoautoautoautostart.exe, and transmits the relevant information to the autostart.exe process.

Ii. solution implementation

1.autostart.exe

Autostart.exe is a console program that I implement as a reusable application. You can download and reuse it directly from the end of the article. The source code is quite simple, as shown below:

/// <Summary> /// the first parameter is the name of the EXE, and the second parameter is the bool value, indicates whether to start automatically. // </Summary> static void main (string [] ARGs) {try {bool autostart = bool. parse (ARGs [1]); console. writeline (string. format ("{0} auto start {1 }... ", autostart? "Settings": "cancel", argS [0]); // to operate the registry, start the program as an administrator. Runwhenstart_usingreg (autostart, argS [0], appdomain. currentdomain. basedirectory + ARGs [0]); console. writeline (""); console. writeline ("Operation completed! Press any key to exit... "); console. readline ();} catch (exception ee) {console. writeline ("exception occurred during Operation:" + ee. message + "press any key to exit... "); console. readline () ;}# Region automatically starts when it is started /// <summary> /// automatically starts when it is started, use the Registry /// </Summary> /// <Param name = \ "started \"> whether to enable automatic startup </param> /// <Param name = \ "Name \ "> obtain a unique registry key name </param> /// <Param name = \" path \ "> complete Startup Program path </param> Public static void runwhenstart_usingreg (bool started, st Ring name, string path) {registrykey HKLM = registry. localmachine; try {registrykey run = HKLM. createsubkey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \"); If (started) {run. setvalue (name, PATH);} else {object val = run. getvalue (name); If (Val! = NULL) {run. deletevalue (name) ;}} finally {HKLM. Close () ;}# endregion

If you do not know how to configure autostart.exe to start as an administrator, refer to this article.

2.call autostart.exe in gg.

When the user selects or removes the check box "start automatically", the program performs the following operations:

if (autoStartChanged)    {        string args = string.Format("{0} {1}", "GG.exe", autoStart? "true":"false");        Process.Start(AppDomain.CurrentDomain.BaseDirectory + "AutoStart.exe", args);    }

Running effect:

  

Iii. Download

1. autostart executable program

2. autostart source code

 

If you have any questions or suggestions, you can leave a message or send an email to my mailbox: [email protected].

If you think it's good, please try again.

 

Cleverly uses process separation to set Automatic startup of a program upon startup! (Source Code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.