1

Quando executo esse código no lugar onde que era para aparecer o diretório aparece Ó²o\formula.exe, alguém poderia me ajudar a resolver ?

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

using namespace std;

string teste() {
    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(){
        TCHAR teste[MAX_PATH];
        cout << teste << endl;


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


       cout << cmd.c_str() << endl;

        system(cmd.c_str());



}
3
  • Onde é que o conteúdo de TCHAR teste[MAX_PATH]; é inicializado? 23/05/2018 às 4:54
  • Você queria mesmo cmd += teste; ou deveria ter sido cmd += teste();? Ter duas coisas distintas com o nome teste é pedir para ter confusão. 23/05/2018 às 4:56
  • Conseguir resolver da seguinte maneira
    – Nycolas
    23/05/2018 às 5:03

1 Resposta 1

0
#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(){


cout << dir() << endl;
        string cmd = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d ";
        cmd += dir();
        cmd += "\\formula.exe";

        cout << cmd.c_str() << endl;

        system(cmd.c_str());

    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 .