我一直在试图弄清楚安装程序是如何工作的,它们如何将所有内容捆绑到一个可执行文件中,以及如何创建我自己的安装程序。我尝试使用名为 HxD 的十六进制编辑器,它允许您将文件的当前十六进制转储导出到 .c
源文件中,其中包含包含十六进制转储的数组,如下所示。
兴奋,我尝试使用一些简单的 C++ 代码编写该文件:
ofstream newbin("test.exe", ios::binary);
newbin << hex << rawData;
newbin.close();
...然后尝试运行它。
经过进一步研究,结果发现我的小程序只编写了 MZ。 PE 文件在 Windows 中使用的标头,并排除其余代码。创建的可执行文件具有 4D 5A 90
或 ASCII MZ.
的十六进制转储。这是我的编码错误吗?为什么它不写入十六进制数据?我需要使用一些较低级别的编写工具或程序集吗?如果是这样,是否有任何 C/C++ 库可以让我达到这样的水平?谢谢!
I have been trying to figure out how installers work and how they bundle everything into one executable file and how to create my own. I have tried using a hex editor called HxD which allows you to export the current hex-dump of a file into a .c
source file with an array containing the hex dump that looks like the below.
Excited, I tried to write the file using some simple C++ code:
ofstream newbin("test.exe", ios::binary);
newbin << hex << rawData;
newbin.close();
... and then tried to run it.
After some further research it turns out that my little program is only writing the MZ.
header which PE files use in windows and excluding the rest of the code. The executable that is created has a hex-dump of 4D 5A 90
or in ASCII MZ.
. Is this a fault in my coding? Why won't it write the hex data? Would I need to use some lower-level writing tool or assembly? If so, are there any C/C++ libraries that allow me to write at such a level? Thanks!
发布评论
评论(2)
rawData
是一个char*
,并被流操作符解释为字符串,并以它遇到的第一个0x00
字节终止。对于二进制写入,您最好使用该
方法,导致
假设 65536 是缓冲区的实际使用大小。
希望这有帮助:)
rawData
is achar*
and is interpreted as a character string by the streaming operator, which is terminated by the first0x00
byte it encounters.For binary writing, you are best off using the
method, leading to
Assuming 65536 is the actual used size of the buffer.
Hope this helps :)
存储二进制数据的更好方法是使用资源。菜单、图标、位图存储在资源中。
您可以创建自定义资源并使用
FindResource
函数,LoadResource
,然后LockResource
将其映射到内存中。然后你可以对数据做任何你想做的事情,当然也可以将其写入文件。
安装程序通常使用类似的东西,而不是在源代码中嵌入大量二进制数据。这种方法还有其他优点:
使用当前的方法,所有数据都加载到内存中,因此您的应用程序需要更多内存。
此外,您应该更好地使用专门的工具来创建安装程序。
A better approach to storing binary data is to use resources. Menus, icons, bitmaps are stored in resources.
You can create a custom resource and use
FindResource
function,LoadResource
, and thenLockResource
to map it into memory.Then you can do whatever you want with the data, and of course write it to a file.
Installers usually use something like this rather than embedding lots binary data in the source code. This approach has other advantages:
With your current approach, all the data are loaded into memory, therefore your application requires more memory.
Additionally, you should better use specialized tools for creating installers.