Nadeem Ullah
Nadeem Ullah

Reputation: 927

Closing a process child window (e.g. a Word window) accessing a specific file

Scenario (for force deleting a file, can't go for delete on restart):

  1. I have a file that is opened in some program, say M.S. Word. There could be some other files as well opened in M.S. Word.
  2. In my C# program, I need to know what process is accessing that particular file and need to close/release only that particular file from Word. Other files should not be disturbed, so I won't use Process.Kill() method.

Given the above scenario, I have done the following:

  1. Found the process (instance of Process class) accessing my target file using the code here

  2. Found a list of handles held by that process (using NtQuerySystemInformation() method).

  3. From the list of handles, I was able to find the particular handle holding my file (using NtQueryObject() method). Tried to call CloseHandle() on this handle but that does not work.
  4. Now I can also list down all child window handles from my target process.

How do I find that particular window handle that is related to the file handle I found in step 3 above? My plan is to use SendMessage() method with WM_CLOSE message to that particular window so it would be closed and my file would be released.

One way is to check the file name string in that child window's title and then close that window. But the issue is, a file with the same name could have been opened from some different location.

Upvotes: 0

Views: 205

Answers (1)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10862

I found this very detail post about the subject, maybe it can help you

http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html

That post hase some disucussions and related questions here

http://forum.sysinternals.com/topic19403.html

Upvotes: 0

Related Questions