c# - Enable windows service programmaticaly -


I am trying to enable a window service program by modifying the value in the registry below and changing the value. However, I am not able to start the service after this because the windows are still unable to see it.

  Public Zero Enabled TheService (string serviceName) {try {RegistryKey key = Registry.LocalMachine}. OpenSubKey (@ "System \ CurrentControlSet \ Services \" + serviceName, is true); Key.SetValue ("Start", 2); } Hold (Exception Pre) {Console.Write (ex.Message); }} Public Zero StartService (String Servant) {ServiceController Service = New Service Controller (serviceName); Try {service.start (); Service.WaitForStatus (ServiceControllerStatus.Running, New TimeSpain (0, 0, 0, 20)); } Hold (Exception Pre) {Console.Write (ex.Message);  

There are several ways that you can access windows Can change the startup type of service (see). If I remember correctly that when I tested it but was not completely reliable, I used the Windows API function. I have never tried the registry method I think it will be the most scalable of the three options. I have worked in C # code to call ChangeServiceConfig, but it's not right in front of me. I try to add it tomorrow, it's a good starting point.

Update: How do you do it with Windows API:

  Private Zero ChangeServiceStartType (String SevenName, Startup Type Option startType) {// Get Service Control Manager Database Handheld for IntPtr scmHandle = OpenSCManager (blank, blank, SC_MANAGER_CONNECT); If (scmHandle == IntPtr.Zero) {new exception ("service control manager fails to get handle in database."); } // Get a handle for the specified window service IntPtr serviceHandal = Open service (scmHandle, serviceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG); If (servicehand == IntPtr.Zero) {new exception throw (string.format ("service failed to handle handle" "{0} \".), ServiceName); } Bool changeServiceSuccess = ChangeServiceConfig (serviceHandle, SERVICE_NO_CHANGE, (UIT) startup, SERVICE_NO_CHANGE, blank, empty, IntPtr.Zero, blank, empty, empty, blank); Failed to update service configuration for ({ChangeServiceSuccess} {string msg = string.Format ("service \" {0}). ChangeServiceConfig returned error {1}. ", ServiceName, Marshall.GetLostwin Error () ToString ()); Throw a new exception (msg); } // Clear if (scmHandle! = IntPtr.Zero) CloseServiceHandle (scmHandle); If the (Circle! = IntPtr.Zero) is closed, the sevandal (service circle); } [DllImport ("advapi32.dll", Charset = CharSet.Auto, SetLastError = true)) Private static extern IntPtr OpenSCManager (string machinename, string database name, UIT dwAccess); [DllImport ("advapi32.dll", Charset = Charset Auto, Setlist Error = True)] Private Static Extern IntPtr OpenService (IntPtr hSCManager, string lpServiceName, UIT dwDesiredAccess); [DllImport ("advapi32.dll", Charset = CharSet.Auto, SetLastError = true)] Personal Static Exclusion Boolean Change Service Conf (Interpet HS Service, UIIT 32 NSATTIPE, UIIT 32NestatType, UITIT 32NERRCentrol, String LPBerryPathName, String LPLOorder Group , ITPTR LPDVTag ID, [these] four [] lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName); [DllImport ("advapi32.dll", EntryPoint = "Bandwidth Handle")] Personal Static Extern int CloseServiceHandle (IntPtr hSCObject); Private Contact UIT SC_MANAGER_CONNECT = 0x0001; Private Contact Unit SERVICE_QUERY_CONFIG = 0x00000001; Private Contact UIT SERVICE_CHANGE_CONFIG = 0x00000002; Private Cantonment Unit SERVICE_NO_CHANGE = 0xFFFFFFFF; Private Anonymous startupType option: uint {BootStart = 0, // A device driver started by the system loader. This value is only valid for driver services SystemStart = 1, // A device driver was started by the IoInitSystem function. This value is valid only for driver services Automatic = 2, // A service started automatically by the service control manager during system startup. Manual = 3, // Service started by a service service manager When a process calls the StartService function disabled = 4 / a service which can not be started. Error code ERROR_SERVICE_DISABLED attempts to start the service result}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -