Reputation: 4835
I'm looking for a way to obtain a HANDLE
which is compatible with ReadFile
, WriteFile
, and SetFilePointer
, but instead of going to a file in the file system, or [virtual] memory, I want the operations to be handled by user-supplied callbacks, like so (for example):
HANDLE CreateFakeFile(
DWORD (*lpRead)(LPVOID, DWORD, LPVOID),
DWORD (*lpWrite)(LPVOID, DWORD, LPVOID),
LARGE_INTEGER (*lpSeek)(LARGE_INTEGER, DWORD, LPVOID)
)
The supplied callbacks would then have to be called when the handle is used in file APIs.
Background:
I have data which can (at least in large parts) be procedurally generated, but the API that processes the data expects a file HANDLE
. This cannot easily be changed. However, only a small portion of the data is needed at any given time (I do not know its extents in advance), and the total amount of data is large enough that it is not feasible to pre-generate the whole thing as an in-memory file first, since it would require me to calculate the entire possible data range, even though only some small portions of it will be accessed.
Essentially, I need a fake file stream at the Win32 API level, something that could have been done in C# for example by inheriting from System.IO.Stream
. My supplied user callbacks would then implement the generative algorithm as needed.
I suppose it could be possible with something like a custom file system driver, however, that would go way beyond the scope for this project. Not the least because I don't want to require administrator privileges.
Upvotes: 3
Views: 84
Reputation: 28872
What will probably work for you is Named pipes. It is like a file that you can feed and receive data through the other end. https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipes
Upvotes: 0