From 89e391868dc1d95d3f52594110b787b15f51aef5 Mon Sep 17 00:00:00 2001 From: Kirill Osenkov Date: Mon, 26 Aug 2024 20:11:14 -0700 Subject: [PATCH] Add a .NET tool project --- Directory.Build.props | 8 ++++ LockCheck.Tests/LockCheck.Tests.csproj | 2 +- LockCheck.sln | 14 +++++++ LockCheck/LockCheck.csproj | 4 +- LockCheck/Windows/NativeMethods.cs | 15 +++++-- LockCheckTool/LockCheckTool.csproj | 27 +++++++++++++ LockCheckTool/Program.cs | 55 ++++++++++++++++++++++++++ Test.NetCore/Test.NetCore.csproj | 2 +- 8 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 Directory.Build.props create mode 100644 LockCheckTool/LockCheckTool.csproj create mode 100644 LockCheckTool/Program.cs diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..3980ab7 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,8 @@ + + + + embedded + latest + + + \ No newline at end of file diff --git a/LockCheck.Tests/LockCheck.Tests.csproj b/LockCheck.Tests/LockCheck.Tests.csproj index 75e8e5c..4f8ed0f 100644 --- a/LockCheck.Tests/LockCheck.Tests.csproj +++ b/LockCheck.Tests/LockCheck.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net8.0 false diff --git a/LockCheck.sln b/LockCheck.sln index 715eede..cdf1e73 100644 --- a/LockCheck.sln +++ b/LockCheck.sln @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.NetCore", "Test.NetCor EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LockCheck.Tests", "LockCheck.Tests\LockCheck.Tests.csproj", "{2970DE55-5A3A-4252-ABF9-9146A759AFD2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LockCheckTool", "LockCheckTool\LockCheckTool.csproj", "{AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +76,18 @@ Global {2970DE55-5A3A-4252-ABF9-9146A759AFD2}.Release|Mixed Platforms.Build.0 = Release|Any CPU {2970DE55-5A3A-4252-ABF9-9146A759AFD2}.Release|Win32.ActiveCfg = Release|Any CPU {2970DE55-5A3A-4252-ABF9-9146A759AFD2}.Release|Win32.Build.0 = Release|Any CPU + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Mixed Platforms.ActiveCfg = Debug|Mixed Platforms + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Mixed Platforms.Build.0 = Debug|Mixed Platforms + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Win32.ActiveCfg = Debug|Win32 + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Debug|Win32.Build.0 = Debug|Win32 + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Any CPU.Build.0 = Release|Any CPU + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Mixed Platforms.ActiveCfg = Release|Mixed Platforms + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Mixed Platforms.Build.0 = Release|Mixed Platforms + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Win32.ActiveCfg = Release|Win32 + {AAB30D88-20E8-5AF2-E863-11F6D5BCCF0B}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LockCheck/LockCheck.csproj b/LockCheck/LockCheck.csproj index 45a6f6f..0eaf800 100644 --- a/LockCheck/LockCheck.csproj +++ b/LockCheck/LockCheck.csproj @@ -1,7 +1,7 @@  - net472;netcoreapp3.1 + net472;net8.0 true @@ -21,7 +21,7 @@ true - $(NoWarn);CA1031;CA1303;CA1801;CA1716;NU5105 + $(NoWarn);CA1031;CA1303;CA1416;CA1801;CA1716;NU5105 true diff --git a/LockCheck/Windows/NativeMethods.cs b/LockCheck/Windows/NativeMethods.cs index e685531..d48d7fd 100644 --- a/LockCheck/Windows/NativeMethods.cs +++ b/LockCheck/Windows/NativeMethods.cs @@ -223,14 +223,21 @@ internal static int GetProcessSessionId(int dwProcessId) internal static string GetProcessOwner(SafeProcessHandle handle) { - if (OpenProcessToken(handle, TOKEN_QUERY, out var token)) + try { - if (ProcessTokenToSid(token, out var sid)) + if (OpenProcessToken(handle, TOKEN_QUERY, out var token)) { - var x = new SecurityIdentifier(sid); - return x.Translate(typeof(NTAccount)).Value; + if (ProcessTokenToSid(token, out var sid)) + { + var x = new SecurityIdentifier(sid); + return x.Translate(typeof(NTAccount)).Value; + } } } + catch + { + } + return null; } diff --git a/LockCheckTool/LockCheckTool.csproj b/LockCheckTool/LockCheckTool.csproj new file mode 100644 index 0000000..ea39b12 --- /dev/null +++ b/LockCheckTool/LockCheckTool.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + Exe + + + + major + 1.0.1 + true + lockcheck + True + lockchecktool + cklutz + A tool to list processes locking a given file. + MIT + https://github.com/cklutz/LockCheck + https://github.com/cklutz/LockCheck + .NET dotnet process lock check tool lockcheck + + + + + + + \ No newline at end of file diff --git a/LockCheckTool/Program.cs b/LockCheckTool/Program.cs new file mode 100644 index 0000000..66c6f29 --- /dev/null +++ b/LockCheckTool/Program.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using System.Linq; + +namespace LockCheck +{ + internal class Program + { + private static int Main(string[] args) + { + try + { + if (args.Length == 0) + { + Console.Error.WriteLine("Usage: {0} FILE [FILE ...]", + typeof(Program).Assembly.GetName().Name); + } + + var infos = LockManager.GetLockingProcessInfos(args); + if (!infos.Any()) + { + Console.WriteLine("No locking processes found."); + return 0; + } + + bool first = true; + foreach (var p in infos) + { + if (!first) + { + Console.WriteLine("----------------------------------------------------"); + } + + Console.WriteLine("Process ID : {0}", p.ProcessId); + Console.WriteLine("Application Name : {0}", p.ApplicationName); + Console.WriteLine("Path : {0}", p.ExecutableFullPath); + Console.WriteLine("Process Start Time: {0}", p.StartTime.ToString("F")); + first = false; + } + } + catch (Win32Exception ex) + { + Console.Error.WriteLine(ex.Message); + return ex.ErrorCode; + } + catch (Exception ex) + { + Console.Error.WriteLine(ex); + return ex.HResult; + } + + return 0; + } + } +} diff --git a/Test.NetCore/Test.NetCore.csproj b/Test.NetCore/Test.NetCore.csproj index 67fb9ca..df85754 100644 --- a/Test.NetCore/Test.NetCore.csproj +++ b/Test.NetCore/Test.NetCore.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net8.0