store directory

1

I would like to know how to add the user name where it is in quotation marks written "should I put the username here"

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
#include<lmcons.h>

using namespace std;

int main() {
//User Name
TCHAR username[UNLEN + 1];
DWORD username_len = UNLEN + 1;

GetUserName((TCHAR*)username, &username_len);

wcout << username << endl;

//Computer name
TCHAR compname[UNCLEN + 1];
DWORD compname_len = UNCLEN + 1;

GetComputerName((TCHAR*)compname, &compname_len);

wcout << compname << endl;

cin.get();

system("reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run /t REG_SZ /v formula /d C://users//"DEVO COLOCAR O NOME DO USUARIO AQUI"//downloads//formula.exe");

return EXIT_SUCCESS;

}
    
asked by anonymous 05.05.2018 / 19:11

1 answer

1

There are a few ways you can do what you want. Of the several I show two.

sprintf

With sprintf can interpolate a string with any values you want. First you need to construct string as char[] that will take the final text and then call sprintf on it:

char username[] = "carlos";
char cmd[200];
sprintf(cmd, "reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run /t REG_SZ /v formula /d C://users//%s//downloads//formula.exe", username);

system(cmd);

Look closely at the text used in sprintf that has %s where the name is placed.

See this example in Ideone

Concatenation of string

By using the + operator on a% c ++%, you can concatenate a string directly, making the whole process easy too:

char username[] = "carlos";
string cmd = "reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run /t REG_SZ /v formula /d C://users//";
cmd += username;
cmd += "//downloads//formula.exe";

system(cmd.c_str());

See this example on Ideone

This version has some differences to point out. The command is built as char[] and not string . For this reason the call to char[] that is supposed to pass system must be with char* to get cmd.c_str() in format string .

    
06.05.2018 / 02:33