将十六进制数据写入可执行文件不起作用? C++

发布于 2025-01-05 18:34:25 字数 698 浏览 1 评论 0 原文

我一直在试图弄清楚安装程序是如何工作的,它们如何将所有内容捆绑到一个可执行文件中,以及如何创建我自己的安装程序。我尝试使用名为 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.

enter image description here

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.

enter image description here

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时光的容颜 2025-01-12 18:34:25

rawData 是一个 char*,并被流操作符解释为字符串,并以它遇到的第一个 0x00 字节终止。

对于二进制写入,您最好使用该

ostream& write(const char*, int);

方法,导致

newbin.write(rawData, 65536);

假设 65536 是缓冲区的实际使用大小。

希望这有帮助:)

rawData is a char* and is interpreted as a character string by the streaming operator, which is terminated by the first 0x00 byte it encounters.

For binary writing, you are best off using the

ostream& write(const char*, int);

method, leading to

newbin.write(rawData, 65536);

Assuming 65536 is the actual used size of the buffer.

Hope this helps :)

任性一次 2025-01-12 18:34:25

存储二进制数据的更好方法是使用资源。菜单、图标、位图存储在资源中。

您可以创建自定义资源并使用 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 then LockResource 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:

  • You don't have to re-convert your data into source code and then recomplile the whole application when the data changes. Only resources have to be recompiled and re-linked.
  • The resources are not loaded into memory until use the functions above, what means until you need them. Thus the application loads faster into the memory. (Resource data are actually mapped into address space right from the file.)
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文