简体   繁体   English

在C ++中的.exe应用程序中包含文件(dll,txt等)

[英]Including Files(dll, txt etc.) in .exe Application in C++

I made a program written in C++ that uses many files, like .dll or .png. 我创建了一个用C ++编写的程序,它使用许多文件,比如.dll或.png。 The program and the files are in the same directory. 程序和文件位于同一目录中。 Is it possible including files in the .exe application? 是否可以包含.exe应用程序中的文件? So that I only need the .exe file to run the program. 所以我只需要.exe文件来运行程序。

Ah, yes. 没错。 The answer is writing all the files you need, contained in a zip file, at the end of the executable you're running. 答案是在您正在运行的可执行文件的末尾写下所需的所有文件,包含在zip文件中。 Here's how: Part 1 (writing to the executable) and Part 2 [broken link] (reading from the executable). 方法如下: 第1部分 (写入可执行文件)和 第2部分 [断开链接](从可执行文件中读取)。 It's fun stuff; 这很有趣; good question. 好问题。

Note that you can't do this for DLL files - they must either be in the same directory as the executable or installed on the machine. 请注意,您无法对DLL文件执行此操作 - 它们必须与可执行文件位于同一目录中或安装在计算机上。

Interesting question and answer. 有趣的问答。

I did it a long time ago by putting all the files inside the .exe as resources, and then extracting them all using the following procedure: 我很久以前把它作为资源放在.exe中的所有文件,然后使用以下过程将它们全部解压缩:

HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(MY_RESOURCE_ID), MY_RESOURCE_TYPE);
HGLOBAL hRes = LoadResource(NULL, hResInfo)
LPVOID lpData = LockResource(hRes);
HANDLE hFile = CreateFile(....)
WriteFile(hFile, lpData, SizeOfResource(NULL, hResInfo), ...);
CloseHandle(hFile)

The problem with my implementation was that I had to recompile the .exe everytime a new file was added (which happened only once or twice, luckily), but it's good to know how does it work under the hood. 我的实现问题是我每次添加一个新文件时都必须重新编译.exe(幸运的是只发生过一次或两次),但很高兴知道它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM