Skip to content

Forcefully unlock files locked by other processes #232

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

Open
Tracked by #287
BinToss opened this issue Nov 15, 2021 · 2 comments
Open
Tracked by #287

Forcefully unlock files locked by other processes #232

BinToss opened this issue Nov 15, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request p:HIGH

Comments

@BinToss
Copy link
Member

BinToss commented Nov 15, 2021

Other processes are locking haloce.exe to write to it, which prevents us from writing our binary patches.

Priority Solution
HIGH Cancel "Load Game" and present user with access error. Use Windows' shutdown files API thing (see Deadlock) to list blocking processes.
Medium Embed SysInternals' Handles/Handles64 to yank and break handles. Alternatively, System Informer, but it's larger.
low Finish Deadlock Native Handles pull request. Integrate into HXE. Invocations will be a PITA. CIL Exports?

Propositions

A. Write to copy of haloce.exe, leaving the locked file as-is (mostly)

  1. Rename the directory entry of the file to haloce.exe.0.bak. This won't touch the file itself. Instead, we change filesystem information about the file. Confusing, I know. System.IO.FileInfo?
  2. Copy "haloce.exe.0.bak" to "haloce.exe" as new file in the same directory.
  3. Resume normal "haloce.exe" activities.
    B. Close file handles with filename 'haloce.exe'
  4. Use SysInternal's Handles app/lib to get a list of processes' File handles of filename "haloce.exe". See Handles feature of System Informer.
  5. Close each handle. Suitable API in System.Management

Examples

  • IObit Unlocker Heh. No way~!
  • Process Hacker System Informer
  • SysInternals Handles, Handles64, ProcessExplorer

Biiig dumpy

Use System.IO.File.Open() to check if file is locked (share: none)

https://www.cloudsavvyit.com/2956/how-to-manage-open-file-handles-with-powershell#testing-for-locked-files
https://www.cloudsavvyit.com/2956/how-to-manage-open-file-handles-with-powershell#openfiles-utility
https://www.cloudsavvyit.com/2956/how-to-manage-open-file-handles-with-powershell#the-handle-application

https://www.majorgeeks.com/files/details/unlocker.html
https://docs.microsoft.com/en-us/sysinternals/downloads/handle
https://lockhunter.com/download.htm

https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.unlock?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.handle?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.file.openhandle?view=net-6.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.fileshare?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.file?view=net-5.0
https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.filesystem.unlock?view=net-5.0
https://devblogs.microsoft.com/dotnet/file-io-improvements-in-dotnet-6
https://stackoverflow.com/questions/2225733/how-to-forcefully-unlock-a-file-in-c
https://stackoverflow.com/questions/47977927/error-the-process-cannot-access-the-file-because-it-is-being-used-by-another-pro
https://dotnet-assembly.blogspot.com/2012/10/c-check-file-is-being-used-by-another.html
https://www.codeproject.com/questions/141068/file-is-being-used-by-another-process

@BinToss BinToss changed the title feat: forcefully unlock files locked by other processes fix: forcefully unlock files locked by other processes Nov 15, 2021
@BinToss BinToss self-assigned this Nov 15, 2021
@BinToss BinToss mentioned this issue Mar 6, 2022
3 tasks
@BinToss BinToss added p:HIGH enhancement New feature or request labels Mar 18, 2022
@BinToss BinToss changed the title fix: forcefully unlock files locked by other processes Forcefully unlock files locked by other processes Mar 18, 2022
@BinToss
Copy link
Member Author

BinToss commented Aug 9, 2022

https://www.nirsoft.net/utils/opened_files_view.html
We could just add this as an embedded resource to be extracted and run and a command line util when needed.

BUT I already said I would contribute to CodeDead/deadlock-dotnet-sdk

@BinToss BinToss pinned this issue Aug 9, 2022
@BinToss
Copy link
Member Author

BinToss commented Aug 6, 2024

Scratch that NirSoft suggestion. Use Microsoft SysInternals' Handles/Handles64.

  1. OpenedFilesView does not have an EV-signed driver. It will not work under Secure Boot.
  2. Handles is better >:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p:HIGH
Projects
None yet
Development

No branches or pull requests

1 participant