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

浙公网安备 33010602011771号