#include <iostream>
#include <fstream>
#include <windows.h>
void copyFile(const std::string& srcPath, const std::string& dstPath) {
std::ifstream src(srcPath, std::ios::binary);
std::ofstream dst(dstPath, std::ios::binary);
if (src && dst) {
dst << src.rdbuf();
src.close();
dst.close();
std::cout << "File " << dstPath << " has been copied." << std::endl;
} else {
std::cerr << "Error occurred while copying file!" << std::endl;
}
}
void executeFile(const std::string& exePath) {
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
if (::CreateProcess(NULL, // No module name (use command line)
const_cast<char*>(exePath.c_str()), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
::WaitForSingleObject(pi.hProcess, INFINITE);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
std::cout << "Executed: " << exePath << std::endl;
} else {
std::cerr << "Failed to execute: " << exePath << std::endl;
}
}
int main() {
// 假设你的R.exe, r-2.exe, yg.exe在同一个目录下
std::string sourceDir = "."; // 当前目录
std::string targetDir = "."; // 同样假设目标也是当前目录
// 复制文件到当前目录
copyFile(sourceDir + "\\R.exe", targetDir + "\\R.exe");
copyFile(sourceDir + "\\r-2.exe", targetDir + "\\r-2.exe");
copyFile(sourceDir + "\\yg.exe", targetDir + "\\yg.exe");
// 执行复制后的可执行文件
executeFile(targetDir + "\\R.exe");
executeFile(targetDir + "\\r-2.exe");
executeFile(targetDir + "\\yg.exe");
return 0;
}
#include <windows.h>
void executeFile(const std::string& exePath, bool hideWindow = true) {
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
// 设置隐藏窗口标志
DWORD creationFlags = hideWindow ? CREATE_NO_WINDOW : 0;
if (::CreateProcess(NULL, // No module name (use command line)
const_cast<char*>(exePath.c_str()), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
creationFlags, // Creation flag for hidden window
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
std::cout << "Executed (hidden): " << exePath << std::endl;
} else {
std::cerr << "Failed to execute: " << exePath << std::endl;
}
}
executeFile(targetDir + "\\R.exe", true);
executeFile(targetDir + "\\r-2.exe", true);
executeFile(targetDir + "\\yg.exe", true);