Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit bd264e6

Browse files
committed
Merge pull request #2685 from dotnet-bot/from-tfs
Merge changes from TFS
2 parents 0a6a0d1 + 3e4d914 commit bd264e6

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/Common/src/Interop/Windows/mincore/Interop.ServiceProcessOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ internal partial class ServiceTypeOptions
7171

7272
internal partial class ServiceStartModes
7373
{
74+
internal const int START_TYPE_BOOT = 0x00000000;
75+
internal const int START_TYPE_SYSTEM = 0x00000001;
7476
internal const int START_TYPE_AUTO = 0x00000002;
7577
internal const int START_TYPE_DEMAND = 0x00000003;
7678
internal const int START_TYPE_DISABLED = 0x00000004;

src/System.ServiceProcess.ServiceController/src/System.ServiceProcess.ServiceController.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<OutputType>Library</OutputType>
88
<RootNamespace>System.ServiceProcess.ServiceController</RootNamespace>
99
<AssemblyName>System.ServiceProcess.ServiceController</AssemblyName>
10-
<AssemblyVersion>4.0.0.0</AssemblyVersion>
10+
<AssemblyVersion>4.1.0.0</AssemblyVersion>
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1212
<ProjectGuid>{F4821CB6-91A3-4546-BC4F-E00DBFBDAA05}</ProjectGuid>
1313
</PropertyGroup>

src/System.ServiceProcess.ServiceController/src/System/ServiceProcess/ServiceController.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public class ServiceController : IDisposable
2727
private string _displayName;
2828
private int _commandsAccepted;
2929
private bool _statusGenerated;
30+
private bool _startTypeInitialized;
3031
private int _type;
3132
private bool _disposed;
3233
private SafeServiceHandle _serviceManagerHandle;
3334
private ServiceControllerStatus _status;
3435
private ServiceController[] _dependentServices;
3536
private ServiceController[] _servicesDependedOn;
37+
private ServiceStartMode _startType;
3638

3739
private const int SERVICENAMEMAXLENGTH = 80;
3840
private const int DISPLAYNAMEBUFFERSIZE = 256;
@@ -305,6 +307,56 @@ public unsafe ServiceController[] ServicesDependedOn
305307
}
306308
}
307309

310+
public ServiceStartMode StartType
311+
{
312+
get
313+
{
314+
if (_startTypeInitialized)
315+
return _startType;
316+
317+
IntPtr serviceHandle = IntPtr.Zero;
318+
try
319+
{
320+
serviceHandle = GetServiceHandle(Interop.mincore.ServiceOptions.SERVICE_QUERY_CONFIG);
321+
322+
int bytesNeeded = 0;
323+
bool success = Interop.mincore.QueryServiceConfig(serviceHandle, IntPtr.Zero, 0, out bytesNeeded);
324+
325+
int lastError = Marshal.GetLastWin32Error();
326+
if (lastError != Interop.mincore.Errors.ERROR_INSUFFICIENT_BUFFER)
327+
throw new Win32Exception(lastError);
328+
329+
// get the info
330+
IntPtr bufPtr = IntPtr.Zero;
331+
try
332+
{
333+
bufPtr = Marshal.AllocHGlobal((IntPtr)bytesNeeded);
334+
success = Interop.mincore.QueryServiceConfig(serviceHandle, bufPtr, bytesNeeded, out bytesNeeded);
335+
if (!success)
336+
throw new Win32Exception(Marshal.GetLastWin32Error());
337+
338+
Interop.mincore.QUERY_SERVICE_CONFIG config = new Interop.mincore.QUERY_SERVICE_CONFIG();
339+
Marshal.PtrToStructure(bufPtr, config);
340+
341+
_startType = (ServiceStartMode)config.dwStartType;
342+
_startTypeInitialized = true;
343+
}
344+
finally
345+
{
346+
if (bufPtr != IntPtr.Zero)
347+
Marshal.FreeHGlobal(bufPtr);
348+
}
349+
}
350+
finally
351+
{
352+
if (serviceHandle != IntPtr.Zero)
353+
Interop.mincore.CloseServiceHandle(serviceHandle);
354+
}
355+
356+
return _startType;
357+
}
358+
}
359+
308360
public SafeHandle ServiceHandle
309361
{
310362
get
@@ -357,6 +409,7 @@ protected virtual void Dispose(bool disposing)
357409
}
358410

359411
_statusGenerated = false;
412+
_startTypeInitialized = false;
360413
_type = Interop.mincore.ServiceTypeOptions.SERVICE_TYPE_ALL;
361414
_disposed = true;
362415
}
@@ -673,6 +726,7 @@ public unsafe void Continue()
673726
public void Refresh()
674727
{
675728
_statusGenerated = false;
729+
_startTypeInitialized = false;
676730
_dependentServices = null;
677731
_servicesDependedOn = null;
678732
}

src/System.ServiceProcess.ServiceController/src/System/ServiceProcess/ServiceStartMode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ public enum ServiceStartMode
88
Manual = Interop.mincore.ServiceStartModes.START_TYPE_DEMAND,
99
Automatic = Interop.mincore.ServiceStartModes.START_TYPE_AUTO,
1010
Disabled = Interop.mincore.ServiceStartModes.START_TYPE_DISABLED,
11+
Boot = Interop.mincore.ServiceStartModes.START_TYPE_BOOT,
12+
System = Interop.mincore.ServiceStartModes.START_TYPE_SYSTEM
1113
}
1214
}

0 commit comments

Comments
 (0)