dcsimg

WEBINAR:
On-Demand

Application Modernization: What Is It and How to Get Started


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?

  1. Query the used file handles (system wide), and search for the processes which are using the file we want to delete.
  2. For more information, check out the CodeGuru article entitled, Examine Information on Windows NT System Level Primitives.

  3. 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.
/*
Note: 1. SE_DEBUG privilege must be enabled. 
      2. The function works with every kind of HANDLE
      3. It will bother the remote process :)
      4. The handles will be invalid after you closed 
         them remotely
*/

//Close a handle in a remote process
DWORD CloseRemoteHandle( DWORD processID, HANDLE handle )
{
 HANDLE ht = 0;
 DWORD rc = 0;
	
 _tprintf( _T("Closing handle in process #%d ... "), 
          processID );

 // open the process
 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;
 }

 // load kernel32.dll
 HMODULE hKernel32 = LoadLibrary( _T("kernel32.dll") );

 // CreateRemoteThread()
 ht = CreateRemoteThread( 
  hProcess, 
  0, 
  0, 
  (DWORD(__stdcall *)(void*))GetProcAddress(hKernel32,"CloseHandle"),
  handle, 
  0, 
  &rc );
	
 if ( ht == NULL )
 {
  //Something is wrong with the privileges, 
  //or the process doesn't like us
  rc = GetLastError();
  _tprintf( _T("CreateRemoteThread() failed\n") );
  goto cleanup;
 }

 switch ( WaitForSingleObject( ht, 2000 ) )
 {
  case WAIT_OBJECT_0:
   //Well done
   rc = 0;
   _tprintf( _T("Ok\n"), rc );
  break;
	
  default:
   //Oooops, shouldn't be here
   rc = GetLastError();
   _tprintf( _T("WaitForSingleObject() failed\n") );
   goto cleanup;
  break;
 }

 cleanup:
 //Closes the remote thread handle
 CloseHandle( ht );

 //Free up the kernel32.dll
 if ( hKernel32 != NULL)
  FreeLibrary( hKernel32 );

 //Close the process handle
 CloseHandle( hProcess );
	
 return rc;
}


Downloads

Comments

  • File not found ?

    Posted by james on 06/27/2012 08:25am

    hallo, if I want to build the downloaded project there is missed the "SystemInfo.h"-file. Is this a class built by you or is this a WIN-SDK-File ? best regards james

    Reply
  • delete file

    Posted by KenRayn on 02/14/2012 02:35am

    well you can use a program for this like LongPathTool.com maybe ? :)

    Reply
  • Missing Files

    Posted by pislas on 09/11/2008 10:33pm

    Some of the files are missing from this download...cant make it compile, and it doesnt delete my in use files when i run it on them... ForceDel.exe "testfile.txt" SystemProcessInformation::Refresh() failed. Couldn't delete. Error = 32

    Reply
  • Couldn't delete. Error = 5

    Posted by glenneroo on 02/18/2007 10:15am

    SystemProcessInformation::Refresh() failed. Couldn't delete. Error = 5 when deleting this odd file that's from my old windows. It's the only file left before i can reformat this drive, but Vista won't let me :( D:\WINDOWS\system32\Macromed\Flash\Flash9.ocx any ideas?

    Reply
  • Thanks Zoltan!

    Posted by acix on 02/28/2006 05:09pm

    this is exactly what I was looking for. Thank you very much!

    Reply
  • How to open a process ??

    Posted by elmajdouli on 01/09/2005 06:46pm

    In case our programme is running in simple user environnement. (A user wich don't have any power)

    Reply
  • Couldn't delete. Error = 123

    Posted by alauzon on 07/01/2004 04:35pm

    Th efile I want to save is an e-mule preview avi file. I rebooted and I am still not able to delete. I am on Windows XP last version. Couldn't delete. Error = 123

    Reply
  • You are Life Saver !!!

    Posted by Legacy on 12/30/2003 08:00am

    Originally posted by: BiGBoY

    AWESOME!!!! What more can i say?!

    Reply
  • Couldn't delete. Error = 32

    Posted by Legacy on 11/24/2003 08:00am

    Originally posted by: Tony Lau

    It works for me for many cases, but I got the following error message:

    SystemProcessInformation::Refresh() failed.
    Couldn't delete. Error = 32

    from my server running Windows 2003:

    OS Name Microsoft(R) Windows(R) Server 2003, Enterprise Edition
    Version 5.2.3790 Build 3790

    Any clues?

    • publish the exe with changes

      Posted by Dhino on 10/09/2007 10:15am

      do u have a compiled exe with the changes above effected? if yes, pls publish.

      Reply
    • My answer

      Posted by Feghoot on 06/05/2006 08:28pm

      I don't know if this is your problem, but my problem was that in SystemInfo.cpp he was only allocating 64K for the system process information, and it apparently wasn't enough. I changed BufferSize in the header file to 1M (0x100000), changed his allocation and free code to use malloc and free, and it worked. I also took out the EnableDebugPriv call because it seemed to leave mdm.exe pegging my processor, and added a call ::SetFileAttributes (lpFileName, FILE_ATTRIBUTE_NORMAL) before the call to ::DeleteFile, to handle read-only files.

      Reply
    • answer?

      Posted by fquenzer on 05/17/2006 11:50pm

      Did anyone by any chance have an answer to this question? I am getting this as well.

      Reply
    • ERROR CODE 32 in the Destructor code

      Posted by judson_vc on 10/23/2005 02:20pm

      hello ,i too got a same error. what i have to do for the ERROR =32. just i wrotea code that in a destructor { filename.close(); delete(filename); delete bmp; } in that code when i debug,the cursor goes but the GetLastError displays eror code=32 why so pls do reply to me folks

      Reply
    Reply
  • Worked after deactivating file index feature of XP

    Posted by Legacy on 10/15/2003 07:00am

    Originally posted by: Simon Smith

    I had to deactivate the file index feature of XP (in the options of the file), then it worked.


    Reply
  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date