删除被锁定的文件(类似Unlocker的方法)

个人论坛:http://www.devhk007.com

 

删除被锁定的文件(类似Unlocker的方法)

Deleting Locked Files
   Zoltan Csizmadia


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.
复制内容到剪贴板
代码:
/*
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值