ANSI-C程序,它将资源添加到Portable Executable exe文件中 [英] ANSI-C program that adds resources to a Portable Executable exe file

查看:50
本文介绍了ANSI-C程序,它将资源添加到Portable Executable exe文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我正在从Web服务器分发Windows应用程序。此应用程序具有存储在Windows资源中的嵌入式配置。一开始,我用Windows机器上的嵌入式资源编译可执行文件,所有
工作正常(我使用win32 api来处理资源)。但是现在,我有太多的应用程序版本,我想直接从我的服务器上即时编译它们。由于某些服务器不运行Windows,我需要一个ANSI-C应用程序来添加
资源(不使用win32特定的API函数!)。


我下载了" Microsoft可移植可执行文件和通用目标文件格式规范"来自msdn网站的v8.3并尝试编写我自己的
资源编译器。我现在能够添加资源,如果我使用像ReExplorer这样的第三方,我可以找到添加的资源并提取正确的内容。但是一旦应用程序被我自己编写的工具修补,Windows就不再执行它了
- >启动应用程序时出现win32错误0n193。


所以,我的结论是,在修补原始exe文件以添加我的额外资源时,我做错了,或者我不知道做必须做的事情!


这是我目前正在做的事情的描述:


   - 首先,从Windows机器和MSVC ++工具,我编译一个应用程序并添加一个"虚拟"工具。空资源。这个虚拟资源允许我已经在节头中拥有资源条目,因此,在添加真实资源内容时简化了
我的工作。由于我不知道最终资源的大小,我添加一个空的(或者只是一个字节,否则Windows资源编译器会跳过资源!)。


   - 然后,从服务器,我有一个小型C ++应用程序执行以下操作:


   - 它找到"IMAGE_RESOURCE_DATA_ENTRY"将资源放入exe文件中。


   - 它扩大了.rsrc数据部分以保存新的资源数据(考虑到页面对齐的东西)


   - 它在放大的.rsrc部分的末尾复制新的资源内容。


   - 它修补相应的"IMAGE_RESOURCE_DATA_ENTRY"。指向这些新添加的数据。


   - 它改变了位于".rsrc"之后的部分的偏移量。部分(在我的情况下,只有一个部分:" .reloc"。


    - 它将新图像打包到新的exe文件中并将其发送到客户端。


这是一个exe文件,其图标由windows正确显示,可以通过PeExplorer等工具解析,并使用此工具正确提取新添加的资源,通过像exescan这样的工具进行分析,没有显示任何错误,
但是当从具有错误0n193的Windows机器启动时崩溃(windbg甚至没有启动应用程序,告诉我我的应用程序不是有效的win32)。


我担心的是我还必须修补.reloc部分中的内容,以便正确地考虑到我改变了.rsrc部分的大小。但我读了文档在这一部分我并不确定我理解它。所以,如果我b $ b需要改变它,我不知道应该改变什么......


任何帮助都是h非常感谢。特别是,如果你知道一个现有的库可以实现我尝试做的事情,或者你知道一个工具可以分析我生成的exe来告诉我我做错了什么,或者你知道我是做什么的
做错了,我真的很感激任何建议。


问候,


Brice


解决方案

完全错误193意味着"ERROR_BAD_EXE_FORMAT"。


因为你正在做所有事情你自己需要检查你改变的一切。添加资源本身是不够的。如果我正确地记住了资源大小,那么也存在一些校验和。


根本没有使用Windows API我自己从未尝试过更新资源。所有这一切都是很多工作,你真的需要了解或多或少了解完整的COFF文档。


根据我的理解,使用Pavel提到的唯一"简单"的解决方案。方式。


Dear all,

I am distributing a Windows application from a web server. This application has an embedded configuration that is stored in a windows resource. At the beginning, I was compiling the executable with embedded resources from a Windows machine and everything worked fine (I used the win32 api to handle resources). But now, I have too much versions of the application and I want to compile them on-the-fly directly from my server. As some servers do not run Windows, I need to have an ANSI-C application that adds the resources (no use of win32 specific API functions !).

I downloaded the "Microsoft Portable Executable and Common Object File Format Specification" v8.3 from msdn site and tried to write my own resource compiler. I am now able to add the resources and, if I use a third-party like ReExplorer, I can find the added resources and extract the correct content. But once the application has been patched by my self-written tool, Windows does not execute it anymore -> I get a win32 error 0n193 when starting the application.

So, my conclusion is that, either I do something wrong when patching the original exe file to add my additional resource, or I don't do something mandatory !

Here is a description of what I am currently doing :

   - First, from a Windows machine and with the MSVC++ tools, I compile an application and add a "dummy" empty resource. This dummy resource allows me to already have the resource entry in the section header and thus, simplifies my job when adding the real resource content. As I don't know the size of the final resource, I add an empty one (or just one byte, else windows resource compiler skips the resource !).

   - Then, from the server, I have a small C++ application that performs the following :

   - It finds the "IMAGE_RESOURCE_DATA_ENTRY" of the resource into the exe file.

   - it enlarges the .rsrc data section to hold the new resource data (taking into account the page alignment stuff)

   - it copies the new resource content at the end of the enlarged .rsrc section.

   - it patches the corresponding "IMAGE_RESOURCE_DATA_ENTRY" to point to this newly added data.

   - it changes the offset of the sections located after the ".rsrc" section (in my case, there is only one section : ".reloc".

    - it packs the new image in a new exe file and sends it to the client.

The result of this is an exe file whose icon is properly displayed by windows, that can be parsed by tools like PeExplorer with newly added resource properly extracted by this tool, that can be analysed by a tool like exescan without any error displayed, but that crashes when launched from a windows machine with error 0n193 (windbg even does not start the app, telling me that my application is not a valid win32 one).

What I fear is that I also have to patch something in the .reloc section in order to properly take into account that I changed the size of the .rsrc section. But I read the documentation on this section and I am not really sure I understand it. So, if I need to change something in it, I have no idea of what should be changed...

Any help would be highly appreciated. In particular, if you are aware of an existing library that would implement what I try to do, or if you are aware of a tool that could analyse my produced exe to tell me what I am doing wrong, or if you know what I a doing wrong, I would really appreciate any suggestion.

Regards,

Brice

解决方案

At all Error 193 means "ERROR_BAD_EXE_FORMAT".

Since you are doing all by yourself you need to check everything you changed. It is not enough to add the resourcesl itself. If I remmeber correctly the resource size is also there and also a few checksums.

At all I never tried to update resources by myself with NOT using Windows API. Doing this all by yourself is a lot work and you really need tu understand more or less the complete COFF documentation.

From my understanding using the solution that Pavel mentioned is the only "easy" way.


这篇关于ANSI-C程序,它将资源添加到Portable Executable exe文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆