Skip to content

Commit 91ca1e3

Browse files
committed
Updated dependencies.
1 parent 973dcbd commit 91ca1e3

23 files changed

+466
-244
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage attributes that are applied to this project.
2+
// Project-level suppression either have no target or are given a specific target and scoped to a namespace, type, member, etc.
3+
4+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:Closing brace must be followed by blank line", Justification = "My style")]
5+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File must have header", Justification = "My style")]
6+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File must have header", Justification = "My style")]
7+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name must match first type name", Justification = "My style")]
8+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1516:Elements must be separated by blank line", Justification = "My style")]
9+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:Elements must appear in the correct order", Justification = "My style")]
10+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1306:Field names must begin with lower-case letter", Justification = "My style")]
11+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1504:All accessors must be single-line or multi-line", Justification = "My style")]
12+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1502:Element must not be on a single line", Justification = "My style")]
13+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:Elements must be ordered by access", Justification = "My style")]
14+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1124:Do not use regions", Justification = "Seriously?")]
15+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1514:Element documentation header must be preceded by blank line", Justification = "My style")]
16+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "My style")]
17+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces must not be omitted", Justification = "My style")]
18+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312:Variable names must begin with lower-case letter", Justification = "My style")]
19+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1405:Debug.Assert must provide message text", Justification = "When you have contracts we can talk about it")]
20+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1510:Chained statement blocks must not be preceded by blank line", Justification = "My style")]
21+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1520:Use braces consistently", Justification = "My style")]
22+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1519:Braces must not be omitted from multi-line child statement", Justification = "My style")]
23+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1002:Semicolons must be spaced correctly", Justification = "My style")]
24+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static elements should appear before instance elements", Justification = "My style")]
25+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "My style")]
26+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1720:Identifier contains type name", Justification = "My style")]

Kill-Update-Plugin/Kill-Update-Plugin.cs

+78-26
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
namespace KillUpdate
1414
{
15-
public class KillUpdatePlugin : TaskbarIconHost.IPluginClient
15+
#pragma warning disable CS8618 // Non-nullable property is uninitialized
16+
public class KillUpdatePlugin : TaskbarIconHost.IPluginClient, IDisposable
1617
{
1718
#region Plugin
1819
public string Name
@@ -94,7 +95,7 @@ public bool GetMenuIsChecked(ICommand command)
9495
return MenuIsCheckedTable[command]();
9596
}
9697

97-
public Bitmap GetMenuIcon(ICommand command)
98+
public Bitmap? GetMenuIcon(ICommand command)
9899
{
99100
return null;
100101
}
@@ -198,21 +199,24 @@ public bool IsClosed
198199

199200
private T LoadEmbeddedResource<T>(string resourceName)
200201
{
202+
Assembly assembly = Assembly.GetExecutingAssembly();
203+
string ResourcePath = string.Empty;
204+
201205
// Loads an "Embedded Resource" of type T (ex: Bitmap for a PNG file).
202-
foreach (string ResourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames())
203-
if (ResourceName.EndsWith(resourceName))
204-
{
205-
using (Stream rs = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName))
206-
{
207-
T Result = (T)Activator.CreateInstance(typeof(T), rs);
208-
Logger.AddLog($"Resource {resourceName} loaded");
206+
// Make sure the resource is tagged as such in the resource properties.
207+
foreach (string Item in assembly.GetManifestResourceNames())
208+
if (Item.EndsWith(resourceName, StringComparison.InvariantCulture))
209+
ResourcePath = Item;
209210

210-
return Result;
211-
}
212-
}
211+
// If not found, it could be because it's not tagged as "Embedded Resource".
212+
if (ResourcePath.Length == 0)
213+
Logger.AddLog($"Resource {resourceName} not found");
214+
215+
using Stream rs = assembly.GetManifestResourceStream(ResourcePath);
216+
T Result = (T)Activator.CreateInstance(typeof(T), rs);
217+
Logger.AddLog($"Resource {resourceName} loaded");
213218

214-
Logger.AddLog($"Resource {resourceName} not found");
215-
return default(T);
219+
return Result;
216220
}
217221

218222
private Dictionary<ICommand, string> MenuHeaderTable = new Dictionary<ICommand, string>();
@@ -254,9 +258,7 @@ private void InitServiceManager()
254258
private void StopServiceManager()
255259
{
256260
FullRestartTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
257-
FullRestartTimer = null;
258261
UpdateTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
259-
UpdateTimer = null;
260262
}
261263

262264
private void UpdateTimerCallback(object parameter)
@@ -384,7 +386,7 @@ private void ChangeLockMode(ServiceController Service, bool lockIt)
384386
{
385387
IsIconChanged = true;
386388
ServiceStartMode NewStartType = lockIt ? ServiceStartMode.Disabled : ServiceStartMode.Manual;
387-
ServiceHelper.ChangeStartMode(Service, NewStartType);
389+
NativeMethods.ChangeStartMode(Service, NewStartType, out _);
388390

389391
StartType = NewStartType;
390392
Logger.AddLog($"Service type={StartType}");
@@ -400,8 +402,8 @@ private void StopIfRunning(ServiceController Service, bool lockIt)
400402
}
401403
}
402404

403-
private static readonly string WindowsUpdateServiceName = "wuauserv";
404-
private static readonly string LockedSettingName = "Locked";
405+
private const string WindowsUpdateServiceName = "wuauserv";
406+
private const string LockedSettingName = "Locked";
405407
private readonly TimeSpan CheckInterval = TimeSpan.FromSeconds(15);
406408
private readonly TimeSpan FullRestartInterval = TimeSpan.FromHours(1);
407409
private ServiceStartMode? StartType;
@@ -422,8 +424,8 @@ private void InitZombification()
422424

423425
Zombification = new ZombifyMe.Zombification("Kill-Update");
424426
Zombification.Delay = TimeSpan.FromMinutes(1);
425-
Zombification.WatchingMessage = null;
426-
Zombification.RestartMessage = null;
427+
Zombification.WatchingMessage = string.Empty;
428+
Zombification.RestartMessage = string.Empty;
427429
Zombification.Flags = ZombifyMe.Flags.NoWindow | ZombifyMe.Flags.ForwardArguments;
428430
Zombification.IsSymmetric = true;
429431
Zombification.AliveTimeout = TimeSpan.FromMinutes(1);
@@ -436,16 +438,66 @@ private void ExitZombification()
436438
{
437439
Logger.AddLog("ExitZombification starting");
438440

439-
if (Zombification != null)
440-
{
441-
Zombification.Cancel();
442-
Zombification = null;
443-
}
441+
Zombification.Cancel();
444442

445443
Logger.AddLog("ExitZombification done");
446444
}
447445

448446
private ZombifyMe.Zombification Zombification;
449447
#endregion
448+
449+
#region Implementation of IDisposable
450+
/// <summary>
451+
/// Called when an object should release its resources.
452+
/// </summary>
453+
/// <param name="isDisposing">Indicates if resources must be disposed now.</param>
454+
protected virtual void Dispose(bool isDisposing)
455+
{
456+
if (!IsDisposed)
457+
{
458+
IsDisposed = true;
459+
460+
if (isDisposing)
461+
DisposeNow();
462+
}
463+
}
464+
465+
/// <summary>
466+
/// Called when an object should release its resources.
467+
/// </summary>
468+
public void Dispose()
469+
{
470+
Dispose(true);
471+
GC.SuppressFinalize(this);
472+
}
473+
474+
/// <summary>
475+
/// Finalizes an instance of the <see cref="KillUpdatePlugin"/> class.
476+
/// </summary>
477+
~KillUpdatePlugin()
478+
{
479+
Dispose(false);
480+
}
481+
482+
/// <summary>
483+
/// True after <see cref="Dispose(bool)"/> has been invoked.
484+
/// </summary>
485+
private bool IsDisposed = false;
486+
487+
/// <summary>
488+
/// Disposes of every reference that must be cleaned up.
489+
/// </summary>
490+
private void DisposeNow()
491+
{
492+
using (UpdateTimer)
493+
{
494+
}
495+
496+
using (FullRestartTimer)
497+
{
498+
}
499+
}
500+
#endregion
450501
}
502+
#pragma warning restore CS8618 // Non-nullable property is uninitialized
451503
}

Kill-Update-Plugin/Kill-Update-Plugin.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
</Reference>
6767
</ItemGroup>
6868
<ItemGroup>
69+
<Compile Include="GlobalSuppressions.cs" />
6970
<Compile Include="Kill-Update-Plugin.cs" />
7071
<Compile Include="PluginDetails.cs" />
7172
<Compile Include="Properties\AssemblyInfo.cs" />

Kill-Update-Plugin/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// You can specify all the values or you can default the Build and Revision Numbers
3131
// by using the '*' as shown below:
3232
// [assembly: AssemblyVersion("1.0.*")]
33-
[assembly: AssemblyVersion("1.0.0.101")]
34-
[assembly: AssemblyFileVersion("1.0.0.74")]
33+
[assembly: AssemblyVersion("1.0.0.141")]
34+
[assembly: AssemblyFileVersion("1.0.0.81")]
3535
[assembly: NeutralResourcesLanguage("en-US")]
3636

Kill-Update-Plugin/ServiceHelper.cs

+30-40
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Diagnostics;
34
using System.Runtime.InteropServices;
45
using System.ServiceProcess;
56

6-
public static class ServiceHelper
7+
internal static class NativeMethods
78
{
89
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
910
public static extern Boolean ChangeServiceConfig(
1011
IntPtr hService,
11-
UInt32 nServiceType,
12-
UInt32 nStartType,
13-
UInt32 nErrorControl,
12+
uint nServiceType,
13+
uint nStartType,
14+
uint nErrorControl,
1415
string? lpBinaryPathName,
1516
string? lpLoadOrderGroup,
1617
IntPtr lpdwTagId,
@@ -19,45 +20,35 @@ public static extern Boolean ChangeServiceConfig(
1920
string? lpPassword,
2021
string? lpDisplayName);
2122

22-
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
23-
static extern IntPtr OpenService(
24-
IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
23+
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
24+
static extern IntPtr OpenService(IntPtr hScManager, string lpServiceName, uint dwDesiredAccess);
2525

2626
[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
27-
public static extern IntPtr OpenSCManager(
28-
string? machineName, string? databaseName, uint dwAccess);
27+
public static extern IntPtr OpenSCManager(string? machineName, string? databaseName, uint dwAccess);
2928

3029
[DllImport("advapi32.dll", EntryPoint = "CloseServiceHandle")]
31-
public static extern int CloseServiceHandle(IntPtr hSCObject);
30+
public static extern int CloseServiceHandle(IntPtr hScObject);
3231

33-
private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF;
34-
private const uint SERVICE_QUERY_CONFIG = 0x00000001;
35-
private const uint SERVICE_CHANGE_CONFIG = 0x00000002;
36-
private const uint SC_MANAGER_ALL_ACCESS = 0x000F003F;
32+
private const uint ServiceNoChange = 0xFFFFFFFF;
33+
private const uint ServiceQueryConfig = 0x00000001;
34+
private const uint ServiceChangeConfig = 0x00000002;
35+
private const uint ScManagerAllAccess = 0x000F003F;
3736

38-
public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode)
37+
public static bool ChangeStartMode(ServiceController svc, ServiceStartMode mode, out int error)
3938
{
40-
var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
41-
if (scManagerHandle == IntPtr.Zero)
42-
{
43-
throw new ExternalException("Open Service Manager Error");
44-
}
39+
error = 0;
4540

46-
var serviceHandle = OpenService(
47-
scManagerHandle,
48-
svc.ServiceName,
49-
SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);
41+
var scManagerHandle = OpenSCManager(null, null, ScManagerAllAccess);
42+
Debug.Assert(scManagerHandle != IntPtr.Zero);
5043

51-
if (serviceHandle == IntPtr.Zero)
52-
{
53-
throw new ExternalException("Open Service Error");
54-
}
44+
var serviceHandle = OpenService(scManagerHandle, svc.ServiceName, ServiceQueryConfig | ServiceChangeConfig);
45+
Debug.Assert(serviceHandle != IntPtr.Zero);
5546

56-
var result = ChangeServiceConfig(
47+
bool result = ChangeServiceConfig(
5748
serviceHandle,
58-
SERVICE_NO_CHANGE,
49+
ServiceNoChange,
5950
(uint)mode,
60-
SERVICE_NO_CHANGE,
51+
ServiceNoChange,
6152
null,
6253
null,
6354
IntPtr.Zero,
@@ -66,15 +57,14 @@ public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode)
6657
null,
6758
null);
6859

69-
if (result == false)
70-
{
71-
int nError = Marshal.GetLastWin32Error();
72-
var win32Exception = new Win32Exception(nError);
73-
throw new ExternalException("Could not change service start type: "
74-
+ win32Exception.Message);
75-
}
60+
if (!result)
61+
error = Marshal.GetLastWin32Error();
7662

77-
CloseServiceHandle(serviceHandle);
78-
CloseServiceHandle(scManagerHandle);
63+
int hResult = CloseServiceHandle(serviceHandle);
64+
Debug.Assert(hResult == 0);
65+
hResult = CloseServiceHandle(scManagerHandle);
66+
Debug.Assert(hResult == 0);
67+
68+
return result;
7969
}
8070
}
Binary file not shown.

0 commit comments

Comments
 (0)