C++ Win32 替换可执行文件中的字符串
我正在寻找一种好方法来替换本机 win32 编译的 exe 中的多个字符串。例如,我的代码中有以下内容:
const char *updateSite = "http://www.place.com"
const char *updateURL = "/software/release/updater.php"
我需要用 exe 中的其他任意长度字符串修改这些字符串。我意识到我可以将这种类型的配置存储在其他地方,但将其保留在 exe 中可以满足我的应用程序的可移植性要求。我将不胜感激任何有关最佳方法的帮助和/或建议。
谢谢!
更新:我在 Metasploit 项目中发现了一些代码似乎可以做到这一点: MSF:Util:Exe
I'm looking for a good way to replace several strings inside a native win32 compiled exe. For example, I have the following in my code:
const char *updateSite = "http://www.place.com"
const char *updateURL = "/software/release/updater.php"
I need to modify these strings with other arbitrary length strings within the exe. I realize I could store this type of configuration elsewhere, but keeping it in the exe meets the portability requirements for my app. I would appreciate any help and/or advice on the best way to do this.
Thanks!
Update: I found some code in the Metasploit project that seems to do this:
MSF:Util:Exe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会搞乱 EXE 本身,如果您确实需要 1 个文件,那么请执行旧的 zip 附加技巧并将您的配置放在那里。
可能看起来像这样:
优点:
缺点:
如果你不想压缩它,那么只需自己编写一些简单的未压缩存档。
I would not mess around the the EXE itself, if you really need 1 file, then do the old zip append trick and put your configs in there.
Could look like this:
Pros:
Contras:
If you don't want to zip it, then just write some simple uncompressed archive thing your own.
PE 文件中有全局重定位表 - 它是必须由 PE 加载器更改的地址列表(例如,必须运行时存储的全局变量或常量,例如字符串)。如果您知道这个特定变量是哪个条目,您可以获取它的地址,然后手动更改它。然而,这将是一个彻头彻尾的问题,您需要深入了解您最喜欢的编译器和 PE 格式。使用 XML 或 Lua 或其他完全可移植的东西会更容易 - 它们正是为了这种目的而发明的。
编辑:
为什么不只使用 const char** ?这是一个正常的运行时变量有什么问题吗?
In a PE file is the global relocations table- it is a list of addresses (for example, global variables or constants that must be runtime-stored, like, say, strings) that must be altered by the PE loader. If you knew which entry this particular variable was, you could get it's address and then alter it manually. However, this would be a total bitch and you'd need an in-depth knowledge of your favourite compiler and the PE format. Easier just to use XML or Lua or something else that's totally portable - they were invented for exactly this kind of purpose.
Edit:
Why not just use a const char**? Is there something wrong with this being a normal runtime variable?
IMO 是将字符串存储在字符串表资源中的最佳位置。它已合并到您的 .EXE 文件中,因此可移植性不会受到影响。
使用 Visual Studio 编辑器进行更改那个值。
使用 LoadString WinAPI,或更好的 CString::LoadString 方法,在您的代码中加载值。
还有第 3 方软件允许您修改已编译的 .EXE 中的字符串,而无需重新编译。一个例子是资源黑客。
IMO the best place to store that strings in a string table resource. It's incorporated into your .EXE file, so the portability will not be compromised.
Use the visual studio editor to alter that values.
Use LoadString WinAPI, or better, CString::LoadString method, in your code, to load the values.
There's also 3-rd party software allowing you to modify the strings in the compiled .EXE, without recompilation. An example is Resource Hacker.