Strange character in place of the directory

Asked

Viewed 31 times

1

When I run this code in the place where it was supposed to appear the directory appears Ó²o formula.exe, someone could help me solve ?

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



}
  • Where is the content of TCHAR teste[MAX_PATH]; is initialized?

  • You really wanted to cmd += teste; or should have been cmd += teste();? Having two different things with the name teste is to ask for confusion.

  • Can solve as follows

1 answer

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.