Guest User

Untitled

a guest
Jun 22nd, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. installutil.exe /Name=YourServiceName YourService.exe
  2.  
  3. installutil.exe /u YourService.exe
  4.  
  5. using System;
  6. using System.Collections;
  7. using System.Configuration.Install;
  8. using System.ComponentModel;
  9. using System.Runtime.InteropServices;
  10. using System.ServiceProcess;
  11.  
  12. namespace TestService
  13. {
  14. [RunInstaller(true)]
  15. public class ProjectInstaller : Installer
  16. {
  17. private const string DEFAULT_SERVICE_NAME = "TestService";
  18. private const string DISPLAY_BASE_NAME = "Test Service";
  19.  
  20. private ServiceProcessInstaller _ServiceProcessInstaller;
  21. private ServiceInstaller _ServiceInstaller;
  22.  
  23. public ProjectInstaller()
  24. {
  25. _ServiceProcessInstaller = new ServiceProcessInstaller();
  26. _ServiceInstaller = new ServiceInstaller();
  27.  
  28. _ServiceProcessInstaller.Account = ServiceAccount.LocalService;
  29. _ServiceProcessInstaller.Password = null;
  30. _ServiceProcessInstaller.Username = null;
  31.  
  32. this.Installers.AddRange(new System.Configuration.Install.Installer[] {
  33. _ServiceProcessInstaller,
  34. _ServiceInstaller});
  35. }
  36.  
  37. public override void Install(IDictionary stateSaver)
  38. {
  39. if (this.Context != null && this.Context.Parameters.ContainsKey("Name"))
  40. stateSaver["Name"] = this.Context.Parameters["Name"];
  41. else
  42. stateSaver["Name"] = DEFAULT_SERVICE_NAME;
  43.  
  44. ConfigureInstaller(stateSaver);
  45.  
  46. base.Install(stateSaver);
  47.  
  48. IntPtr hScm = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
  49. if (hScm == IntPtr.Zero)
  50. throw new Win32Exception();
  51. try
  52. {
  53. IntPtr hSvc = OpenService(hScm, this._ServiceInstaller.ServiceName, SERVICE_ALL_ACCESS);
  54. if (hSvc == IntPtr.Zero)
  55. throw new Win32Exception();
  56. try
  57. {
  58. QUERY_SERVICE_CONFIG oldConfig;
  59. uint bytesAllocated = 8192; // Per documentation, 8K is max size.
  60. IntPtr ptr = Marshal.AllocHGlobal((int)bytesAllocated);
  61. try
  62. {
  63. uint bytesNeeded;
  64. if (!QueryServiceConfig(hSvc, ptr, bytesAllocated, out bytesNeeded))
  65. {
  66. throw new Win32Exception();
  67. }
  68. oldConfig = (QUERY_SERVICE_CONFIG)Marshal.PtrToStructure(ptr, typeof(QUERY_SERVICE_CONFIG));
  69. }
  70. finally
  71. {
  72. Marshal.FreeHGlobal(ptr);
  73. }
  74.  
  75. string newBinaryPathAndParameters = oldConfig.lpBinaryPathName + " /s:" + (string)stateSaver["Name"];
  76.  
  77. if (!ChangeServiceConfig(hSvc, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE,
  78. newBinaryPathAndParameters, null, IntPtr.Zero, null, null, null, null))
  79. throw new Win32Exception();
  80. }
  81. finally
  82. {
  83. if (!CloseServiceHandle(hSvc))
  84. throw new Win32Exception();
  85. }
  86. }
  87. finally
  88. {
  89. if (!CloseServiceHandle(hScm))
  90. throw new Win32Exception();
  91. }
  92. }
  93.  
  94. public override void Rollback(IDictionary savedState)
  95. {
  96. ConfigureInstaller(savedState);
  97. base.Rollback(savedState);
  98. }
  99.  
  100. public override void Uninstall(IDictionary savedState)
  101. {
  102. ConfigureInstaller(savedState);
  103. base.Uninstall(savedState);
  104. }
  105.  
  106. private void ConfigureInstaller(IDictionary savedState)
  107. {
  108. _ServiceInstaller.ServiceName = (string)savedState["Name"];
  109. _ServiceInstaller.DisplayName = DISPLAY_BASE_NAME + " (" + _ServiceInstaller.ServiceName + ")";
  110. }
  111.  
  112. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  113. private static extern IntPtr OpenSCManager(
  114. string lpMachineName,
  115. string lpDatabaseName,
  116. uint dwDesiredAccess);
  117.  
  118. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  119. private static extern IntPtr OpenService(
  120. IntPtr hSCManager,
  121. string lpServiceName,
  122. uint dwDesiredAccess);
  123.  
  124. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  125. private struct QUERY_SERVICE_CONFIG
  126. {
  127. public uint dwServiceType;
  128. public uint dwStartType;
  129. public uint dwErrorControl;
  130. public string lpBinaryPathName;
  131. public string lpLoadOrderGroup;
  132. public uint dwTagId;
  133. public string lpDependencies;
  134. public string lpServiceStartName;
  135. public string lpDisplayName;
  136. }
  137.  
  138. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  139. [return: MarshalAs(UnmanagedType.Bool)]
  140. private static extern bool QueryServiceConfig(
  141. IntPtr hService,
  142. IntPtr lpServiceConfig,
  143. uint cbBufSize,
  144. out uint pcbBytesNeeded);
  145.  
  146. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  147. [return: MarshalAs(UnmanagedType.Bool)]
  148. private static extern bool ChangeServiceConfig(
  149. IntPtr hService,
  150. uint dwServiceType,
  151. uint dwStartType,
  152. uint dwErrorControl,
  153. string lpBinaryPathName,
  154. string lpLoadOrderGroup,
  155. IntPtr lpdwTagId,
  156. string lpDependencies,
  157. string lpServiceStartName,
  158. string lpPassword,
  159. string lpDisplayName);
  160.  
  161. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  162. [return: MarshalAs(UnmanagedType.Bool)]
  163. private static extern bool CloseServiceHandle(
  164. IntPtr hSCObject);
  165.  
  166. private const uint SERVICE_NO_CHANGE = 0xffffffffu;
  167. private const uint SC_MANAGER_ALL_ACCESS = 0xf003fu;
  168. private const uint SERVICE_ALL_ACCESS = 0xf01ffu;
  169. }
  170. }
  171.  
  172. using System;
  173. using System.ServiceProcess;
  174.  
  175. namespace TestService
  176. {
  177. class Program
  178. {
  179. static void Main(string[] args)
  180. {
  181. string serviceName = null;
  182. foreach (string s in args)
  183. {
  184. if (s.StartsWith("/s:", StringComparison.OrdinalIgnoreCase))
  185. {
  186. serviceName = s.Substring("/s:".Length);
  187. }
  188. }
  189.  
  190. if (serviceName == null)
  191. throw new InvalidOperationException("Service name not specified on command line.");
  192.  
  193. // Substitute the name of your class that inherits from ServiceBase.
  194.  
  195. TestServiceImplementation impl = new TestServiceImplementation();
  196. impl.ServiceName = serviceName;
  197. ServiceBase.Run(impl);
  198. }
  199. }
  200.  
  201. class TestServiceImplementation : ServiceBase
  202. {
  203. protected override void OnStart(string[] args)
  204. {
  205. // Your service implementation here.
  206. }
  207. }
  208. }
  209.  
  210. Private Function GetServiceName() As String
  211. Try
  212. Dim processId = Process.GetCurrentProcess().Id
  213. Dim query = "SELECT * FROM Win32_Service where ProcessId = " & processId.ToString
  214. Dim searcher As New Management.ManagementObjectSearcher(query)
  215. Dim share As Management.ManagementObject
  216. For Each share In searcher.Get()
  217. Return share("Name").ToString()
  218. Next share
  219. Catch ex As Exception
  220. Dim a = 0
  221. End Try
  222. Return "DefaultServiceName"
  223. End Function
Add Comment
Please, Sign In to add comment