Windows service starts a process with administrator privileges

Source: Internet
Author: User

The thing is, the company's products have a daemon (Windows Service) need to start the product of the main program EXE, let the main program it runs as Administrator permissions (because the main program will load a plug-in, the plug-in has a list of port monitoring features, need to be found by the port to the process PID, The process name or process mirror path is looked up by the progress PID, which requires administrator privileges to find the process name and path for some special processes such as Svchost. Programs under Windows do not get administrator privileges at run time , and can only be elevated to administrator rights when the process is created. If the normal process is running an Administrator permission program, you can call the Shellexcute API. Double-click the mouse to run EXE, you can add Invoker in the manifest file Admin,uac will prompt the user to run with administrator privileges. But, the special is here!! The daemon is Windows Service,service cannot call Shellexcute to create a process, and if so, it will fail. Need to call the CreateProcessAsUser API to create a process, the common use of this API, can not create a program with administrator rights, need a few special usage, as follows:

1 /**2 * Create Process3 * @param process_name process name4 * @param process information5 * @param is_run_with_create is started when created6 * @return 0 Success7  */8 intCreate_process (Char* Process_name, lpprocess_information process,intis_run_with_create)9 {Ten    One      A  -HANDLE Htoken =NULL;  -HANDLE Htokendup =NULL; the     intErrret =-1; -      -     if(String_is_empty (process_name)) { -         return-1; +     } -  +          A          Do    at         {   -             if(OpenProcessToken (getcurrentprocess (),token_all_access,&htoken))  -             {   -                 if(Duplicatetokenex (Htoken, Token_all_access,null, Securityidentification, Tokenprimary, &htokendup))  -                 {   - startupinfo si;  in                     -  toLPVOID penv =NULL; +DWORD Dwsessionid =Wtsgetactiveconsolesessionid ();  -                      theZeroMemory (&si,sizeof(Startupinfo));  *                     $                 Panax Notoginseng                      -                     if(! Settokeninformation (Htokendup,tokensessionid,&dwsessionid,sizeof(DWORD)))  the                     {   +                         A                          Break;  the                     }   +        -                     $SI.CB =sizeof(STARTUPINFO);  $Si.lpdesktop ="Winsta0\\default";  -Si.wshowwindow =Sw_show;  -Si.dwflags = Startf_useshowwindow/*| Startf_usestdhandles*/;  the        -                    Wuyi                     if(! Createenvironmentblock (&penv,htokendup,false))  the                     {   -                         Wu                          Break;  -                     }   About        $                     if(!CreateProcessAsUser (Htokendup,process_name,null,null,null,false, -Normal_priority_class | Create_new_console |Create_unicode_environment, -penv,null,&si,process))  -                     {   A                  +                          Break;  the                     }   -        $                     if(penv) the                     {   the Destroyenvironmentblock (penv);  the                     }   the                 }   -                 Else   in                 {   the                      Break;  the                 }   About        the                        the             }   the             Else   +             {   -                 theErrret =0; Bayi                  Break;  the             }   the} while(0);  -        -         if(Htokendup! = NULL && Htokendup! =Invalid_handle_value) the CloseHandle (htokendup);  the         if(Htoken! = NULL && Htoken! =Invalid_handle_value) the CloseHandle (Htoken);  the  -  the     returnErrret; the}

With the above code, you can use the Windows service process to create a main program with administrator privileges.

References

Http://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time

http://blog.csdn.net/woshinia/article/details/7850295

Http://stackoverflow.com/questions/6261427/how-to-run-a-process-as-an-administrator-from-win32-c

Windows service starts a process with administrator privileges

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.