问题描述
我想模拟一个文件而不将它写在磁盘上.我的可执行文件末尾有一个文件,我想提供它的 dll 路径.当然,因为它没有真正的路径,所以我必须伪造它.
I'd like to simulate a file without writing it on disk. I have a file at the end of my executable and I would like to give its path to a dll. Of course since it doesn't have a real path, I have to fake it.
我第一次尝试在 Windows 下使用命名管道来做到这一点.这将允许像 \.pipemymemoryfile 这样的路径,但我不能让它工作,而且我不确定 dll 是否支持这样的路径.
I first tried using named pipes under Windows to do it. That would allow for a path like \.pipemymemoryfile but I can't make it works, and I'm not sure the dll would support a path like this.
其次,我找到了 CreateFileMapping 和 GetMappedFileName.它们可以用来模拟另一个片段中的文件吗?我不确定这个 API 是做什么的.
Second, I found CreateFileMapping and GetMappedFileName. Can they be used to simulate a file in a fragment of another ? I'm not sure this is what this API does.
我正在尝试做的事情似乎与 boxedapp 类似.关于他们如何做到这一点的任何想法?我想这有点像 API 拦截(比如 Detour),但这需要做很多工作.还有其他方法吗?
What I'm trying to do seems similar to boxedapp. Any ideas about how they do it ? I suppose it's something like API interception (Like Detour ), but that would be a lot of work. Is there another way to do it ?
为什么?我对这个特定的解决方案很感兴趣,因为我想隐藏数据,以便只分发一个文件,但也出于让它以这种方式工作的极客原因;)我同意将数据复制到临时文件会起作用并且是一个更简单的解决方案.
Why ? I'm interested in this specific solution because I'd like to hide the data and for the benefit of distributing only one file but also for geeky reasons of making it works that way ;)I agree that copying data to a temporary file would work and be a much easier solution.
推荐答案
您可以将数据存储在 NTFS 流中.这样你就可以得到一条指向你的数据的真实路径,你可以以
You can store the data in an NTFS stream. That way you can get a real path pointing to your data that you can give to your dll in the form of
x:myfile.exe:mystreamname
这与普通文件完全一样,但只有在使用的文件系统是 NTFS 时才有效.这是当今 Windows 下的标准,但如果您想支持旧系统或希望能够从 U 盘或类似设备运行它,这当然不是一个选项.请注意,如果文件作为邮件附件发送或简单地从 NTFS 分区复制到 FAT32 分区,则文件中存在的任何流都将丢失.
This works precisely like a normal file, however it only works if the file system used is NTFS. This is standard under Windows nowadays, but is of course not an option if you want to support older systems or would like to be able to run this from a usb-stick or similar. Note that any streams present in a file will be lost if the file is sent as an attachment in mail or simply copied from a NTFS partition to a FAT32 partition.
我认为最兼容的方法是将数据写入实际文件,但您当然可以在 NTFS 系统上以一种方式在 FAT 系统上这样做.由于增加了复杂性,我确实不建议这样做.适当的方法当然是单独分发您的文件,但由于您已表示不希望这样做,在这种情况下,您应该将其写入临时文件并为 dll 提供该文件的路径.确保将临时文件写入用户的临时目录(您可以使用 GetTempPath 在 C/C++ 中).
I'd say that the most compatible way would be to write your data to an actual file, but you can of course do it one way on NTFS systems and another on FAT systems. I do recommend against it because of the added complexity. The appropriate way would be to distribute your files separately of course, but since you've indicated that you don't want this, you should in that case write it to a temporary file and give the dll the path to that file. Make sure you write the temporary file to the users' temp directory (you can find the path using GetTempPath in C/C++).
您的另一个选择是编写一个文件系统过滤器驱动程序,但我强烈建议不要这样做.这也违背了使用单个文件的目的......
Your other option would be to write a filesystem filter driver, but that is a road that I strongly advise against. That sort of defeats the purpose of using a single file as well...
另外,如果您只想分发一个文件,使用 zip 文件或安装程序怎么样?
Also, in case you want only a single file for distribution, how about using a zip file or an installer?
这篇关于如何创建虚拟文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!