不使用installutil.exe安裝WINDOWS服務

  1. using  System;
  2. using  System.Runtime.InteropServices;
  3. namespace  MyServiceInstaller
  4. {
  5. class  ServiceInstaller
  6. {
  7. #region Private Variables
  8. private   string  _servicePath;
  9. private   string  _serviceName;
  10. private   string  _serviceDisplayName;
  11. #endregion Private Variables
  12. #region DLLImport
  13. [DllImport( "advapi32.dll" )]
  14. public   static   extern  IntPtr OpenSCManager( string  lpMachineName, string  lpSCDB,  int  scParameter);
  15. [DllImport( "Advapi32.dll" )]
  16. public   static   extern  IntPtr CreateService(IntPtr SC_HANDLE, string  lpSvcName, string  lpDisplayName, 
  17. int  dwDesiredAccess, int  dwServiceType, int  dwStartType, int  dwErrorControl, string  lpPathName, 
  18. string  lpLoadOrderGroup, int  lpdwTagId, string  lpDependencies, string  lpServiceStartName, string  lpPassword);
  19. [DllImport( "advapi32.dll" )]
  20. public   static   extern   void  CloseServiceHandle(IntPtr SCHANDLE);
  21. [DllImport( "advapi32.dll" )]
  22. public   static   extern   int  StartService(IntPtr SVHANDLE, int  dwNumServiceArgs, string  lpServiceArgVectors);
  23. [DllImport( "advapi32.dll" ,SetLastError= true )]
  24. public   static   extern  IntPtr OpenService(IntPtr SCHANDLE, string  lpSvcName, int  dwNumServiceArgs);
  25. [DllImport( "advapi32.dll" )]
  26. public   static   extern   int  DeleteService(IntPtr SVHANDLE);
  27. [DllImport( "kernel32.dll" )]
  28. public   static   extern   int  GetLastError();
  29. #endregion DLLImport
  30. /// <summary>
  31. /// 應用程序入口.
  32. /// </summary>
  33. [STAThread]
  34. static   void  Main( string [] args)
  35. {
  36. string  svcPath;
  37. string  svcName;
  38. string  svcDispName;
  39. //服務程序的路徑
  40. svcPath = @ "C:/MyService.exe" ;
  41. svcDispName= "MyService" ;
  42. svcName=  "MyService" ;
  43. ServiceInstaller c =  new  ServiceInstaller();
  44. c.InstallService(svcPath, svcName, svcDispName);
  45. Console.Read();
  46. }
  47. /// <summary>
  48. /// 安裝和運行
  49. /// </summary>
  50. /// <param name="svcPath">程序路徑.</param>
  51. /// <param name="svcName">服務名</param>
  52. /// <param name="svcDispName">服務顯示名稱.</param>
  53. /// <returns>服務安裝是否成功.</returns>
  54. public   bool  InstallService( string  svcPath,  string  svcName,  string  svcDispName)
  55. {
  56. #region Constants declaration.
  57. int  SC_MANAGER_CREATE_SERVICE = 0x0002;
  58. int  SERVICE_WIN32_OWN_PROCESS = 0x00000010;
  59. //int SERVICE_DEMAND_START = 0x00000003;
  60. int  SERVICE_ERROR_NORMAL = 0x00000001;
  61. int  STANDARD_RIGHTS_REQUIRED = 0xF0000;
  62. int  SERVICE_QUERY_CONFIG = 0x0001;
  63. int  SERVICE_CHANGE_CONFIG = 0x0002;
  64. int  SERVICE_QUERY_STATUS = 0x0004;
  65. int  SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
  66. int  SERVICE_START =0x0010;
  67. int  SERVICE_STOP =0x0020;
  68. int  SERVICE_PAUSE_CONTINUE =0x0040;
  69. int  SERVICE_INTERROGATE =0x0080;
  70. int  SERVICE_USER_DEFINED_CONTROL =0x0100;
  71. int  SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | 
  72. SERVICE_QUERY_CONFIG |
  73. SERVICE_CHANGE_CONFIG |
  74. SERVICE_QUERY_STATUS | 
  75. SERVICE_ENUMERATE_DEPENDENTS | 
  76. SERVICE_START | 
  77. SERVICE_STOP | 
  78. SERVICE_PAUSE_CONTINUE | 
  79. SERVICE_INTERROGATE | 
  80. SERVICE_USER_DEFINED_CONTROL);
  81. int  SERVICE_AUTO_START = 0x00000002;
  82. #endregion Constants declaration.
  83. try
  84. {
  85. IntPtr sc_handle = OpenSCManager( null , null ,SC_MANAGER_CREATE_SERVICE);
  86. if  (sc_handle.ToInt32() != 0)
  87. {
  88. 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 );
  89. if (sv_handle.ToInt32() ==0)
  90. {
  91. CloseServiceHandle(sc_handle);
  92. return   false ;
  93. }
  94. else
  95. {
  96. //試嘗啓動服務
  97. int  i = StartService(sv_handle,0, null );
  98. if (i==0)
  99. {
  100. return   false ;
  101. }
  102. CloseServiceHandle(sc_handle);
  103. return   true ;
  104. }
  105. }
  106. else
  107. return   false ;
  108. }
  109. catch (Exception e)
  110. {
  111. throw  e;
  112. }
  113. }
  114. /// <summary>
  115. /// 反安裝服務.
  116. /// </summary>
  117. /// <param name="svcName">服務名.</param>
  118. public   bool  UnInstallService( string  svcName)
  119. {
  120. int  GENERIC_WRITE = 0x40000000;
  121. IntPtr sc_hndl = OpenSCManager( null , null ,GENERIC_WRITE);
  122. if (sc_hndl.ToInt32() !=0)
  123. {
  124. int  DELETE = 0x10000;
  125. IntPtr svc_hndl = OpenService(sc_hndl,svcName,DELETE);
  126. if (svc_hndl.ToInt32() !=0)
  127. int  i = DeleteService(svc_hndl);
  128. if  (i != 0)
  129. {
  130. CloseServiceHandle(sc_hndl);
  131. return   true ;
  132. }
  133. else
  134. {
  135. CloseServiceHandle(sc_hndl);
  136. return   false ;
  137. }
  138. }
  139. else
  140. return   false ;
  141. }
  142. else
  143. return   false ;
  144. }
  145. }
  146. }
發佈了60 篇原創文章 · 獲贊 12 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章