In the start-up service created in C #, calling an external executable file has the following issues:
1, the interactive EXE file with the window after the call, the actual is not executed;
2, the service is started with Windows, the service may start after the Windows desktop has not come out, will report errors, resulting in the program can not be executed;
3, Installation Services need administrator rights
and other issues.
To deal with some of the above issues:
1, call the interactive EXE file with the window, mainly the Interop.cs file,
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Security.Principal;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsolewithwindowsservice{classInterop { Public Static voidCreateProcess (stringAppstringpath) { BOOLresult; INTPTR Htoken=windowsidentity.getcurrent (). Token; INTPTR Hdupedtoken=IntPtr.Zero; Process_information Pi=Newprocess_information (); Security_attributes SA=Newsecurity_attributes (); Sa. Length=marshal.sizeof (SA); Startupinfo si=NewStartupinfo (); SI.CB=marshal.sizeof (SI); intDwsessionid =Wtsgetactiveconsolesessionid (); Result= Wtsqueryusertoken (Dwsessionid, outhtoken); if(!result) {ShowMessageBox ("Wtsqueryusertoken failed","Alertservice Message"); } result=Duplicatetokenex (Htoken, generic_all_access,refSA, (int) Security_impersonation_level. Securityidentification, (int) Token_type. Tokenprimary,refHdupedtoken); if(!result) {ShowMessageBox ("Duplicatetokenex failed","Alertservice Message"); } IntPtr lpenvironment=IntPtr.Zero; Result= Createenvironmentblock ( outLpenvironment, Hdupedtoken,false); if(!result) {ShowMessageBox ("Createenvironmentblock failed","Alertservice Message"); } result=CreateProcessAsUser (Hdupedtoken, app, String.Empty,refSarefSA,false,0, IntPtr.Zero,NULL,refSirefpi); if(!result) { intError =Marshal.GetLastWin32Error (); stringMessage = String.Format ("CreateProcessAsUser Error: {0}", error); ShowMessageBox (Message,"Alertservice Message"); } if(Pi.hprocess! =IntPtr.Zero) CloseHandle (pi.hprocess); if(Pi.hthread! =IntPtr.Zero) CloseHandle (Pi.hthread); if(Hdupedtoken! =IntPtr.Zero) CloseHandle (Hdupedtoken); } [StructLayout (layoutkind.sequential)] Public structStartupinfo { PublicInt32 CB; Public stringlpreserved; Public stringLpDesktop; Public stringLptitle; PublicInt32 DwX; PublicInt32 DwY; PublicInt32 dwxsize; PublicInt32 Dwxcountchars; PublicInt32 Dwycountchars; PublicInt32 Dwfillattribute; PublicInt32 dwFlags; PublicInt16 Wshowwindow; PublicInt16 CbReserved2; PublicIntPtr LpReserved2; PublicIntPtr hStdInput; PublicIntPtr Hstdoutput; PublicIntPtr Hstderror; } [StructLayout (layoutkind.sequential)] Public structprocess_information { PublicIntPtr hprocess; PublicIntPtr Hthread; PublicInt32 Dwprocessid; PublicInt32 dwThreadID; } [StructLayout (layoutkind.sequential)] Public structSecurity_attributes { PublicInt32 Length; PublicIntPtr Lpsecuritydescriptor; Public BOOLbInheritHandle; } Public enumsecurity_impersonation_level {securityanonymous, securityidentification, Sec Urityimpersonation, securitydelegation} Public enumToken_type {tokenprimary=1, tokenimpersonation} Public Const intGeneric_all_access =0x10000000; [DllImport ("Kernel32.dll", SetLastError =true, CharSet= CharSet.Auto, CallingConvention =Callingconvention.stdcall)] Public Static extern BOOLCloseHandle (IntPtr handle); [DllImport ("Advapi32.dll", SetLastError =true, CharSet= CharSet.Ansi, CallingConvention =Callingconvention.stdcall)] Public Static extern BOOLCreateProcessAsUser (IntPtr htoken,stringLpapplicationname,stringlpCommandLine,refsecurity_attributes Lpprocessattributes,refsecurity_attributes Lpthreadattributes,BOOLbInheritHandle, Int32 dwcreationflags, IntPtr lpenvrionment,stringLpcurrentdirectory,refStartupinfo Lpstartupinfo,refprocess_information lpprocessinformation); [DllImport ("Advapi32.dll", SetLastError =true)] Public Static extern BOOLDuplicatetokenex (IntPtr hexistingtoken, Int32 dwdesiredaccess,refsecurity_attributes lpthreadattributes, Int32 impersonationlevel, Int32 Dwtokentype, refIntPtr Phnewtoken); [DllImport ("Wtsapi32.dll", SetLastError =true)] Public Static extern BOOLWtsqueryusertoken (Int32 sessionId, outIntPtr Token); [DllImport ("Userenv.dll", SetLastError =true)] Static extern BOOLCreateenvironmentblock ( outIntPtr lpenvironment, IntPtr htoken,BOOLBinherit); Public StaticIntPtr Wts_current_server_handle =IntPtr.Zero; Public Static voidShowMessageBox (stringMessagestringtitle) { intRESP =0; Wtssendmessage (Wts_current_server_handle, Wtsgetactiveconsolesessionid (), TI Tle, title. Length, message, message. Length,0,0, outRespfalse); } [DllImport ("Kernel32.dll", SetLastError =true)] Public Static extern intWtsgetactiveconsolesessionid (); [DllImport ("Wtsapi32.dll", SetLastError =true)] Public Static extern BOOLwtssendmessage (IntPtr hserver,intSessionId, String ptitle,inttitlelength, String pMessage,intMessagelength,intStyle,intTimeout, out intPresponse,BOOLbwait); }}
In the service call asked file WindowsService.cs inside this reference
Interop.createprocess (@ "D:\getp.exe", @ "C:\Windows\System32\"); Perform
The exe here can be arbitrary.
2, in the service built a thread, delay execution EXE file, to avoid the 2nd problem, while the loop execution, a lot of software services constantly pop up the news ads on this like this.
3, administrator Permissions issues:
On the project, right click on "Properties", select "Security", tick the CheckBox "Enable ClickOnce"
Finally, return to security and remove the check box "Enable ClickOnce".
This allows for administrator privileges to be installed.
This example installs the process named "My Data Service" and executes the Getp.exe file every 200 seconds.
The runtime can choose "1" for the installation, "3" To uninstall, after installation, you can see the "My Data Services" project in the service.
More than write the registry to add boot up, even if a Trojan horse will not report the virus
Download: http://pan.baidu.com/s/1pLRnm8j
Beginner C #--C # Create a boot call external interactive EXE file