Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

c++ - Force close file by its path on Windows

I'm writing a temporary file manager for other developers. I want to remove files even our console applications crashes or being closed by "X" button.

So far I found std::set_terminate, std::atexit and SetConsoleCtrlHandler methods with which I can delete all temporary files I need. Problem is - I can't delete opened files. Furthermore - I cant control streams to those files, cause developers using several libraries (GDAL for example) which uses their own stream mechanisms and can accept only a target file path.

How can I force close and delete all files, opened by current application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to close the file handles owned by your current process. To do this:

  • Use the NtQuerySystemInformation API with undocumented SystemHandleInformation parameter.
  • This gives you an array of all handles open in the system
  • Iterate over array and select only ones which match your process PID, and are file handles
  • You can then further narrow it down using GetFinalPathNameByHandle to get paths of opened files e.g. you could select specific file names or all files with tmp in their name.
  • For any files you want to delete, call CloseHandle() to force close the handle, then of course DeleteFile() on the path.

Some code (without any error checking):

SYSTEM_HANDLE_INFORMATION* pInfo=NULL;
DWORD dwSize=0;
NTSTATUS status=0;

do
{
  // keep reallocing until buffer is big enough
  status = NtQuerySystemInformation(SystemHandleInformation, pInfo, dwSize, &dwSize);
  if (status==STATUS_INFO_LENGTH_MISMATCH)
     pInfo = (SYSTEM_HANDLE_INFORMATION*)realloc(pInfo, dwSize);
} while(status!=0);

// iterate over every handle
for (DWORD i=0; i<pInfo->dwCount; i++)
{
  if (pInfo->handles[i].ProcessID==GetCurrentProcessId() && pInfo->handles[i].HandleType==28)
  {
     TCHAR szPath[MAX_PATH];
     GetFinalPathNameByHandle((HANDLE)pInfo->handles[i].HandleNumber, szPath, MAX_PATH, 0);
     if (_tcsstr(szFilePath, L"filename_I_want_to_delete"))
     {
       CloseHandle((HANDLE)pInfo->handles[i].HandleNumber);
       DeleteFile(szPath);
     }
  }
}

This is assuming all the files you need to delete are owned by the process doing the deletion. If any of the files belong to another process you will need an extra step using DuplicateHandle() with the DUPLICATE_CLOSE_SOURCE option. Assuming you have suitable permissions this gives you the handle, which you can then close and delete the file as before.

There is some good sample code here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...