-1

When my plugin DLL is loaded into the host application's address space, I need to write some data into a file. The problem is that at that point of time this file is already opened by the host process with exclusive write access, and my call to CreateFile fails with ERROR_SHARING_VIOLATION

I wonder, can I somehow obtain the file handle from the host process using WinAPI calls? This looks like a terrible idea, but I really need to write into that file. Does someone have any good ideas about how to resolve this problem?

Gart
  • 2,297
  • 1
  • 28
  • 36
  • 1
    Is it a file that users of the host application can close and then reopen after your plugin has done its work? What sort of application is the host, anyway? It's possible that the host application provides methods for plugins to write to files opened by the host, depending on what the host application is. – JAB Jun 27 '12 at 15:08
  • @JAB: The application is a very specific image viewer, and the file I'm trying to open is a text file with the image metadata. It comes along with the image. Unfortunately, the host app doesn't provide an API to modify this file. – Gart Jun 27 '12 at 15:16
  • 1
    Even if you can get the handle, it is pretty much guaranteed to break/confuse the host process. Data won't be wehere it expects it, the current file positions will be wrong among other things. – Deanna Jun 27 '12 at 16:06
  • @Deanna: Yes, but I can restore the state of the handle after I use it. The application is single threaded and I am sure nothing else will be accessing the file while my code is running – Gart Jun 27 '12 at 18:19
  • 2
    "I am sure nothing else will be accessing the file while my code is running." I have a feeling the writers of the host application thought the same thing when they made it have exclusive write access, _because_ they used exclusive access. Unless they just didn't know what they were doing, which is always possible. – JAB Jun 27 '12 at 19:03

1 Answers1

1

Open handles can be enumerated via NtQueryInformationProcess and/or NtQuerySystemInformation, but it isn't officially supported (you can find non-Microsoft documentation readily with Google) and may not work in future versions of Windows.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720