From 5bc19e43af32afad383f9ed4f62f22b0255d6093 Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Sun, 2 Dec 2018 00:00:00 +0800 Subject: [PATCH] Introduce constant strings --- src/Core/WinSWCore/Native/Advapi32.cs | 30 +++++++++++++------------ src/Core/WinSWCore/Native/Kernel32.cs | 6 +++-- src/Core/WinSWCore/Util/SigIntHelper.cs | 10 ++++----- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/Core/WinSWCore/Native/Advapi32.cs b/src/Core/WinSWCore/Native/Advapi32.cs index 9efb32c..4289edd 100755 --- a/src/Core/WinSWCore/Native/Advapi32.cs +++ b/src/Core/WinSWCore/Native/Advapi32.cs @@ -273,48 +273,50 @@ namespace winsw.Native /// public class Advapi32 { - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private const string Advapi32LibraryName = "advapi32.dll"; + + [DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, IntPtr lpInfo); - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, ref SERVICE_FAILURE_ACTIONS sfa); - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, ref SERVICE_DELAYED_AUTO_START sfa); - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern IntPtr OpenSCManager(string? machineName, string? databaseName, uint dwAccess); - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess); - [DllImport("advapi32.dll", SetLastError = true)] + [DllImport(Advapi32LibraryName, SetLastError = true)] internal static extern bool CloseServiceHandle(IntPtr hSCObject); - [DllImport("advapi32.DLL")] + [DllImport(Advapi32LibraryName)] public static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus); - [DllImport("advapi32.dll")] + [DllImport(Advapi32LibraryName)] internal static extern uint LsaOpenPolicy(ref LSA_UNICODE_STRING SystemName, ref LSA_OBJECT_ATTRIBUTES ObjectAttributes, int DesiredAccess, out IntPtr PolicyHandle); - [DllImport("advapi32.dll", SetLastError = true)] + [DllImport(Advapi32LibraryName, SetLastError = true)] internal static extern uint LsaAddAccountRights(IntPtr PolicyHandle, IntPtr AccountSid, LSA_UNICODE_STRING[] UserRights, uint CountOfRights); - [DllImport("advapi32")] + [DllImport(Advapi32LibraryName)] internal static extern void FreeSid(IntPtr pSid); - [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [DllImport(Advapi32LibraryName, CharSet = CharSet.Auto, SetLastError = true)] internal static extern bool LookupAccountName(string lpSystemName, string lpAccountName, IntPtr psid, ref int cbsid, StringBuilder domainName, ref int cbdomainLength, ref int use); - [DllImport("advapi32.dll")] + [DllImport(Advapi32LibraryName)] internal static extern bool IsValidSid(IntPtr pSid); - [DllImport("advapi32.dll", SetLastError = true)] + [DllImport(Advapi32LibraryName, SetLastError = true)] internal static extern uint LsaClose(IntPtr ObjectHandle); - [DllImport("advapi32.dll", SetLastError = false)] + [DllImport(Advapi32LibraryName, SetLastError = false)] internal static extern uint LsaNtStatusToWinError(uint status); } diff --git a/src/Core/WinSWCore/Native/Kernel32.cs b/src/Core/WinSWCore/Native/Kernel32.cs index 1193406..bf323db 100755 --- a/src/Core/WinSWCore/Native/Kernel32.cs +++ b/src/Core/WinSWCore/Native/Kernel32.cs @@ -9,10 +9,12 @@ namespace winsw.Native /// public class Kernel32 { - [DllImport("kernel32.dll", SetLastError = true)] + private const string Kernel32LibraryName = "kernel32.dll"; + + [DllImport(Kernel32LibraryName, SetLastError = true)] public static extern bool SetStdHandle(int nStdHandle, SafeFileHandle handle); - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport(Kernel32LibraryName, SetLastError = true)] public static extern bool CreateProcess( string? lpApplicationName, string lpCommandLine, diff --git a/src/Core/WinSWCore/Util/SigIntHelper.cs b/src/Core/WinSWCore/Util/SigIntHelper.cs index 11f8f65..c915d19 100644 --- a/src/Core/WinSWCore/Util/SigIntHelper.cs +++ b/src/Core/WinSWCore/Util/SigIntHelper.cs @@ -9,15 +9,15 @@ namespace winsw.Util { private static readonly ILog Logger = LogManager.GetLogger(typeof(SigIntHelper)); - private const string KERNEL32 = "kernel32.dll"; + private const string Kernel32LibraryName = "kernel32.dll"; - [DllImport(KERNEL32, SetLastError = true)] + [DllImport(Kernel32LibraryName, SetLastError = true)] private static extern bool AttachConsole(uint dwProcessId); - [DllImport(KERNEL32, SetLastError = true)] + [DllImport(Kernel32LibraryName, SetLastError = true)] private static extern bool FreeConsole(); - [DllImport(KERNEL32)] + [DllImport(Kernel32LibraryName)] private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, bool Add); // Delegate type to be used as the Handler Routine for SCCH @@ -33,7 +33,7 @@ namespace winsw.Util CTRL_SHUTDOWN_EVENT } - [DllImport(KERNEL32)] + [DllImport(Kernel32LibraryName)] private static extern bool GenerateConsoleCtrlEvent(CtrlTypes dwCtrlEvent, uint dwProcessGroupId); ///