加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 修改自身的正在运行的可执行文件

发布时间:2020-12-16 07:00:48 所属栏目:百科 来源:网络整理
导读:我有一个用于 Windows CE 5的Visual Studio 2008 C项目,我希望当前运行的可执行文件可以自行修改. 具体来说,我希望能够读取/写入exe文件本身存储的一些数据.我不需要(或希望)修改可执行代码. 在常规窗口中,我可以使用字符串资源和UpdateResource函数,但在Win
我有一个用于 Windows CE 5的Visual Studio 2008 C项目,我希望当前运行的可执行文件可以自行修改.

具体来说,我希望能够读取/写入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.
}

祝你的项目好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读