問題描述
我想模擬一個文件而不將它寫在磁盤上.我的可執(zhí)行文件末尾有一個文件,我想提供它的 dll 路徑.當(dāng)然,因為它沒有真正的路徑,所以我必須偽造它.
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 類似.關(guān)于他們?nèi)绾巫龅竭@一點的任何想法?我想這有點像 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 ?
為什么?我對這個特定的解決方案很感興趣,因為我想隱藏數(shù)據(jù),以便只分發(fā)一個文件,但也出于讓它以這種方式工作的極客原因;)我同意將數(shù)據(jù)復(fù)制到臨時文件會起作用并且是一個更簡單的解決方案.
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.
推薦答案
您可以將數(shù)據(jù)存儲在 NTFS 流中.這樣你就可以得到一條指向你的數(shù)據(jù)的真實路徑,你可以以
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
這與普通文件完全一樣,但只有在使用的文件系統(tǒng)是 NTFS 時才有效.這是當(dāng)今 Windows 下的標(biāo)準(zhǔn),但如果您想支持舊系統(tǒng)或希望能夠從 U 盤或類似設(shè)備運行它,這當(dāng)然不是一個選項.請注意,如果文件作為郵件附件發(fā)送或簡單地從 NTFS 分區(qū)復(fù)制到 FAT32 分區(qū),則文件中存在的任何流都將丟失.
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.
我認為最兼容的方法是將數(shù)據(jù)寫入實際文件,但您當(dāng)然可以在 NTFS 系統(tǒng)上以一種方式在 FAT 系統(tǒng)上這樣做.由于增加了復(fù)雜性,我確實不建議這樣做.適當(dāng)?shù)姆椒ó?dāng)然是單獨分發(fā)您的文件,但由于您已表示不希望這樣做,在這種情況下,您應(yīng)該將其寫入臨時文件并為 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++).
您的另一個選擇是編寫一個文件系統(tǒng)過濾器驅(qū)動程序,但我強烈建議不要這樣做.這也違背了使用單個文件的目的......
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...
另外,如果您只想分發(fā)一個文件,使用 zip 文件或安裝程序怎么樣?
Also, in case you want only a single file for distribution, how about using a zip file or an installer?
這篇關(guān)于如何創(chuàng)建虛擬文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!