Skip to content

Commit

Permalink
Merge pull request #11 from KirillOsenkov/dev/kirillo/tool
Browse files Browse the repository at this point in the history
Add a .NET tool project
  • Loading branch information
cklutz authored Oct 1, 2024
2 parents 0e52833 + 89e3918 commit 0e14e0a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>

<PropertyGroup>
<DebugType>embedded</DebugType>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion LockCheck.Tests/LockCheck.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
14 changes: 14 additions & 0 deletions LockCheck.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions LockCheck/LockCheck.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand All @@ -21,7 +21,7 @@

<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CA1031;CA1303;CA1801;CA1716;NU5105</NoWarn>
<NoWarn>$(NoWarn);CA1031;CA1303;CA1416;CA1801;CA1716;NU5105</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
15 changes: 11 additions & 4 deletions LockCheck/Windows/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 27 additions & 0 deletions LockCheckTool/LockCheckTool.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<PropertyGroup>
<RollForward>major</RollForward>
<Version>1.0.1</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>lockcheck</ToolCommandName>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>lockchecktool</PackageId>
<Authors>cklutz</Authors>
<Description>A tool to list processes locking a given file.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/cklutz/LockCheck</PackageProjectUrl>
<RepositoryUrl>https://github.com/cklutz/LockCheck</RepositoryUrl>
<PackageTags>.NET dotnet process lock check tool lockcheck</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LockCheck\LockCheck.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions LockCheckTool/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Test.NetCore/Test.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 0e14e0a

Please sign in to comment.