Environment: Windows NT4 SP6, Windows 2000
This is a command line utility to close and delete a file which is locked
by another process. I doesn't work with modules.
Usage
FORCEDEL.EXE [/S] filename
/S Soft delete. Like the "del" command
filename File name you want to delete
How does it work?
- Query the used file handles (system wide), and search for the processes
which are using the file we want to delete.
(continued)
For more information, check out the CodeGuru article entitled,
Examine Information on Windows NT System Level Primitives.
-
Start a remote thread (CreateRemoteThread) to close the given handle in
every found process (#1)
Code
The following code closes a handle in a remote process. The handle must be
remote process specific.
DWORD CloseRemoteHandle( DWORD processID, HANDLE handle )
{
HANDLE ht = 0;
DWORD rc = 0;
_tprintf( _T("Closing handle in process #%d ... "),
processID );
HANDLE hProcess = OpenProcess( PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
| PROCESS_VM_WRITE
| PROCESS_VM_READ,
FALSE, processID );
if ( hProcess == NULL )
{
rc = GetLastError();
_tprintf( _T("OpenProcess() failed\n") );
return rc;
}
HMODULE hKernel32 = LoadLibrary( _T("kernel32.dll") );
ht = CreateRemoteThread(
hProcess,
0,
0,
(DWORD(__stdcall *)(void*))GetProcAddress(hKernel32,"CloseHandle"),
handle,
0,
&rc );
if ( ht == NULL )
{
rc = GetLastError();
_tprintf( _T("CreateRemoteThread() failed\n") );
goto cleanup;
}
switch ( WaitForSingleObject( ht, 2000 ) )
{
case WAIT_OBJECT_0:
rc = 0;
_tprintf( _T("Ok\n"), rc );
break;
default:
rc = GetLastError();
_tprintf( _T("WaitForSingleObject() failed\n") );
goto cleanup;
break;
}
cleanup:
CloseHandle( ht );
if ( hKernel32 != NULL)
FreeLibrary( hKernel32 );
CloseHandle( hProcess );
return rc;
}
Downloads
ForceDel.zip -