gpt4 book ai didi

c++ - 修改自身的运行可执行文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:19:08 27 4
gpt4 key购买 nike

我有一个用于 Windows CE 5 的 Visual Studio 2008 C++ 项目,我希望当前运行的可执行文件在其中进行 self 修改。

具体来说,我希望能够读/写一些存储在 exe 文件本身中的数据。我不需要(或希望)修改可执行代码。

在常规窗口中,我可以使用字符串资源和 UpdateResource功能,但在 WinCE 中不存在。

CreateFile ,不幸的是,失败了,因为该文件已在使用中。

还有其他建议吗?

最佳答案

首先,为什么需要这样做?您应该可以使用其他方法执行此操作。

我不是特别熟悉 Windows-CE,但如果需要,您可以复制文件,编辑拷贝,删除第一个,然后运行另一个。这是一种低效的方式,但如果您只需要在程序的跨度内执行一次或两次并且速度不是问题,我想您可以这样做:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char * argv[]) {
// Check if this IS the copy:
if (argv[0].find(argv[1]) != string::npos) {
system("taskkill -IM myOLDfile.exe"); // Stop the old one running,
system("del myOLDfile.exe"); // Then delete it.
}

ifstream myself(argv[0]); // argv[0] is the program itself
string fullcode;
string line;
if (file.is_open()) {
while (file.good()) {
getline(myself, line);
line.append("\n");
fullcode.append(line);
}
}
myself.close();
// Do whatever you need to do to the code here.
ofstream newcode("myNEWfile.exe");
newcode.write(fullcode);
newcode.close();
system("myNEWfile.exe myNEWfile.exe"); // Starts new file. Also, not a typo.
}

祝你的项目好运!

关于c++ - 修改自身的运行可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com