Skip to content

Dangerously hijack file handles to close them #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 306 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
306 commits
Select commit Hold shift + click to select a range
3c1b2ca
docs: finish unfinished xml doc
BinToss Oct 15, 2022
2f22c7e
docs: fix invalid reference of renamed enum
BinToss Oct 15, 2022
1236997
docs: clean up re-used docs to reflect new class
BinToss Oct 15, 2022
a8ac39b
refactor: replace FileLockerEx.GetFileLockerEx() with constructor
BinToss Oct 15, 2022
47b1bb7
refactor: rename ResultsFilter to HandlesFilter
BinToss Oct 15, 2022
637471f
feat: add equivalent methods for handling handles to public API
BinToss Oct 15, 2022
f203ee1
refactor: resolve warning PInvoke004
BinToss Oct 20, 2022
7cd5501
refactor: set structs, fields as readonly; ignore CS0649
BinToss Oct 20, 2022
216a1ef
refactor: override GetHashCode() in pseudo-funcptr
BinToss Oct 20, 2022
e52fdbd
refactor: disable warning CS0169 for reserved field
BinToss Oct 20, 2022
3c6cc06
refactor: move EnterDebugMode call to class FileLockerEx
BinToss Oct 24, 2022
c1ce4e7
refactor: move HandlesFilter params from const to methods
BinToss Oct 24, 2022
a6c31d2
refactor: override SafeFileHandleEx ToString with JSON serialization
BinToss Oct 28, 2022
7616902
refactor: replace ToArray with Add and separate instantiation
BinToss Oct 28, 2022
ab3435f
refactor: add method CheckAccess for debugging purposes
BinToss Oct 28, 2022
d59487b
refactor: add parameter to pass out WarningException
BinToss Oct 28, 2022
79397ec
refactor: move CsWin32 supplements to new files
BinToss Jan 3, 2023
97e4992
refactor: slightly improve getting process command lines
BinToss Jan 3, 2023
7d49548
refactor: move SYSTEM_HANDLE structs to new files
BinToss Jan 3, 2023
123d581
refactor: catch exceptions thrown by file info queries
BinToss Jan 3, 2023
f0763ab
refactor: add constructor for copying a SafeHandleEx
BinToss Jan 3, 2023
0e2083a
refactor: handle System process id issues; move GetFullProcessImageName
BinToss Jan 3, 2023
44bccb8
refactor: ensure correct ACCESS_MASK is used
BinToss Jan 3, 2023
023a2df
refactor: remove unused usings, update prop type and ref
BinToss Jan 3, 2023
780275a
refactor: change ToString from JSON serialization to formatted text
BinToss Jan 3, 2023
7d3ae8e
refactor: change Exception types, misc changes in GetSystemHandleInfoEx
BinToss Jan 3, 2023
572ce32
refactor: use conditional access
BinToss Jan 3, 2023
41b4c5a
refactor: if PID == 4, don't open process handle
BinToss Jan 7, 2023
1f1dc4f
refactor: add more native definitions, supporting Types
BinToss Feb 13, 2023
cab4e5b
refactor: inline temporary variable
BinToss Mar 27, 2023
9c29b0a
refactor: rename UnlockSystemHandle to CloseSourceHandle
BinToss Mar 27, 2023
137c35f
refactor: refactor NTSTATUS, mimic dotnet/PInvoke
BinToss Mar 27, 2023
a9d70cb
docs: update inline code docs
BinToss Mar 27, 2023
7a21538
style: update file encoding to UTF-8 BOM
BinToss Mar 27, 2023
f506116
refactor: move unsafe context to method declaration
BinToss Mar 27, 2023
777e7ee
refactor: simplify 'using' statement
BinToss Mar 27, 2023
96f3e5e
fix: use correct overload for 'out' param modifier
BinToss Mar 27, 2023
0242b56
fix: use correct types for param rgsFileNames of RmRegisterResources
BinToss Mar 27, 2023
58faeaf
docs: remove a TODO (will not implement)
BinToss Mar 27, 2023
1a8cb83
refactor: improve details of custom exceptions
BinToss Mar 27, 2023
d1fe87b
refactor: replace target-OS vars with host-OS vars
BinToss Mar 27, 2023
ad659aa
refactor: utilize new ctor to init buffer
BinToss Mar 27, 2023
143b27d
refactor: overhaul GetProcessCommandLine
BinToss Mar 27, 2023
4b9135c
refactor: improve GetFullProcessImageName
BinToss Mar 27, 2023
ab43042
refactor: assign native handle value to SafeHandle handle field
BinToss Mar 27, 2023
79f2abe
refactor: check if handle is File handle in SafeFileHandleEx ctor
BinToss Mar 27, 2023
33453cc
refactor: improve TryGetFinalPath
BinToss Mar 27, 2023
83c4bb1
refactor: replace C-style (U)LARGE_INTEGER structs with c# types
BinToss Mar 26, 2023
06ac4f6
reactor: add internal method IsDebugModeEnabled
BinToss Mar 26, 2023
f13cff9
build: set EmitCompilerGeneratedFiles to true
BinToss Mar 26, 2023
e11386b
refactor: update NativeMethods.txt symbols
BinToss Mar 26, 2023
ee29401
style: remove trailing whitespace
BinToss Mar 26, 2023
23a207f
refactor: clarify exception; make Path readonly
BinToss Mar 26, 2023
63dc718
refactor: move SafeBufferT; add instance methods; utilize it
BinToss Mar 26, 2023
75dc135
docs: update inline documentation
BinToss Mar 26, 2023
f652e54
style: remove trailing whitespace
BinToss Mar 27, 2023
19469b6
refactor: move UIntPtr64<T> to new file
BinToss Mar 27, 2023
8dd3f01
refactor: add struct PS_PROTECTION
BinToss Mar 27, 2023
8605bce
refactor: display NTSTATUS Code enum value in debugger
BinToss Mar 27, 2023
911b58e
refactor: remove redundant member 'AtlThunkSListPtr32'
BinToss Mar 27, 2023
2efab6f
refactor: finish OBJECT_TYPES_INFORMATION definition
BinToss Mar 27, 2023
edd8920
refactor: set LDR_DATA_TABLE_ENTRY members to readonly
BinToss Mar 27, 2023
01edeb7
style: remove trailing whitespace
BinToss Mar 27, 2023
f128e47
refactor: set structs or their members to readonly
BinToss Mar 27, 2023
49b0e18
style: remove trailing whitespace
BinToss Mar 27, 2023
3eca414
chore: disable CS0649 for readonly interop structs
BinToss Mar 27, 2023
af39417
fix: properly convert UNICODE_STRING 'byte' length to string's 'char'…
BinToss Mar 27, 2023
a3057cc
refactor: replace LARGE_INTEGER with long
BinToss Mar 27, 2023
cf09984
fix: use non-generic LIST_ENTRY
BinToss Mar 27, 2023
72c5d2d
fix: prevent runtime TypeLoadException
BinToss Mar 27, 2023
53a3cde
fix: UNICODE_STRING.ToStringLength() no longer returns garbage
BinToss Mar 27, 2023
0a44710
refactor: rename two functions back to their original names
BinToss Mar 27, 2023
70f8390
refactor: change ObjectTypesInformationBuffer to internal
BinToss Mar 27, 2023
51b3682
refactor: rename function to original
BinToss Mar 27, 2023
dd2a087
refactor: move const to local scope
BinToss Mar 28, 2023
9ee76c8
refactor: add GetSafeHandle() to system handle
BinToss Mar 28, 2023
625e24c
refactor: set HANDLE members to internal
BinToss Mar 28, 2023
894e679
refactor: replace PhGetObjectTypeName with custom solution
BinToss Mar 28, 2023
06e1882
refactor: remove dysfunctional Ph functions
BinToss Mar 28, 2023
c83db4d
refactor: add GetHandleInformation method to system handle
BinToss Mar 28, 2023
5bc848a
refactor: update usings
BinToss Mar 28, 2023
ef4337e
refactor: simplify access to JsonIgnore
BinToss Mar 28, 2023
15fdd44
refactor: rename GetHandleInformation wrapper
BinToss Mar 28, 2023
abf91de
refactor: remove unused ToSafeFileHandle()
BinToss Mar 28, 2023
34128bf
refactor: base ObjectTypesInformationBuffer on SafeBuffer<T>
BinToss Mar 28, 2023
dcdee0b
refactor: set SYSTEM_HANDLE_INFORMATION_EX as readonly
BinToss Mar 28, 2023
96016f1
refactor: compare against Win32ErrorCode
BinToss Mar 28, 2023
c4d3ece
docs: remove old TODO
BinToss Mar 28, 2023
952f1ba
refactor: remove ObjectTypesInformationBuffer
BinToss Mar 28, 2023
bca3936
refactor: move SYSTEM_HANDLE_INFORMATION_EX to Windows.Win32
BinToss Mar 28, 2023
326cfaa
docs: remove old comments
BinToss Mar 28, 2023
247e2eb
refactor: make NTSTATUS more similar to PInvoke definition
BinToss Mar 28, 2023
238ce6b
refactor: add exception-throwing wrapper for GetHandleInformation
BinToss Mar 28, 2023
aa3a482
refactor: move IsFileHandle() to SafeHandleEx
BinToss Mar 28, 2023
feee78c
refactor: add TryGetFullProcessImageName
BinToss Mar 28, 2023
8e46b33
refactor: return a tuple, as intended
BinToss Mar 28, 2023
8d24878
refactor: move SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX to Windows.Win32
BinToss Mar 28, 2023
6576402
refactor: add exception message
BinToss Mar 28, 2023
8b43267
refactor: rework SafeHandleEx constructor, properties
BinToss Mar 28, 2023
5048cdd
refactor: rework SafeFileHandleEx constructor, properties
BinToss Mar 28, 2023
8b3df17
refactor: rework TryGetFinalPath()
BinToss Mar 28, 2023
91655bc
refactor: finish SafeFileHandleEx.ToString()
BinToss Mar 28, 2023
9cd45c6
refactor: remove redundant FreeHGlobal
BinToss Mar 30, 2023
b110aea
refactor: make NativeMethods non-partial
BinToss Mar 30, 2023
28593c1
refactor: rewrite FindLockingHandles' Discard()
BinToss Apr 2, 2023
64786cc
refactor: remove old DEBUG preprocessor ops
BinToss Apr 2, 2023
48d4d12
refactor: change PS_PROTECTION to public, add enums
BinToss Apr 2, 2023
a92efb1
refactor: return exception to FileHandleType when IsFileHandle.v is n…
BinToss Apr 3, 2023
71ac75a
refactor: convert FileName Get to block body with if-else
BinToss Apr 3, 2023
b86b304
refactor: remove redundant "is (not) default" checks
BinToss Apr 3, 2023
05ac570
refactor: ensure all native GetProcessCommandLine exceptions are wrap…
BinToss Apr 4, 2023
4a6cdf6
chore: split, update FileLockerEx TODO
BinToss Apr 4, 2023
91860f6
docs: update FileLockerEx ctor 'remarks' with Debug, Admin notes
BinToss Apr 4, 2023
5d50cc6
refactor: update HandlesFilter enum with IncludeProtectedProcesses
BinToss Apr 4, 2023
c07b8a1
refactor: remove unused variable 'exceptionData'
BinToss Apr 4, 2023
cc81b4b
refactor: prefix 'safe' GetProcessCommandLine with 'Try'
BinToss Apr 4, 2023
d8870cd
refactor: add return statements to expedite ProcessName Get accessor
BinToss Apr 4, 2023
62f6ca1
refactor: convert ProcessMainModulePath body to expression
BinToss Apr 4, 2023
7bc0972
refactor: simplify reference to NTSTATUS.Code enum
BinToss Apr 4, 2023
06f05dd
refactor: convert HandleObject if-else to switch statement
BinToss Apr 4, 2023
fdb3a77
refactor: add SafeHandleEx.ProcessProtection property
BinToss Apr 4, 2023
0750c40
refactor: convert ProcessIsProtected body to expression
BinToss Apr 4, 2023
ea8e997
refactor: check process protection for command line accessibility
BinToss Apr 4, 2023
a0dd6d6
style: change file encoding to UTF-8 with BOM
BinToss Apr 4, 2023
7831d99
revert: remove try-catch from GetHandleObjectType
BinToss Apr 4, 2023
1e5c1b0
refactor: wrap the entirety of TryGetFinalPath in try-catch
BinToss Apr 4, 2023
a07d7b5
refactor: implement IEquatable for PS_PROTECTION
BinToss Apr 4, 2023
306e842
refactor: change which FileLockerEx ctor is used by FindLockingHandl…
BinToss Apr 9, 2023
d4c3247
refactor: add SafeFileHandleEx.FileNameInfo
BinToss Apr 9, 2023
c749fe3
refactor: remove silly, unused explicit operator from OBJECT_TYPES_IN…
BinToss Apr 9, 2023
d6f3e2c
docs: update docs for some OBJECT_INFORMATION_CLASS enums
BinToss Apr 9, 2023
ea2fb9f
docs: add missing exception doc for GetHandleObjectType()
BinToss Apr 9, 2023
26c04bc
docs: add description for typeparam T of SafeBuffer<T>
BinToss Apr 9, 2023
21d6048
refactor: inline TryGetFullProcessImageName
BinToss Apr 9, 2023
57d0b38
docs: add UnauthorizedAccessException info for GetFullProcessImageName
BinToss Apr 9, 2023
931038e
fix: prevent redundant unsuccessful Get ProcessProtection calls
BinToss Apr 9, 2023
1f84d0c
refactor: add SafeHandle.ObjectName property
BinToss Apr 9, 2023
9b01fe9
refactor: replace HandleObjectType's ProcessIsProtected refs with Pro…
BinToss Apr 9, 2023
02a2afc
docs: add summaries for FileType
BinToss Apr 9, 2023
09a4a4a
refactor: rewrite TryGetFinalPath exception conditions
BinToss Apr 9, 2023
8faaca9
refactor: use UnauthorizedAccessExceptions where applicable
BinToss Apr 9, 2023
419a38b
refactor: add ProcessInfo
BinToss Apr 9, 2023
c887584
refactor: change FindLockingHandles return type to List<ProcessInfo>
BinToss Apr 9, 2023
d8f6c6f
refactor: reduce TryGetFinalPath timeout
BinToss Apr 9, 2023
e44c523
docs: add ObjectName summary, examples of string values
BinToss Apr 9, 2023
a1ced27
fix: change string comparison operator
BinToss Apr 9, 2023
6d8ca88
fix: handle null and zero-length returns for ObjectName
BinToss Apr 9, 2023
2bc267b
refactor: add TryGetFinalPath stopwatch
BinToss Apr 10, 2023
05bf4f7
feat: allow user to specify the how the handle list is sorted
BinToss Apr 11, 2023
5dc8a17
docs: refine FileLockerEx.Path summary
BinToss Apr 11, 2023
2f461e0
feat: add class ProcessQueryHandle to store access rights with handle
BinToss Apr 17, 2023
717bf36
refactor: move process properties, methods to ProcessInfo
BinToss Apr 18, 2023
3ee71ca
refactor: remove instanced process lists from FileLockerEx
BinToss Apr 19, 2023
e7b2019
build: expose CsWin32 API publicly
BinToss Apr 24, 2023
b186662
fix: assign correct field offsets for PROCESS_BASIC_INFORMATION64
BinToss Apr 24, 2023
7df3ebc
build: add CsWin32 symbol IsWow64Process2
BinToss Apr 26, 2023
4b05eeb
refactor: add explicit cast (T*)UIntPtr64<T>
BinToss Apr 26, 2023
2f1b50d
refactor: remove unused usings
BinToss Apr 26, 2023
bcb5a94
docs: add FindLockingHandles remarks, TODO: review keep()
BinToss Apr 26, 2023
3b61f26
refactor: make PROCESS_BASIC_INFORMATION public
BinToss Apr 26, 2023
a600f8f
refactor: add missing managed array properties
BinToss Apr 26, 2023
8249163
refactor: remove PEB_Ex
BinToss Apr 26, 2023
a24601a
refactor: add record struct ProcessAndHostOSArch
BinToss Apr 26, 2023
e2bd6bb
perf: replace enum.HasFlag() with & operator
BinToss Apr 26, 2023
67b48d1
refactor: add wrapper classes for 32-bit, 64-bit native definitions
BinToss Apr 26, 2023
28d3dc5
refactor: add implicit cast of ProcessQueryHandle to SafeProcessHandle
BinToss Apr 26, 2023
3ba04cc
refactor: remove alias of global::PInvoke.NTSTATUS
BinToss Apr 26, 2023
436427e
fix: reference ProcessInfo.ProcessId instead of ProcessInfo.Process.Id
BinToss Apr 26, 2023
12e7bdb
refactor: add properties Is32BitEmulatedProcess, ProcessAndHostOSArch
BinToss Apr 26, 2023
29d8a20
refactor: pass Is32BitEmulateProcess to GetProcessCommandLine
BinToss Apr 26, 2023
acb957b
perf: assign return values to backing field
BinToss Apr 26, 2023
b142b96
refactor: add GetPropertiesViaProcessHandle(), ParentId
BinToss Apr 26, 2023
696e434
refactor: shorten ProcessMainModulePath, ProcessName accessors
BinToss Apr 26, 2023
89aa84f
refractor: remove ProcessInfo.ProcessIsProtected
BinToss Apr 26, 2023
e7ce7d6
refactor: add ParentId to SafeFileHandleEx.ToString()
BinToss Apr 26, 2023
dfeb9e9
fix: SortByProperty.HandleType sorts by HandleObjectType
BinToss Apr 26, 2023
ac118fc
refactor: rename ObjectProperName to ObjectRealName
BinToss Apr 26, 2023
8c740cd
feat: add property safeFileHandleEx.IsFilePathRemote
BinToss Apr 28, 2023
df1c8e0
refactor: make CsWin32 additions public
BinToss Apr 28, 2023
bbac7d1
refactor: remove orphaned isProcessProtected field
BinToss Apr 28, 2023
30bf1ad
refactor: simplify some ProcessInfo properties
BinToss Apr 28, 2023
28b6631
refactor: change ProcessCommandLine to call GetPropertiesViaProcessHa…
BinToss Apr 28, 2023
9e5cf56
refactor: add PS_PROTECTION.GetHashCode() override
BinToss May 2, 2023
f600e61
refactor: make ProcessProtection compare field to (null, null) instea…
BinToss May 2, 2023
9045fa2
fix: allow casting List<ProcessInfo> to/from ProcessList
BinToss May 2, 2023
cc5c5a1
refactor: change Trace.WriteLine calls to Trace.TraceError
BinToss May 2, 2023
61d8c30
refactor: make FileName check ObjectName if FileFullPath is null
BinToss May 2, 2023
b4f8987
refactor: inline TryGetFinalPath
BinToss May 2, 2023
29e198d
fix: update remaining references of FILE_NAME enum to GETFINALPATHNAM…
BinToss May 2, 2023
acd9b15
refactor: copy TryGetFinalPath docs to FileFullPath
BinToss May 2, 2023
7d5a7b8
refactor: remove TryGetFinalPath
BinToss May 2, 2023
82ae66d
refactor: change handleObjectType from private to protected
BinToss May 2, 2023
edbaae2
refactor: override base IsClosed with call to GetHandleInformation
BinToss May 2, 2023
2267f20
refactor: change objectName from private to protected
BinToss May 2, 2023
ea518ac
refactor: allow calling SafeFileHandleEx.ToString without initializin…
BinToss May 2, 2023
fbe991c
refactor: move HANDLE_FLAGS cast closer to target variable
BinToss May 2, 2023
66104ff
refactor: add constants for FileFullPath strings
BinToss May 2, 2023
ee46bfb
refactor: do not normalize FileFullPath when IsFilePathRemote
BinToss May 2, 2023
3431542
refactor: use PInvoke.Win32Exception wrapper in ProcessInfo
BinToss May 2, 2023
d0cadd7
refactor: make get_ProcessHandle assign values to canGetQueryLimitedI…
BinToss May 2, 2023
8102108
refactor: make GetFullProcessImageName an instance method
BinToss May 2, 2023
d037aac
refactor: use SafeBuffer<T> instead of PWSTR
BinToss May 2, 2023
a1f886f
refactor: add const error messages to ProcessHandle
BinToss May 2, 2023
b1ca0d9
refactor: compare fields to const values instead of default
BinToss May 2, 2023
5dd5b91
refactor: move UIntPtr32<T> to new file
BinToss May 3, 2023
5273f06
refactor: sort SafeFileHandleEx properties lexicographically
BinToss May 4, 2023
1c54e02
perf: remove superfluous GetFileInformationByHandleEx task
BinToss May 5, 2023
771e379
fix: replace GetFileInformationByHandleEx buffer param with safeBuffer
BinToss May 5, 2023
f003c8b
refactor: inline temporary variable
BinToss May 5, 2023
0e6c43b
fix: assign, return type when GetFileType succeeds
BinToss May 5, 2023
0f298a9
refactor: rewrite IsDirectory to use GetFileInformationByHandleEx
BinToss May 5, 2023
756d0b7
feat: add HandleAttributes property
BinToss May 5, 2023
95d9eb0
refactor: invert if statement; remove else
BinToss May 5, 2023
beeb6b9
refactor: also check FileNameInfo for FileName processing
BinToss May 6, 2023
a056ce3
refactor: use errFailedMessage for FileName failures
BinToss May 6, 2023
57dc820
feat: add ProcessId property to system handle ex struct; converts fro…
BinToss May 6, 2023
28b576d
feat: finish SortByProperty implementation
BinToss May 6, 2023
e38511c
feat: finish implementing new IsClosed property
BinToss May 7, 2023
4dd2396
docs: document GetHandleInfo(), GetHandleInformation(SafeHandle)
BinToss May 7, 2023
4251f05
feat: add CloseSourceHandle(bool) overload
BinToss May 7, 2023
f5e5c72
docs: add readonly HandleAttributes remarks
BinToss May 7, 2023
4312a64
fix: convert properties to strings for sorting instead of to byte arrays
BinToss May 7, 2023
72fe0a3
fix: do not initialize properties by default when evaluating SafeFile…
BinToss May 7, 2023
bfb841b
refactor: remove FileFullPath stopwatch
BinToss May 7, 2023
7ad0787
chore: sort NativeMethods.txt contents
BinToss May 7, 2023
1ddff98
fix: if buffer too small and returnLength is less than buffer length,…
BinToss May 8, 2023
b4819c2
refactor: use 'is' when comparing to constants
BinToss May 8, 2023
bfbd325
fix: offset FileNameInfo buffer to start of char sequence
BinToss May 8, 2023
36eceb1
fix: convert ex to string, not the ToString method
BinToss May 8, 2023
1cdea26
fix: prevent reallocation loops when buffer larger than returnLength
BinToss May 8, 2023
cbbc524
feat: add NameBuffer placeholder for trailing char array
BinToss May 8, 2023
7b052c5
refactor: realign output of SafeFileHandleEx.ToString()
BinToss May 8, 2023
0087880
fix: subtract FileNameLength size from buffer length
BinToss May 8, 2023
3766142
refactor: remove unnecessary parameter
BinToss May 8, 2023
500c7ec
fix: remove trailing null characters from ProcessMainModulePath
BinToss May 8, 2023
76a9c29
fix: correctly init, read FileNameInfo buffer
BinToss May 8, 2023
abae5f7
docs: add FileNameInfo summary
BinToss May 8, 2023
491df6c
refactor: inline temporary variable
BinToss May 8, 2023
d32b6c8
feat: add handle-returning DuplicateHandle overload
BinToss May 11, 2023
007e96e
refactor: define Handle-void* conversion
BinToss May 11, 2023
7c6ead9
refactor: add overload SysHandleEx.GetHandleInfo(SafeProcessHandle)
BinToss May 11, 2023
672cf78
feat: add property DuplicateHandle
BinToss May 11, 2023
b32aa35
refactor: rename ProcessQueryHandle to SafeProcessHandleEx
BinToss May 12, 2023
a5c665e
refactor: aggregate granted process access rights to open the SafePro…
BinToss May 12, 2023
b158c42
refactor: replace ProcessHandle's consecutive try-catch statements wi…
BinToss May 12, 2023
886dbb8
fix: duplicate source handle when not owned by current process
BinToss May 16, 2023
0375677
docs: update ProcessInfo.ProcessHandle exceptions
BinToss May 16, 2023
0ec0c11
fix: finish IsClosed reference refactors
BinToss May 16, 2023
0349dd0
fix: use ProcessInfo's ProcessId property, NOT the field
BinToss May 16, 2023
192d36d
refactor: change ProcessBasicInformation's buffer reallocation implem…
BinToss May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 222 additions & 6 deletions deadlock-dotnet-sdk/DeadLock.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics;
using System.ComponentModel;
using System.Diagnostics;
using deadlock_dotnet_sdk.Domain;
using HandlesFilter = deadlock_dotnet_sdk.Domain.FileLockerEx.HandlesFilter;

namespace deadlock_dotnet_sdk
{
Expand All @@ -12,7 +14,7 @@ public class DeadLock
/// </summary>
public bool RethrowExceptions { get; set; }

#endregion
#endregion Properties

/// <summary>
/// Default constructor
Expand All @@ -31,16 +33,17 @@ public DeadLock(bool rethrowExceptions)
RethrowExceptions = rethrowExceptions;
}

#region ProcessLocks

/// <summary>
/// Retrieve the FileLocker object that contains a List of Process objects that are locking a file
/// </summary>
/// <param name="filePath">The full path of a file</param>
/// <returns>The FileLocker object that contains a List of Process objects that are locking a file</returns>
public FileLocker FindLockingProcesses(string filePath)
{
FileLocker fileLocker = new(filePath,
return new(filePath,
NativeMethods.FindLockingProcesses(filePath, RethrowExceptions).ToList());
return fileLocker;
}

/// <summary>
Expand Down Expand Up @@ -113,7 +116,7 @@ public void Unlock(FileLocker fileLocker)
}

/// <summary>
/// Unlock one or more files by killing all the processes that are holding a handle on the files
/// Unlock one or more files by killing all the processes that are holding a handle on the files
/// </summary>
/// <param name="fileLockers">The FileLocker objects that contain the List of Process objects that are locking a file</param>
public void Unlock(params FileLocker[] fileLockers)
Expand Down Expand Up @@ -142,7 +145,7 @@ await Task.Run(() =>
}

/// <summary>
/// Unlock one or more files asynchronously by killing all the processes that are holding a handle on the files
/// Unlock one or more files asynchronously by killing all the processes that are holding a handle on the files
/// </summary>
/// <param name="fileLockers">The FileLocker objects that contain the List of Process objects that are locking a file</param>
public async Task UnlockAsync(params FileLocker[] fileLockers)
Expand Down Expand Up @@ -207,5 +210,218 @@ public async Task UnlockAsync(params string[] filePaths)
await UnlockAsync(f);
}
}

#endregion ProcessLocks

#region HandleLocks

/// <summary>
/// Retrieve the <see cref="FileLockerEx"/> object that contains a List of handles that are locking a file.
/// </summary>
/// <param name="filePath">The full or partial path of a file or directory.</param>
/// <param name="filter">By default, only handles whose object's Type is confirmed to "File" are returned. Optionally, handles for data pipes, printers, and other Types can be included, in addition to handles whose object Type could not be identified for some reason.</param>
/// <returns>The <see cref="FileLockerEx"/> object that contains the <paramref name="filePath"/> and a list of handles matching the <paramref name="filter"/>.</returns>
public FileLockerEx FindLockingHandles(string filePath, HandlesFilter filter, out WarningException? warningException)
{
warningException = null;
try
{
return new(filePath, filter, RethrowExceptions, out warningException);
}
catch (UnauthorizedAccessException) when (!RethrowExceptions)
{ return new(); }
}

/// <summary>
/// Retrieve the List of <see cref="FileLockerEx"/> objects for one or multiple files and/or directories
/// </summary>
/// <param name="filter">By default, only handles whose object's Type is confirmed to "File" are returned. Optionally, handles for data pipes, printers, and other Types can be included, in addition to handles whose object Type could not be identified for some reason.</param>
/// <returns>The List of <see cref="FileLockerEx"/> objects that contains a List of handles that are locking one or multiple files and/or directories</returns>
public List<FileLockerEx> FindLockingHandles(HandlesFilter filter, ref List<WarningException> warnings, params string[] filePaths)
{
List<FileLockerEx> fileLockers = new();
warnings = new();

if (filePaths.Length is 1)
{
fileLockers.Add(FindLockingHandles(filePaths[0], filter, out WarningException? warningException));
if (warningException != null) warnings.Add(warningException);
}
else
{
foreach (string filePath in filePaths)
{
fileLockers.Add(FindLockingHandles(filePath, filter, out WarningException? warningException));
if (warningException != null) warnings.Add(warningException);
}
}
return fileLockers;
}

/// <summary>
/// Asynchronously retrieve the <see cref="FileLockerEx"/> object that contains a List of handles that are locking a file or directory
/// </summary>
/// <param name="filePath">The full or partial path of a file</param>
/// <param name="filter">By default, only handles whose object's Type is confirmed to "File" are returned. Optionally, handles for data pipes, printers, and other Types can be included, in addition to handles whose object Type could not be identified for some reason.</param>
/// <returns>The <see cref="FileLockerEx"/> object that contains a List of handles that are locking a file or directory</returns>
public static async Task<FileLockerEx> FindLockingHandlesAsync(string filePath, HandlesFilter filter = HandlesFilter.FilesOnly)
{
FileLockerEx fileLocker = new();

await Task.Run(() => fileLocker = new(filePath, filter, false, out _));

return fileLocker;
}

/// <summary>
/// Asynchronously retrieve the List of <see cref="FileLockerEx"/> objects for one or multiple files and/or directories
/// </summary>
/// <param name="filePaths">The full or partial paths of files and/or directories </param>
/// <returns>The List of <see cref="FileLockerEx"/> objects that contain the handles that are locking a file or directory</returns>
/// <remarks>To monitor for a WarningException–an exception signifying reduced functionality–add a trace listener via System.Diagnostics.Trace.Listeners.Add(TraceListener)</remarks>
public static async Task<List<FileLockerEx>> FindLockingHandlesAsync(HandlesFilter filter = HandlesFilter.FilesOnly, params string[] filePaths)
{
List<FileLockerEx> fileLockers = new();

await Task.Run(() =>
{
foreach (string filePath in filePaths)
{
fileLockers.Add(new FileLockerEx(filePath, filter, false, out var warning));
if (warning is not null)
Trace.TraceWarning(warning.ToString());
}
});

return fileLockers;
}

/// <summary>
/// Unlock a File or Directory by leveraging undocumented kernel functions to make all processes release their handles of the file
/// Release the system handle.
/// ! WARNING !
/// ! If a handle or a duplicate of a handle is in use by a driver or other kernel-level software, a function that accesses the now-invalid handle can cause a stopcode (AKA Blue Screen Of Death).<br/>
/// ! Be very wary of potentially destabilizing your or your end-user's system!<br/>
/// ! Even more so if you used the <see cref="HandlesFilter.IncludeFailedTypeQuery"/> or <see cref="HandlesFilter.IncludeNonFiles"/> filter flags
/// </summary>
/// <param name="fileLocker">The <see cref="FileLockerEx"/> that contains the List of handles that should be released</param>
public void UnlockEx(FileLockerEx fileLocker)
{
foreach (SafeFileHandleEx h in fileLocker.Lockers)
{
if (h.IsClosed.v is true && h.IsInvalid) continue;
try
{
h.CloseSourceHandle(true);
}
catch (Exception) when (!RethrowExceptions) { }
}
}

/// <summary>
/// Unlock one or more files or directories by directing each handle's owner process to release the handle
/// ! WARNING !
/// ! If a handle or a duplicate of a handle is in use by a driver or other kernel-level software, a function that accesses the now-invalid handle can cause a stopcode (AKA Blue Screen Of Death).<br/>
/// ! Be very wary of potentially destabilizing your or your end-user's system!<br/>
/// ! Even more so if you used the <see cref="HandlesFilter.IncludeFailedTypeQuery"/> or <see cref="HandlesFilter.IncludeNonFiles"/> filter flags
/// </summary>
/// <param name="fileLockers">The <see cref="FileLockerEx"/> objects that contain the List of handles that are locking a file or directory</param>
public void UnlockEx(params FileLockerEx[] fileLockers)
{
foreach (FileLockerEx fileLocker in fileLockers)
{
UnlockEx(fileLocker);
}
}

/// <summary>
/// Unlock a File or Directory asynchronously by directing each handle's owner process to release the handle
/// </summary>
/// <param name="fileLocker">The <see cref="FileLockerEx"/> that contains the List of handles that should be released</param>
public async Task UnlockExAsync(FileLockerEx fileLocker)
{
await Task.Run(() =>
{
foreach (SafeFileHandleEx h in fileLocker.Lockers)
{
if (h.IsClosed.v is true && h.IsInvalid) continue;
try
{
h.CloseSourceHandle(true);
}
catch (Exception) when (!RethrowExceptions) { }
}
});
}

/// <summary>
/// Unlock one or more files/directories asynchronously by directing each handle's owner process to release the relevant handles
/// </summary>
/// <param name="fileLockers">The <see cref="FileLockerEx"/> objects that contain the List of handles that are locking a file/directory</param>
public async Task UnlockExAsync(params FileLockerEx[] fileLockers)
{
await Task.Run(() =>
{
foreach (FileLockerEx fileLocker in fileLockers)
{
foreach (SafeFileHandleEx h in fileLocker.Lockers)
{
if (h.IsClosed.v is true && h.IsInvalid) continue;
try
{
h.CloseSourceHandle(true);
}
catch (Exception) when (!RethrowExceptions) { }
}
}
});
}

/// <summary>
/// Unlock a file/directory without retrieving the List of FileLockerEx objects
/// </summary>
/// <param name="filePath">The path of the file/directory that should be unlocked</param>
public void UnlockEx(string filePath)
{
FileLockerEx fileLocker = FindLockingHandles(filePath, HandlesFilter.FilesOnly, out _);
UnlockEx(fileLocker);
}

/// <summary>
/// Unlock a file/directory without retrieving the List of FileLockerEx objects asynchronously
/// </summary>
/// <param name="filePath">The path of the file/directory that should be unlocked</param>
public async Task UnlockExAsync(string filePath)
{
FileLockerEx locker = await FindLockingHandlesAsync(filePath);
await UnlockExAsync(locker);
}

/// <summary>
/// Unlock one or more files/directories without retrieving the List of FileLockerEx objects
/// </summary>
/// <param name="filePaths">The full or partial paths of the files/directories that should be unlocked</param>
public void UnlockEx(params string[] filePaths)
{
foreach (FileLocker fileLocker in FindLockingProcesses(filePaths))
{
Unlock(fileLocker);
}
}

/// <summary>
/// Unlock one or more files/directories without retrieving the List of FileLockerEx objects asynchronously
/// </summary>
/// <param name="filePaths">The full or partial paths of the files/directories that should be unlocked</param>
public async Task UnlockExAsync(params string[] filePaths)
{
List<FileLockerEx> fileLockers = await FindLockingHandlesAsync(HandlesFilter.FilesOnly, filePaths);
foreach (FileLockerEx f in fileLockers)
{
await UnlockExAsync(f);
}
}

#endregion HandleLocks
}
}
Loading