Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs (C#)

Code before:
                        }
                    }
                }
            }
            finally
            {
                if (scManagerHandle != nint.Zero)
                {
                    bool succeeded = NativeMethods.CloseServiceHandle(scManagerHandle);
                    Diagnostics.Assert(succeeded, "SCManager handle close failed");
                }
            }
        }

        #endregion Overrides

#nullable enable
        /// <summary>
        /// Adds UserName, Description, BinaryPathName, DelayedAutoStart and StartupType to a ServiceController object.
        /// </summary>
        /// <param name="scManagerHandle">Handle to the local SCManager instance.</param>
        /// <param name="service"></param>
        /// <returns>ServiceController as PSObject with UserName, Description and StartupType added.</returns>
        private static PSObject AddProperties(nint scManagerHandle, ServiceController service)
        {
            NakedWin32Handle hService = IntPtr.Zero;

            // As these are optional values, a failure due to permissions or
            // other problem is ignored and the properties are set to null.
            bool isDelayedAutoStart = false;
            string? binPath = null;
            string? description = null;
            string? startName = null;
            ServiceStartupType startupType = ServiceStartupType.InvalidValue;
            try
            {
                // We don't use service.ServiceHandle as that requests
                // SERVICE_ALL_ACCESS when we only need SERVICE_QUERY_CONFIG.
                hService = NativeMethods.OpenServiceW(
                    scManagerHandle,
                    service.ServiceName,
                    NativeMethods.SERVICE_QUERY_CONFIG
                );
                if (hService != nint.Zero)
                {
                    if (NativeMethods.QueryServiceConfig2(
                        hService,
                        NativeMethods.SERVICE_CONFIG_DESCRIPTION,
                        out NativeMethods.SERVICE_DESCRIPTIONW descriptionInfo))
                    {
                        description = descriptionInfo.lpDescription;

Review comment (suggestion):
```suggestion
            NakedWin32Handle hService = nint.Zero;
```

Code after:
                        }
                    }
                }
            }
            finally
            {
                if (scManagerHandle != nint.Zero)
                {
                    bool succeeded = NativeMethods.CloseServiceHandle(scManagerHandle);
                    Diagnostics.Assert(succeeded, "SCManager handle close failed");
                }
            }
        }

        #endregion Overrides

#nullable enable
        /// <summary>
        /// Adds UserName, Description, BinaryPathName, DelayedAutoStart and StartupType to a ServiceController object.
        /// </summary>
        /// <param name="scManagerHandle">Handle to the local SCManager instance.</param>
        /// <param name="service"></param>
        /// <returns>ServiceController as PSObject with UserName, Description and StartupType added.</returns>
        private static PSObject AddProperties(nint scManagerHandle, ServiceController service)
        {
            NakedWin32Handle hService = nint.Zero;

            // As these are optional values, a failure due to permissions or
            // other problem is ignored and the properties are set to null.
            bool? isDelayedAutoStart = null;
            string? binPath = null;
            string? description = null;
            string? startName = null;
            ServiceStartupType startupType = ServiceStartupType.InvalidValue;
            try
            {
                // We don't use service.ServiceHandle as that requests
                // SERVICE_ALL_ACCESS when we only need SERVICE_QUERY_CONFIG.
                hService = NativeMethods.OpenServiceW(
                    scManagerHandle,
                    service.ServiceName,
                    NativeMethods.SERVICE_QUERY_CONFIG
                );
                if (hService != nint.Zero)
                {
                    if (NativeMethods.QueryServiceConfig2(
                        hService,
                        NativeMethods.SERVICE_CONFIG_DESCRIPTION,
                        out NativeMethods.SERVICE_DESCRIPTIONW descriptionInfo))
                    {
                        description = descriptionInfo.lpDescription;