2016-11-06 23 views
1

我想知道的是有可能尝试打开一个文件(以及何时失败,因为它打开与另一个进程共享关闭)找出哪个进程正在使用该文件?C++ | Windows - 有没有办法找出哪个进程拥有锁定文件的所有权?

我想知道这些信息的原因是因为我正在制作一个小程序来“修复”恶意文件。

例如,一些恶意/广告软件等设置文件安全描述符,以便用户不能删除文件等。我的应用程序只是重置安全描述符,允许用户重新获得控制权。

我也看到一个文件打开它的子​​进程,例如(CreateFile)并关闭共享模式,所以文件不能被触及,那么应用程序会从内存中执行子进程。

回答

2

是的,你可以在一般情况下只使用openfiles命令,通过启用收集此信息后,它看起来,openfiles /local on

在Windows NT直至并包括(似乎)Windows XP中有一个名叫oh类似的资源工具包命令,简称打开的句柄

两者的替代方法是使用SysInternal的Process Explorer


注意:在某些情况下,openfiles将无法​​列出某些句柄。当Windows拒绝卸载USB磁盘时,这发生在我身上,声称某个进程正在使用该磁盘上的文件。没有这样的过程出现过。

+0

大概'openfiles'排除内核模式处理,如防病毒软件可能无意中平仓离场。 –

-1

我开发了一个函数来定位这样的进程,杀死它并删除锁定的文件。

bool ForceDeleteFile(LPWSTR FileName); 

下面是完整的源代码:

bool KillFileProcess(LPWSTR FileName) 
{ 
    HANDLE hProcessSnap; 
    HANDLE hProcess; 
    PROCESSENTRY32 pe32; 
    DWORD dwPriorityClass; 
    bool result = false; 
    // Take a snapshot of all processes in the system. 
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    if (hProcessSnap == INVALID_HANDLE_VALUE) 
    { 
     //printError(TEXT("CreateToolhelp32Snapshot (of processes)")); 
     return(FALSE); 
    } 

    // Set the size of the structure before using it. 
    pe32.dwSize = sizeof(PROCESSENTRY32); 

    // Retrieve information about the first process, 
    // and exit if unsuccessful 
    if (!Process32First(hProcessSnap, &pe32)) 
    { 
     //printError(TEXT("Process32First")); // show cause of failure 
     CloseHandle(hProcessSnap);   // clean the snapshot object 
     return(FALSE); 
    } 

    // Now walk the snapshot of processes, and 
    // display information about each process in turn 
    do 
    { 
     // Retrieve the priority class. 
     dwPriorityClass = 0; 
     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); 
     if (hProcess == NULL) 
     { 
      //printError(TEXT("OpenProcess")); 
     } 
     else 
     { 
      dwPriorityClass = GetPriorityClass(hProcess); 
      if (!dwPriorityClass) 
      { 
       //printError(TEXT("GetPriorityClass")); 
      } 
      CloseHandle(hProcess); 
      if (HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID)) 
      { 
       WCHAR filename[MAX_PATH] = {}; 

       if (GetModuleFileNameEx(hProcess, NULL, filename, MAX_PATH)) 
       { 
        if (_wcsicmp((const wchar_t *)FileName, (const wchar_t *)filename) == NULL) 
        { 
         if (TerminateProcess(pe32.th32ProcessID, 0)) 
         { 
          _tprintf(L"Found: Process full killed\nKILLED!\n"); 
          result = true; 

         } 
         else 
         { 
          _tprintf(L"Found: Process full \nFailed to terminate\n"); 
          DoRun(((CString)L"taskkill /F /IM " + (CString)pe32.szExeFile).GetBuffer()); 

          result = false; 

         } 
        } 
       } 
       else 
       { 
        // handle error 
       } 

       CloseHandle(hProcess); 
      } 
     } 


    } while (Process32Next(hProcessSnap, &pe32)); 

    CloseHandle(hProcessSnap); 
    return(result); 
} 
bool ForceDeleteFile(LPWSTR FileName) 
{ 
    bool result = DeleteFile(FileName); 
    if (!result) 
    { 
     _tprintf(L"Can't delete file. using DeleteFile(). Trying to locate process and kill it\n"); 
     result = KillFileProcess(FileName); 
     if (!result) 
      _tprintf(L"Couldn't find the process\n"); 
     else 
     { 
      Sleep(1000); 
      result = DeleteFile(FileName); 
      if (result) 
       _tprintf(L"DeleteFile success"); 
      else 
       _tprintf(L"DeleteFile ============== failed ==============="); 

     } 
    } 
    return result; 
} 
BOOL TerminateProcess(DWORD dwProcessId, UINT uExitCode) 
{ 
    DWORD dwDesiredAccess = PROCESS_TERMINATE; 
    BOOL bInheritHandle = FALSE; 
    HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); 
    if (hProcess == NULL) 
     return FALSE; 

    BOOL result = TerminateProcess(hProcess, uExitCode); 

    CloseHandle(hProcess); 

    return result; 
} 
+0

我不是downvoter,但这似乎局限于锁定文件是可执行文件(即正在运行)的情况? –

+0

是的,作为一个事实,但同样的方法可以用于任何锁定的文件。 –

相关问题