C++ 运行 批处理文件作为独立进程并终止并删除 argv[0]

C++ Run Batch file as standalone process and kill and remove argv[0]

假设我有以下代码:

    ofstream selfdelfile;
    selfdelfile.open("selfdel.bat",ios::out);
    selfdelfile << "del \"" << argv[0] << "\"" << endl;
    selfdelfile << "del %0";
    selfdelfile.close();
    system("cmd /K selfdel.bat &");

C++ 应用程序 creates/writes 文件 selfdel.bat 包含以下行

del "C:\Users\temp\test_app.exe" 
del %0

因此,Batch 应该 remove/delete exe 应用程序及其本身。 所以这里有麻烦:由于 C++ 应用程序按 system("selfdel.bat") 批处理,因此无法删除 exe 应用程序,输出显示进程无法终止。 Ofc,C++ app 正在等待批处理,批处理无法删除.exe。 我正在尝试终止进程,删除文件,e.t.c。 那么该怎么做呢?可能是启动一个新进程或线程?有人可以提供一个简单的示例 C++ 运行 Batch ,并且 Batch 删除自身 + 应用程序。谢谢

我找到了适合我的解决方案,只需在 batchfile

之前添加 "start"
system("start selfdel.bat &");

现在批处理在新 windows 中打开,c++ 应用程序正在关闭和删除。