简体   繁体   English

如何在运行时更改.exe的名称

[英]How to change the name of .exe on runtime

Persay, if I wanna change the .exe on runtime of my program (Like its original name would be: someexe.exe and after you have closed it will change to something random or similiar) 也许,如果我想在程序运行时更改.exe(就像它的原始名称是:someexe.exe,并且在关闭后它将更改为随机或类似的名称)

I've seen it happen before and I'm very much interested in implementing it just for fun! 我以前见过它,我非常有兴趣实施它,只是为了好玩!

Thanks :) 谢谢 :)

I'm not 100% sure, but the regular rename function should work. 我不确定100%,但是常规rename功能应该可以正常工作。 However, I'm not sure if it's possible to rename a file that is currently held open by the OS (and I have no Windows system that I can test on right now). 但是,我不确定是否可以重命名操作系统当前保持打开状态的文件(并且我没有可以立即测试的Windows系统)。

If you can't do it on "your own executable", then you'd have to rename the file by running another program [or batchfile] and then resuming your own program [eg let the rename program start your program again, and it recognising somehow that it's now the new name and can continue. 如果您无法在“自己的可执行文件”上执行此操作,则必须通过运行另一个程序[或batchfile]然后恢复自己的程序来重命名文件[例如,让重命名程序再次启动程序,然后认识到它现在是新名称并且可以继续使用。

Possible code 可能的代码

#include <iostream>
#include <windows.h>
int main(int argc, char** argv) 
{   
  system("title a.exe");
  std::cout<<"a.exe is the current name of your .exe file\n";
  //do your stuff

  std::cout<<"a.exe is changing to b.exe\n";
  system("RENAME C:\\path\\a.exe b.exe");
  system("title b.exe");
  //do your stuff

  std::cout<<"now b.exe is changing to a.exe\n";
  system("title a.exe");
  system("RENAME C:\\path\\b.exe a.exe");
  //do you stuff
  return 0;
}

But using system command is considered to be bad practise therefore you should use rename command ,, code : 但是使用system命令被认为是不好的做法,因此您应该使用rename命令代码:

#include <stdio.h>
int main(int argc , char** argv)
{
  rename("a.exe","bii.exe");
  //do your stuff
  rename("bii.exe","a.exe");
  //do your stuff
  return 0;
}

You have to make a copy of the executable file. 您必须复制可执行文件。 Then you can rename that file and when you close your current program file you need to transfer control to another program preferably a batch file which deleted the original executable. 然后,您可以重命名该文件,并且在关闭当前程序文件时,需要将控制权转移到另一个程序,最好是删除原始可执行文件的批处理文件。 in windows system() should be able to do these stuff. 在Windows system()中应该能够执行这些操作。 you also need to write your own batch file with code to delete thr program that calls it. 您还需要使用代码编写自己的批处理文件,以删除调用该文件的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM