CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Holiday Gift Ideas
Promos and Premiums
Build a Server Rack
Web Design
Car Donations
Find Software
Televisions
Remote Online Backup
Shop Online
Computer Deals
Promote Your Website
Best Price
PDA Phones & Cases
Corporate Gifts


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> Windows Programming >> File & Folder >> File I/O

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Deleting Locked Files
Rating:

Zoltan Csizmadia (view profile)
November 14, 2000

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. (continued)



    Download these IBM resources today!
    e-Kit: IBM Rational Systems Development Solution
    With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

    Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
    Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

    eKit: Rational Build Forge Express
    Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

    Download: IBM Data Studio v1.1
    Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

    eKit: Rational Asset Manager
    Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.

    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

  • ForceDel.zip -

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed

    Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
    Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
    Five Trends for Application Development & Program Management. Download Complimentary Report Now.
    Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
    Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.


  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    Couldn't delete. Error = 5 - glenneroo (02/18/2007)
    Thanks Zoltan! - brian langer (02/28/2006)
    How to open a process ?? - Kamal El Majdouli (01/09/2005)
    Couldn't delete. Error = 123 - Alain Lauzon (07/01/2004)
    You are Life Saver !!! - Legacy CodeGuru (12/30/2003)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES