1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.InteropServices; 6 7 namespace winInstallService 8 { 9 10 class ServiceInstaller 11 { 12 #region Private Variables 13 private string _servicePath; 14 private string _serviceName; 15 private string _serviceDisplayName; 16 #endregion Private Variables 17 #region DLLImport 18 [DllImport("advapi32.dll")] 19 public static extern IntPtr OpenSCManager(string lpMachineName, string lpSCDB, int scParameter); 20 [DllImport("Advapi32.dll")] 21 public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName, 22 int dwDesiredAccess, int dwServiceType, int dwStartType, int dwErrorControl, string lpPathName, 23 string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword); 24 [DllImport("advapi32.dll")] 25 public static extern void CloseServiceHandle(IntPtr SCHANDLE); 26 [DllImport("advapi32.dll")] 27 public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string lpServiceArgVectors); 28 [DllImport("advapi32.dll", SetLastError = true)] 29 public static extern IntPtr OpenService(IntPtr SCHANDLE, string lpSvcName, int dwNumServiceArgs); 30 [DllImport("advapi32.dll")] 31 public static extern int DeleteService(IntPtr SVHANDLE); 32 [DllImport("kernel32.dll")] 33 public static extern int GetLastError(); 34 #endregion DLLImport 35 /// <summary> 36 /// 应用程序入口. 37 /// </summary> 38 39 static void myMain(string[] args) 40 { 41 42 string svcPath; 43 string svcName; 44 string svcDispName; 45 //服务程序的路径 46 svcPath = @"C:\MyService.exe"; 47 svcDispName = "MyService"; 48 svcName = "MyService"; 49 ServiceInstaller c = new ServiceInstaller(); 50 c.InstallService(svcPath, svcName, svcDispName); 51 Console.Read(); 52 53 } 54 55 /// <summary> 56 /// 安装和运行 57 /// </summary> 58 /// <param name="svcPath">程序路径.</param> 59 /// <param name="svcName">服务名</param> 60 /// <param name="svcDispName">服务显示名称.</param> 61 /// <returns>服务安装是否成功.</returns> 62 public bool InstallService(string svcPath, string svcName, string svcDispName) 63 { 64 #region Constants declaration. 65 int SC_MANAGER_CREATE_SERVICE = 0x0002; 66 int SERVICE_WIN32_OWN_PROCESS = 0x00000010; 67 //int SERVICE_DEMAND_START = 0x00000003; 68 int SERVICE_ERROR_NORMAL = 0x00000001; 69 int STANDARD_RIGHTS_REQUIRED = 0xF0000; 70 int SERVICE_QUERY_CONFIG = 0x0001; 71 int SERVICE_CHANGE_CONFIG = 0x0002; 72 int SERVICE_QUERY_STATUS = 0x0004; 73 int SERVICE_ENUMERATE_DEPENDENTS = 0x0008; 74 int SERVICE_START = 0x0010; 75 int SERVICE_STOP = 0x0020; 76 int SERVICE_PAUSE_CONTINUE = 0x0040; 77 int SERVICE_INTERROGATE = 0x0080; 78 int SERVICE_USER_DEFINED_CONTROL = 0x0100; 79 int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | 80 SERVICE_QUERY_CONFIG | 81 SERVICE_CHANGE_CONFIG | 82 SERVICE_QUERY_STATUS | 83 SERVICE_ENUMERATE_DEPENDENTS | 84 SERVICE_START | 85 SERVICE_STOP | 86 SERVICE_PAUSE_CONTINUE | 87 SERVICE_INTERROGATE | 88 SERVICE_USER_DEFINED_CONTROL); 89 int SERVICE_AUTO_START = 0x00000002; 90 #endregion Constants declaration. 91 try 92 { 93 IntPtr sc_handle = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE); 94 if (sc_handle.ToInt32() != 0) 95 { 96 IntPtr sv_handle = CreateService(sc_handle, svcName, svcDispName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, svcPath, null, 0, null, null, null); 97 if (sv_handle.ToInt32() == 0) 98 { 99 CloseServiceHandle(sc_handle); 100 return false; 101 } 102 else 103 { 104 //试尝启动服务 105 int i = StartService(sv_handle, 0, null); 106 if (i == 0) 107 { 108 109 return false; 110 } 111 112 CloseServiceHandle(sc_handle); 113 return true; 114 } 115 } 116 else 117 118 return false; 119 } 120 catch (Exception e) 121 { 122 throw e; 123 } 124 } 125 /// <summary> 126 /// 卸载服务. 127 /// </summary> 128 /// <param name="svcName">服务名.</param> 129 public bool UnInstallService(string svcName) 130 { 131 int GENERIC_WRITE = 0x40000000; 132 IntPtr sc_hndl = OpenSCManager(null, null, GENERIC_WRITE); 133 if (sc_hndl.ToInt32() != 0) 134 { 135 int DELETE = 0x10000; 136 IntPtr svc_hndl = OpenService(sc_hndl, svcName, DELETE); 137 if (svc_hndl.ToInt32() != 0) 138 { 139 int i = DeleteService(svc_hndl); 140 if (i != 0) 141 { 142 CloseServiceHandle(sc_hndl); 143 return true; 144 } 145 else 146 { 147 CloseServiceHandle(sc_hndl); 148 return false; 149 } 150 } 151 else 152 return false; 153 } 154 else 155 return false; 156 } 157 } 158 }