PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBWin7TriggerStartService/NativeMethods.vb

#
Visual Basic | 378 lines | 112 code | 84 blank | 182 comment | 0 complexity | a42684e3de318f5d334afb94f2df3f39 MD5 | raw file
  1. '****************************** Module Header ******************************'
  2. ' Module Name: NativeMethods.vb
  3. ' Project: VBWin7TriggerStartService
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' The P/Invoke signatures of native service APIs and structs.
  7. '
  8. ' This source is subject to the Microsoft Public License.
  9. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  10. ' All other rights reserved.
  11. '
  12. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. '***************************************************************************'
  16. #Region "Imports directives"
  17. Imports System.Runtime.InteropServices
  18. Imports System.Security
  19. Imports Microsoft.Win32.SafeHandles
  20. #End Region
  21. <Flags()> _
  22. Public Enum ServiceControlAccessRights
  23. ' Includes STANDARD_RIGHTS_REQUIRED, in addition to all access rights in
  24. ' this table.
  25. SC_MANAGER_ALL_ACCESS = &HF003F
  26. ' Required to connect to the service control manager.
  27. SC_MANAGER_CONNECT = 1
  28. ' Required to call the CreateService function to create a service
  29. ' object and add it to the database.
  30. SC_MANAGER_CREATE_SERVICE = 2
  31. ' Required to call the EnumServicesStatusEx function to list the
  32. ' services that are in the database.
  33. SC_MANAGER_ENUMERATE_SERVICE = 4
  34. ' Required to call the LockServiceDatabase function to acquire a lock on
  35. ' the database.
  36. SC_MANAGER_LOCK = 8
  37. ' Required to call the QueryServiceLockStatus function to retrieve the
  38. ' lock status information for the database
  39. SC_MANAGER_QUERY_LOCK_STATUS = &H10
  40. ' Required to call the NotifyBootConfigStatus function.
  41. SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
  42. End Enum
  43. <Flags()> _
  44. Public Enum ServiceAccessRights
  45. ' Required to call the QueryServiceConfig and QueryServiceConfig2
  46. ' functions to query the service configuration.
  47. SERVICE_QUERY_CONFIG = 1
  48. ' Required to call the ChangeServiceConfig or ChangeServiceConfig2
  49. ' function to change the service configuration. Because this grants the
  50. ' caller the right to change the executable file that the system runs,
  51. ' it should be granted only to administrators.
  52. SERVICE_CHANGE_CONFIG = 2
  53. ' Required to call the QueryServiceStatusEx function to ask the service
  54. ' control manager about the status of the service.
  55. SERVICE_QUERY_STATUS = 4
  56. ' Required to call the EnumDependentServices function to enumerate all
  57. ' the services dependent on the service.
  58. SERVICE_ENUMERATE_DEPENDENTS = 8
  59. ' Required to call the StartService function to start the service.
  60. SERVICE_START = &H10
  61. ' Required to call the ControlService function to stop the service.
  62. SERVICE_STOP = &H20
  63. ' Required to call the ControlService function to pause or continue the
  64. ' service.
  65. SERVICE_PAUSE_CONTINUE = &H40
  66. ' Required to call the ControlService function to ask the service to
  67. ' report its status immediately.
  68. SERVICE_INTERROGATE = &H80
  69. ' Required to call the ControlService function to specify a user-defined
  70. ' control code.
  71. SERVICE_USER_DEFINED_CONTROL = &H100
  72. ' Includes STANDARD_RIGHTS_REQUIRED in addition to all access rights in
  73. ' this table.
  74. SERVICE_ALL_ACCESS = &HF01FF
  75. End Enum
  76. Public Enum ServiceConfig2InfoLevel As UInt32
  77. SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3
  78. SERVICE_CONFIG_DESCRIPTION = 1
  79. SERVICE_CONFIG_FAILURE_ACTIONS = 2
  80. SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 4
  81. SERVICE_CONFIG_PREFERRED_NODE = 9
  82. SERVICE_CONFIG_PRESHUTDOWN_INFO = 7
  83. SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6
  84. SERVICE_CONFIG_SERVICE_SID_INFO = 5
  85. SERVICE_CONFIG_TRIGGER_INFO = 8
  86. End Enum
  87. Public Enum ServiceTriggerType As UInt32
  88. ' The event is triggered when a device of the specified device interface
  89. ' class arrives or is present when the system starts. This trigger event
  90. ' is commonly used to start a service.
  91. SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL = 1
  92. ' The event is triggered when the first IP address on the TCP/IP
  93. ' networking stack becomes available or the last IP address on the stack
  94. ' becomes unavailable. This trigger event can be used to start or stop a
  95. ' service.
  96. SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY = 2
  97. ' The event is triggered when the computer joins or leaves a domain.
  98. ' This trigger event can be used to start or stop a service.
  99. SERVICE_TRIGGER_TYPE_DOMAIN_JOIN = 3
  100. ' The event is triggered when a firewall port is opened or approximately
  101. ' 60 seconds after the firewall port is closed. This trigger event can
  102. ' be used to start or stop a service.
  103. SERVICE_TRIGGER_TYPE_FIREWALL_PORT_EVENT = 4
  104. ' The event is triggered when a machine policy or user policy change
  105. ' occurs. This trigger event is commonly used to start a service.
  106. SERVICE_TRIGGER_TYPE_GROUP_POLICY = 5
  107. ' The event is a custom event generated by an Event Tracing for Windows
  108. ' (ETW) provider. This trigger event can be used to start or stop a
  109. ' service.
  110. SERVICE_TRIGGER_TYPE_CUSTOM = 20
  111. End Enum
  112. Public Enum ServiceTriggerAction As UInt32
  113. ' Start the service when the specified trigger event occurs.
  114. SERVICE_TRIGGER_ACTION_SERVICE_START = 1
  115. ' Stop the service when the specified trigger event occurs.
  116. SERVICE_TRIGGER_ACTION_SERVICE_STOP = 2
  117. End Enum
  118. Public Enum ServiceTriggerDataType As UInt32
  119. ' The trigger-specific data is in binary format.
  120. SERVICE_TRIGGER_DATA_TYPE_BINARY = 1
  121. ' The trigger-specific data is in string format.
  122. SERVICE_TRIGGER_DATA_TYPE_STRING = 2
  123. End Enum
  124. <StructLayout(LayoutKind.Sequential)> _
  125. Public Structure SERVICE_TRIGGER_SPECIFIC_DATA_ITEM
  126. ''' <summary>
  127. ''' The data type of the trigger-specific data pointed to by pData.
  128. ''' </summary>
  129. Public dwDataType As ServiceTriggerDataType
  130. ''' <summary>
  131. ''' The size of the trigger-specific data pointed to pData, in bytes.
  132. ''' The maximum value is 1024.
  133. ''' </summary>
  134. Public cbData As UInt32
  135. ''' <summary>
  136. ''' A pointer to the trigger-specific data for the service trigger event.
  137. ''' Strings must be Unicode; ANSI strings are not supported.
  138. ''' </summary>
  139. Public pData As IntPtr
  140. End Structure
  141. <StructLayout(LayoutKind.Sequential)> _
  142. Public Structure SERVICE_TRIGGER
  143. ''' <summary>
  144. ''' The trigger event type.
  145. ''' </summary>
  146. Public dwTriggerType As ServiceTriggerType
  147. ''' <summary>
  148. ''' The action to take when the specified trigger event occurs.
  149. ''' </summary>
  150. Public dwAction As ServiceTriggerAction
  151. ''' <summary>
  152. ''' Points to a GUID that identifies the trigger event subtype. The value
  153. ''' of this member depends on the value of the dwTriggerType member.
  154. ''' </summary>
  155. Public pTriggerSubtype As IntPtr
  156. ''' <summary>
  157. ''' The number of SERVICE_TRIGGER_SPECIFIC_DATA_ITEM structures in the
  158. ''' array pointed to by pDataItems.
  159. ''' </summary>
  160. Public cDataItems As UInt32
  161. ''' <summary>
  162. ''' A pointer to an array of SERVICE_TRIGGER_SPECIFIC_DATA_ITEM
  163. ''' structures that contain trigger-specific data.
  164. ''' </summary>
  165. Public pDataItems As IntPtr
  166. End Structure
  167. <StructLayout(LayoutKind.Sequential)> _
  168. Public Structure SERVICE_TRIGGER_INFO
  169. ''' <summary>
  170. ''' The number of triggers in the array of SERVICE_TRIGGER structures
  171. ''' pointed to by the pTriggers member.
  172. ''' </summary>
  173. Public cTriggers As UInt32
  174. ''' <summary>
  175. ''' A pointer to an array of SERVICE_TRIGGER structures that specify the
  176. ''' trigger events for the service.
  177. ''' </summary>
  178. Public pTriggers As IntPtr
  179. ''' <summary>
  180. ''' This member is reserved and must be NULL.
  181. ''' </summary>
  182. Public pReserved As IntPtr
  183. End Structure
  184. <SuppressUnmanagedCodeSecurity()> _
  185. Friend Class SafeServiceHandle
  186. Inherits SafeHandleZeroOrMinusOneIsInvalid
  187. Friend Sub New()
  188. MyBase.New(True)
  189. End Sub
  190. Protected Overrides Function ReleaseHandle() As Boolean
  191. Return NativeMethods.CloseServiceHandle(MyBase.handle)
  192. End Function
  193. End Class
  194. Friend Class NativeMethods
  195. ''' <summary>
  196. ''' Establishes a connection to the service control manager on the
  197. ''' specified computer and opens the specified service control manager
  198. ''' database.
  199. ''' </summary>
  200. ''' <param name="machineName">Name of the target computer.</param>
  201. ''' <param name="databaseName">
  202. ''' Name of the service control manager database.
  203. ''' </param>
  204. ''' <param name="dwAccess">
  205. ''' The access to the service control manager.
  206. ''' </param>
  207. ''' <returns>
  208. ''' If the function succeeds, the return value is a handle to the
  209. ''' specified service control manager database. If the function fails,
  210. ''' the return value is an invalid handle. To get extended error
  211. ''' information, call GetLastError.
  212. ''' </returns>
  213. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  214. Public Shared Function OpenSCManager( _
  215. ByVal machineName As String, ByVal databaseName As String, _
  216. ByVal dwAccess As ServiceControlAccessRights) As SafeServiceHandle
  217. End Function
  218. ''' <summary>
  219. ''' Opens an existing service.
  220. ''' </summary>
  221. ''' <param name="hSCManager">
  222. ''' A handle to the service control manager database. The OpenSCManager
  223. ''' function returns this handle.
  224. ''' </param>
  225. ''' <param name="lpServiceName">
  226. ''' The name of the service to be opened.
  227. ''' </param>
  228. ''' <param name="dwDesiredAccess">The access to the service.</param>
  229. ''' <returns>
  230. ''' If the function succeeds, the return value is a handle to the
  231. ''' specified service. If the function fails, the return value is an
  232. ''' invalid handle. To get extended error information, call GetLastError.
  233. ''' </returns>
  234. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  235. Public Shared Function OpenService( _
  236. ByVal hSCManager As SafeServiceHandle, ByVal lpServiceName As String, _
  237. ByVal dwDesiredAccess As ServiceAccessRights) As SafeServiceHandle
  238. End Function
  239. ''' <summary>
  240. ''' Changes the optional configuration parameters of a service.
  241. ''' </summary>
  242. ''' <param name="hService">A handle to the service.</param>
  243. ''' <param name="dwInfoLevel">
  244. ''' The configuration information to be changed.
  245. ''' </param>
  246. ''' <param name="lpInfo"></param>
  247. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  248. Public Shared Function ChangeServiceConfig2( _
  249. ByVal hService As SafeServiceHandle, ByVal dwInfoLevel As ServiceConfig2InfoLevel, _
  250. ByVal lpInfo As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
  251. End Function
  252. ''' <summary>
  253. ''' Retrieves the optional configuration parameters of the specified service.
  254. ''' </summary>
  255. ''' <param name="hService">A handle to the service.</param>
  256. ''' <param name="dwInfoLevel">
  257. ''' The configuration information to be queried.
  258. ''' </param>
  259. ''' <param name="lpBuffer">
  260. ''' A pointer to the buffer that receives the service configuration
  261. ''' information.
  262. ''' </param>
  263. ''' <param name="cbBufSize">
  264. ''' The size of the structure pointed to by the lpBuffer parameter, in
  265. ''' bytes.
  266. ''' </param>
  267. ''' <param name="pcbBytesNeeded">
  268. ''' A pointer to a variable that receives the number of bytes required to
  269. ''' store the configuration information, if the function fails with
  270. ''' ERROR_INSUFFICIENT_BUFFER.
  271. ''' </param>
  272. ''' <returns>
  273. ''' If the function succeeds, the return value is nonzero. If the
  274. ''' function fails, the return value is zero. To get extended error
  275. ''' information, call GetLastError.
  276. ''' </returns>
  277. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  278. Public Shared Function QueryServiceConfig2( _
  279. ByVal hService As SafeServiceHandle, ByVal dwInfoLevel As ServiceConfig2InfoLevel, _
  280. ByVal lpBuffer As IntPtr, ByVal cbBufSize As Integer, _
  281. <Out()> ByRef pcbBytesNeeded As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  282. End Function
  283. ''' <summary>
  284. ''' Closes a handle to a service control manager or service object.
  285. ''' </summary>
  286. ''' <param name="hSCObject">
  287. ''' A handle to the service control manager object or the service object
  288. ''' to close.
  289. ''' </param>
  290. <DllImport("advapi32.dll")> _
  291. Public Shared Function CloseServiceHandle(ByVal hSCObject As IntPtr) _
  292. As <MarshalAs(UnmanagedType.Bool)> Boolean
  293. End Function
  294. End Class