分享一个生成与隐藏代码

#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);

 

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值